35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
// Creates endpoint for caddy's ask feature (https://caddyserver.com/docs/json/apps/tls/automation/on_demand/ask/)
|
|
// Returns status 200 if domain given in ?domain= already exists in .wiki folder, otherwise it returns an error
|
|
|
|
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
|
|
const address = process.env.ADDRESS || "localhost";
|
|
const port = process.env.PORT || 3000;
|
|
const dataDirectory = process.env.DIRECTORY || __dirname;
|
|
|
|
app.get('/', (req, res) => {
|
|
const folderName = req.query.domain;
|
|
|
|
if (!folderName) {
|
|
return res.status(400).json({ error: `No query, don't create certificate` });
|
|
}
|
|
|
|
const folderPath = path.join(dataDirectory, folderName);
|
|
|
|
fs.access(folderPath, fs.constants.F_OK, (err) => {
|
|
if (err) {
|
|
return res.status(404).json({ error: `${req.query.domain} doesn't exist, don't create certificate` });
|
|
}
|
|
|
|
return res.status(200).json({ message: `${req.query.domain} exists, create certificate` });
|
|
});
|
|
});
|
|
|
|
app.listen(port, address, () => {
|
|
console.log(`Server is running at http://${address}:${port}`);
|
|
});
|