Skip to content

Commit

Permalink
create a new route: GET /admin/config. add data about currently runni…
Browse files Browse the repository at this point in the history
…ng build
  • Loading branch information
sdumetz committed Oct 18, 2024
1 parent c7b0141 commit 1ad4146
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@

ARG PORT
ARG PUBLIC
ARG BUILD_REF

###################
# Build source
###################
FROM node:16-alpine as build
RUN mkdir -p /app/dist /app/source
WORKDIR /app
Expand Down Expand Up @@ -31,14 +38,15 @@ RUN npm run build-ui
# The actual container to be published
###################
FROM node:16-alpine
LABEL org.opencontainers.image.source=https://github.com/Holusion/e-thesaurus
LABEL org.opencontainers.image.source=https://github.com/Holusion/eCorpus
LABEL org.opencontainers.image.description="eCorpus base image"
LABEL org.opencontainers.image.licenses=Apache

ARG PORT=8000

ENV PUBLIC=false
ENV PORT=${PORT}
ENV PUBLIC=${PUBLIC:-false}
ENV PORT=${PORT:-8000}
ENV BUILD_REF=${BUILD_REF:-unknown}

ENV NODE_ENV=production

WORKDIR /app
Expand Down
56 changes: 56 additions & 0 deletions source/server/routes/admin/config/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { randomBytes } from "crypto";
import request from "supertest";
import User from "../../../auth/User.js";
import UserManager from "../../../auth/UserManager.js";
import Vfs from "../../../vfs/index.js";



/**
* Minimal tests as most
*/

describe("GET /admin/config", function(){
let vfs :Vfs, userManager :UserManager, user :User, admin :User;

this.beforeAll(async function(){
let locals = await createIntegrationContext(this);
vfs = locals.vfs;
userManager = locals.userManager;
user = await userManager.addUser("bob", "12345678");
admin = await userManager.addUser("alice", "12345678", true);
});

this.afterAll(async function(){
await cleanIntegrationContext(this);
});

it("requires admin access", async function(){
//Anonymous
await request(this.server).get(`/admin/config`)
.expect(401);
//read-only User
await request(this.server).get(`/admin/config`)
.auth(user.username, "12345678")
.expect(401);

await request(this.server).get(`/admin/config`)
.auth(admin.username, "12345678")
.expect(200);
});

it("can return text/plain env file", async function(){
await request(this.server).get(`/admin/config`)
.auth(admin.username, "12345678")
.accept("text/plain")
.expect(200)
.expect("Content-Type", "text/plain; charset=utf-8");
});
it("can return application/json", async function(){
await request(this.server).get(`/admin/config`)
.auth(admin.username, "12345678")
.accept("application/json")
.expect(200)
.expect("Content-Type", "application/json; charset=utf-8");
});
});
19 changes: 19 additions & 0 deletions source/server/routes/admin/config/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request, Response } from "express";
import { getLocals, getVfs } from "../../../utils/locals.js";



export default async function handleGetConfig(req :Request, res :Response){
let {config} = getLocals(req);
res.status(200)
res.format({
"application/json": ()=>{
res.send(config);
},
"text/plain": ()=>{
res.send(Object.entries(config).map(([key, value])=>{
return `${key.toUpperCase()}="${value}"`
}).join("\n"));
}
})
}
2 changes: 2 additions & 0 deletions source/server/routes/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isAdministrator } from "../../utils/locals.js";
import wrap from "../../utils/wrapAsync.js";
import handleGetStats from "./stats/index.js";
import handleMailtest from "./mailtest.js";
import handleGetConfig from "./config/get.js";


const router = Router();
Expand All @@ -21,6 +22,7 @@ router.use((req, res, next)=>{


router.get("/stats", isAdministrator, wrap(handleGetStats));
router.get("/config", isAdministrator, wrap(handleGetConfig));
router.post("/mailtest", isAdministrator, wrap(handleMailtest));

export default router;
1 change: 1 addition & 0 deletions source/server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const values = {
hot_reload:[false, toBool],
smart_host: ["smtp://localhost", toString],
verbose: [false, toBool],
build_ref: ["unknown", toString],

experimental: [false, toBool],
/// FEATURE FLAGS ///
Expand Down

0 comments on commit 1ad4146

Please sign in to comment.