jgkaplan / gemini-server

Node server for the gemini protocol, based on express
ISC License
65 stars 9 forks source link

Vhost support #26

Open ademayo opened 1 year ago

ademayo commented 1 year ago

The ability to host multiple hostnames

jgkaplan commented 1 year ago

Do you have an example of how you would want this to work?

ademayo commented 1 year ago

Sorry about that, should've been more descriptive - maybe something like

  const app = gemini({
    hosts: {
      "example": {
          domain: 'example.com',
          cert: readFileSync("./path/to/cert.pem"),
          key: readFileSync("./path/to/key.pem"),
          titanEnabled: true
      },
       "example2": {
          domain: 'example2.com',
          cert: readFileSync("./path/to/cert.pem"),
          key: readFileSync("./path/to/key.pem"),
          titanEnabled: true
      }
    }
  });

  const app1 = app.getHost('example');

  app1.on(.....

  const app2 = app.getHost('example2');

  app2.on(.....
ademayo commented 1 year ago

Actually, there's an existing vhost package for express - https://github.com/expressjs/vhost could this be adapted to work in the gemini context - it doesn't seem looking at the source to require anything specific to express. I tried just using this middleware as it exists, and unfortunately it didn't work - I tried

const fs = require('fs');
const gemini = require('gemini-server').default;
const vhost = require('vhost');

const app = gemini({
    cert: fs.readFileSync('./certs/main/cert.pem'),
    key: fs.readFileSync('./certs/main/key.pem'),
    titanEnabled: false
});

const app1App = gemini({
    cert: fs.readFileSync('./certs/app1/cert.pem'),
    key: fs.readFileSync('./certs/app1/key.pem'),
    titanEnabled: false
});

app1App.on('/', (req, res) => {
    res.file('./capsules/app1/index.gmi');
});

const app2App = gemini({
    cert: fs.readFileSync('./certs/app2/cert.pem'),
    key: fs.readFileSync('./certs/app2/key.pem'),
    titanEnabled: false
});

app2App.on('/', (req, res) => {
    res.file('./capsules/app2/index.gmi');
});

app.use(vhost('app1.local', (req, res) => {
    app1App.emit('request', req, res);
}));

app.use(vhost('app2.local', (req, res) => {
    app2App.emit('request', req, res);
}));

app.listen(1965);

and I just get the error

  var host = req.headers.host
                         ^

TypeError: Cannot read properties of undefined (reading 'host')

So it looks with a little bit of adapting it could work

jgkaplan commented 1 year ago

With some adapting, I think it could work. Though I'm using the same term "middleware" here, it's different than express middleware.