Add config file and text editor + update server
This commit is contained in:
69
config-server/server.js
Normal file
69
config-server/server.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const express = require('express')
|
||||
const fs = require('fs/promises')
|
||||
const path = require('path')
|
||||
const yaml = require('js-yaml')
|
||||
const cors = require('cors')
|
||||
const bodyParser = require('body-parser')
|
||||
|
||||
const app = express()
|
||||
const PORT = process.env.CONFIG_PORT || 3012
|
||||
|
||||
// CORS configuration
|
||||
app.use(cors({
|
||||
origin: ['https://start.kent.pw', 'https://auth.kent.pw'],
|
||||
credentials: true,
|
||||
methods: ['GET', 'POST', 'OPTIONS'],
|
||||
allowedHeaders: [
|
||||
'Content-Type',
|
||||
'Authorization',
|
||||
'Remote-User',
|
||||
'Remote-Name',
|
||||
'Remote-Email',
|
||||
'Remote-Groups',
|
||||
'X-Requested-With'
|
||||
],
|
||||
exposedHeaders: ['Set-Cookie']
|
||||
}))
|
||||
|
||||
// Options preflight
|
||||
app.options('/api/config/update', cors())
|
||||
|
||||
// Raw body parser for YAML
|
||||
app.use(bodyParser.text({ type: 'text/yaml' }))
|
||||
|
||||
// Authentication middleware
|
||||
const requireAuth = (req, res, next) => {
|
||||
const user = req.headers['remote-user']
|
||||
if (!user) {
|
||||
return res.status(401).json({ error: 'Unauthorized' })
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
||||
app.post('/api/config/update', requireAuth, async (req, res) => {
|
||||
try {
|
||||
// Get the raw YAML content from the request body
|
||||
const yamlContent = req.body
|
||||
|
||||
// Validate the YAML by trying to parse it
|
||||
yaml.load(yamlContent)
|
||||
|
||||
// Write to the config file
|
||||
const configPath = path.join('/var/www/html/homepage/public', 'config.yaml')
|
||||
await fs.writeFile(configPath, yamlContent, 'utf8')
|
||||
|
||||
res.status(200).json({ message: 'Configuration updated successfully' })
|
||||
} catch (error) {
|
||||
console.error('Error updating configuration:', error)
|
||||
res.status(500).json({ error: 'Failed to update configuration: ' + error.message })
|
||||
}
|
||||
})
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/health', (req, res) => {
|
||||
res.status(200).json({ status: 'ok' })
|
||||
})
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Config server listening on port ${PORT}`)
|
||||
})
|
||||
Reference in New Issue
Block a user