From 65f1c52581dd83dd45054801d1fa4ab306af8aeb Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Tue, 28 Jul 2020 15:35:50 +0200 Subject: [PATCH] Read config values from env --- .drone.yml | 2 +- registry/.gitignore | 1 + registry/src/config.ts | 44 +++++++++++++++++++++++++++++++++++++--- registry/src/registry.ts | 16 ++++----------- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/.drone.yml b/.drone.yml index 2aee469..d86b356 100644 --- a/.drone.yml +++ b/.drone.yml @@ -15,4 +15,4 @@ steps: repo: hibas123.azurecr.io/denreg registry: hibas123.azurecr.io dockerfile: registry/Dockerfile - debug: true + debug: false diff --git a/registry/.gitignore b/registry/.gitignore index 3fec32c..60cd341 100644 --- a/registry/.gitignore +++ b/registry/.gitignore @@ -1 +1,2 @@ tmp/ +data/ diff --git a/registry/src/config.ts b/registry/src/config.ts index 9a9c8c2..2a9da21 100644 --- a/registry/src/config.ts +++ b/registry/src/config.ts @@ -2,9 +2,47 @@ import { Ini } from "./deps.ts"; const config = Ini.decode( - await Deno.readFile("./data/config.ini").then((e) => - new TextDecoder().decode(e) - ) + await Deno.readFile("./data/config.ini") + .then((e) => new TextDecoder().decode(e)) + .catch((err) => { + if (!(err instanceof Deno.errors.NotFound)) { + throw err; + } + + return ""; + }) ) || {}; +const env = Deno.env.toObject(); + +for (const key in env) { + if (key.startsWith("DENREG_")) { + const stripped = key.slice(7); + if (stripped.startsWith("USER_")) { + const username = stripped.slice(5); + const password = env[key]; + + config.user = config.user || {}; + config.user[username] = { password }; + } else { + switch (stripped) { + case "S3_ENDPOINT": + config.s3 = { ...(config.s3 || {}), endpoint: env[key] }; + break; + case "S3_BUCKET": + config.s3 = { ...(config.s3 || {}), bucket: env[key] }; + break; + case "S3_ACCESS": + config.s3 = { ...(config.s3 || {}), accessKey: env[key] }; + break; + case "S3_SECRET": + config.s3 = { ...(config.s3 || {}), secretKey: env[key] }; + break; + } + } + } +} + +console.log("Known users:", Object.keys(config.user)); + export default config; diff --git a/registry/src/registry.ts b/registry/src/registry.ts index 7b5347d..3141458 100644 --- a/registry/src/registry.ts +++ b/registry/src/registry.ts @@ -1,18 +1,10 @@ -const ensureDir = async (name: string) => { - if ( - !(await Deno.stat(name) - .then(() => true) - .catch(() => false)) - ) { - await Deno.mkdir(name); - } -}; +import { FS } from "./deps.ts"; try { - await Deno.remove("./tmp", { recursive: true }); + Deno.removeSync("./tmp", { recursive: true }); } catch (err) {} -await ensureDir("./tmp"); -await ensureDir("./data"); +FS.ensureDirSync("./tmp"); +FS.ensureDirSync("./data"); import "./http.ts";