From f0b91aa39d4f7ab8f050f4ed8c470843299c5bc2 Mon Sep 17 00:00:00 2001 From: winwin2011 Date: Sat, 1 Apr 2023 22:59:16 +0800 Subject: [PATCH] feat: install via env --- .env.sample | 6 +++ .gitignore | 3 +- server/package.json | 1 + server/pnpm-lock.yaml | 7 ++++ server/src/server/index.ts | 38 ++++++++++++------ .../server/services/auth-storage-service.ts | 2 +- server/src/shared/account-storage-service.ts | 3 +- server/src/shared/env-service.ts | 40 +++++++++++++++++++ 8 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 .env.sample create mode 100644 server/src/shared/env-service.ts diff --git a/.env.sample b/.env.sample new file mode 100644 index 00000000..2acd5613 --- /dev/null +++ b/.env.sample @@ -0,0 +1,6 @@ +USERNAME= +PASSWORD= + +HEXON_PORT= + +HEXO_BASE= diff --git a/.gitignore b/.gitignore index 898ecfec..3bad268a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ stats.html data log .DS_Store -*.local \ No newline at end of file +*.local +.env diff --git a/server/package.json b/server/package.json index 7df79552..55cd33cf 100644 --- a/server/package.json +++ b/server/package.json @@ -55,6 +55,7 @@ "crypto-js": "^4.0.0", "dayjs": "^1.11.0", "debug": "^4.3.4", + "dotenv": "^16.0.3", "execa": "^6.1.0", "hexo": "^5.4.2", "http-errors": "^2.0.0", diff --git a/server/pnpm-lock.yaml b/server/pnpm-lock.yaml index a160b36d..2d2fa670 100644 --- a/server/pnpm-lock.yaml +++ b/server/pnpm-lock.yaml @@ -31,6 +31,7 @@ specifiers: crypto-js: ^4.0.0 dayjs: ^1.11.0 debug: ^4.3.4 + dotenv: ^16.0.3 esbuild: ^0.14.34 esbuild-plugin-node-externals: ^0.3.0 eslint: ^8.13.0 @@ -78,6 +79,7 @@ dependencies: crypto-js: 4.1.1 dayjs: 1.11.1 debug: 4.3.4 + dotenv: 16.0.3 execa: 6.1.0 hexo: 5.4.2 http-errors: 2.0.0 @@ -1957,6 +1959,11 @@ packages: is-obj: 2.0.0 dev: true + /dotenv/16.0.3: + resolution: {integrity: sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==} + engines: {node: '>=12'} + dev: false + /duplexer3/0.1.4: resolution: {integrity: sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=} dev: true diff --git a/server/src/server/index.ts b/server/src/server/index.ts index 69858425..ab23838c 100644 --- a/server/src/server/index.ts +++ b/server/src/server/index.ts @@ -1,19 +1,33 @@ import "reflect-metadata" +import * as dotenv from "dotenv" import { container } from "tsyringe" import http from "http" import { HEXON_DEFAULT_PORT, HEXON_PORT_KEY } from "~/shared/constants" import { StorageService } from "~/shared/storage-service" import { HexoInstanceService } from "@/services/hexo-instance-service" import app from "@/app" - -const storage = container.resolve(StorageService) -const server = http.createServer(app.callback()) -server.on("listening", () => { - const addr = server.address() - const bind = - typeof addr === "string" ? "pipe " + addr : "http://localhost:" + addr.port - console.log("Server running on " + bind) - const his = container.resolve(HexoInstanceService) - his.init().catch(console.error) -}) -server.listen(storage.get(HEXON_PORT_KEY) || HEXON_DEFAULT_PORT) +import path from "path" +import { EnvService } from "~/shared/env-service" +;(async () => { + dotenv.config({ + path: + process.env.NODE_ENV === "production" + ? process.cwd() + "/.env" + : path.resolve(process.cwd(), "../.env"), + }) + const storage = container.resolve(StorageService) + const server = http.createServer(app.callback()) + server.on("listening", () => { + const addr = server.address() + const bind = + typeof addr === "string" + ? "pipe " + addr + : "http://localhost:" + addr!.port + console.log("Server running on " + bind) + const his = container.resolve(HexoInstanceService) + his.init().catch(console.error) + }) + const env = container.resolve(EnvService) + await env.sync() + server.listen(storage.get(HEXON_PORT_KEY) || HEXON_DEFAULT_PORT) +})() diff --git a/server/src/server/services/auth-storage-service.ts b/server/src/server/services/auth-storage-service.ts index 0924937b..2068c0cc 100644 --- a/server/src/server/services/auth-storage-service.ts +++ b/server/src/server/services/auth-storage-service.ts @@ -28,7 +28,7 @@ export class AuthStorageService { secret = "secret", expiresIn = "1h", refreshableIn = "7d", - } = this._storage.get(AuthStorageService.KEY) || {} + } = this._storage.get(AuthStorageService.KEY) || {} return { secret, expiresIn, refreshableIn } } diff --git a/server/src/shared/account-storage-service.ts b/server/src/shared/account-storage-service.ts index 149b4acf..44ad2dec 100644 --- a/server/src/shared/account-storage-service.ts +++ b/server/src/shared/account-storage-service.ts @@ -1,7 +1,6 @@ import { SHA1 } from "crypto-js" import { inject, injectable, singleton } from "tsyringe" import { StorageService } from "~/shared/storage-service" -import { Unauthorized } from "http-errors" interface IUserInfo { password: string @@ -25,7 +24,7 @@ export class AccountService { } private _fromStorage(): IUserInfo { const { username = "", password = "" } = - this._storage.get(AccountService.KEY) || {} + this._storage.get(AccountService.KEY) || {} return { username, password } } setUserInfo(username: string, password: string) { diff --git a/server/src/shared/env-service.ts b/server/src/shared/env-service.ts new file mode 100644 index 00000000..392c8695 --- /dev/null +++ b/server/src/shared/env-service.ts @@ -0,0 +1,40 @@ +import { inject, injectable, singleton } from "tsyringe" +import { AccountService } from "./account-storage-service" +import { StorageService } from "./storage-service" +import { HEXON_PORT_KEY } from "./constants" +import { HexoInstanceService } from "~/server/services/hexo-instance-service" + +@injectable() +@singleton() +export class EnvService { + constructor( + @inject(StorageService) private storage: StorageService, + @inject(AccountService) private account: AccountService + ) {} + + sync() { + this.syncAccount() + this.syncHexon() + this.syncHexo() + } + + private syncAccount() { + const username = process.env.USERNAME + const password = process.env.PASSWORD + username && this.account.setUsername(username) + password && this.account.setPassword(password) + console.log({ username, password }) + } + + private syncHexon() { + const port = process.env.HEXON_PORT + port && this.storage.set(HEXON_PORT_KEY, port) + console.log({ port }) + } + + private syncHexo() { + const base = process.env.HEXO_BASE + base && this.storage.set(HexoInstanceService.HEXO_BASE_DIR_KEY, base) + console.log({ base }) + } +}