git-serve-server.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // @ts-check
  2. const https = require('https');
  3. const http = require('http');
  4. const fs = require('fs');
  5. const gitstatic = require("./git-serve");
  6. const express = require("express");
  7. const repository = '.git';
  8. const app = express();
  9. app.get(/^\/.+/, gitstatic.route().repository(repository));
  10. app.get(/\//, (req, res) => {
  11. gitstatic.listAllCommits(repository, (err, commits) => {
  12. console.log(err, commits);
  13. res.send(
  14. commits.map((commit) => {
  15. return `<a href="/${commit.sha}/public/index.html" target="_blank"><span style="font-family: monospace;">${commit.sha.slice(0, 7)} - ${commit.date.toISOString()}</span></a> - <a href="https://github.com/morethanwords/tweb/commit/${commit.sha}" target="_blank">${commit.subject}</a><br>`;
  16. }).join('')
  17. );
  18. });
  19. });
  20. const { networkInterfaces } = require('os');
  21. const nets = networkInterfaces();
  22. const results = {};
  23. for(const name of Object.keys(nets)) {
  24. for(const net of nets[name]) {
  25. // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
  26. if(net.family === 'IPv4' && !net.internal) {
  27. if(!results[name]) {
  28. results[name] = [];
  29. }
  30. results[name].push(net.address);
  31. }
  32. }
  33. }
  34. const useHttp = false;
  35. const transport = useHttp ? http : https;
  36. let options = {};
  37. if(!useHttp) {
  38. options.key = fs.readFileSync(__dirname + '/certs/server-key.pem');
  39. options.cert = fs.readFileSync(__dirname + '/certs/server-cert.pem');
  40. }
  41. console.log(results);
  42. const port = 3000;
  43. const protocol = useHttp ? 'http' : 'https';
  44. console.log('Listening port:', port);
  45. function createServer(host) {
  46. const server = transport.createServer(options, app);
  47. server.listen(port, host, () => {
  48. console.log('Host:', `${protocol}://${host || 'localhost'}:${port}/`);
  49. });
  50. server.on('error', (e) => {
  51. // @ts-ignore
  52. if(e.code === 'EADDRINUSE') {
  53. console.log('Address in use:', host);
  54. server.close();
  55. }
  56. });
  57. }
  58. for(const name in results) {
  59. const ips = results[name];
  60. for(const ip of ips) {
  61. createServer(ip);
  62. }
  63. }
  64. createServer();