Skip to content

Commit

Permalink
feat: install via env
Browse files Browse the repository at this point in the history
  • Loading branch information
YuJianghao committed Apr 1, 2023
1 parent e535344 commit f0b91aa
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
USERNAME=
PASSWORD=

HEXON_PORT=

HEXO_BASE=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ stats.html
data
log
.DS_Store
*.local
*.local
.env
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 26 additions & 12 deletions server/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -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>(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>(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>(EnvService)
await env.sync()
server.listen(storage.get(HEXON_PORT_KEY) || HEXON_DEFAULT_PORT)
})()
2 changes: 1 addition & 1 deletion server/src/server/services/auth-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AuthStorageService {
secret = "secret",
expiresIn = "1h",
refreshableIn = "7d",
} = this._storage.get(AuthStorageService.KEY) || {}
} = this._storage.get<IAuthInfo>(AuthStorageService.KEY) || {}
return { secret, expiresIn, refreshableIn }
}

Expand Down
3 changes: 1 addition & 2 deletions server/src/shared/account-storage-service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,7 +24,7 @@ export class AccountService {
}
private _fromStorage(): IUserInfo {
const { username = "", password = "" } =
this._storage.get(AccountService.KEY) || {}
this._storage.get<IUserInfo>(AccountService.KEY) || {}
return { username, password }
}
setUserInfo(username: string, password: string) {
Expand Down
40 changes: 40 additions & 0 deletions server/src/shared/env-service.ts
Original file line number Diff line number Diff line change
@@ -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 })
}
}

0 comments on commit f0b91aa

Please sign in to comment.