Skip to content

Commit

Permalink
fix(web): ohbug.config 加入 url 配置
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyao27 committed Jul 19, 2022
1 parent 92701ca commit d5cc2e2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 31 deletions.
10 changes: 0 additions & 10 deletions docker-compose.prod.yml → docker-compose.development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,11 @@ services:
- POSTGRES_USER=ohbug
- POSTGRES_PASSWORD=ohbug
- POSTGRES_DB=ohbug
hostname: postgres
ports:
- '5432:5432'

redis:
image: redis:alpine
restart: always
hostname: redis
ports:
- '6379:6379'

app:
image: ohbug/ohbug-ce:latest
ports:
- '3000:3000'
- '6660:6660'
volumes:
- ./ohbug.config.yml:/app/ohbug.config.yml
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ services:
- POSTGRES_USER=ohbug
- POSTGRES_PASSWORD=ohbug
- POSTGRES_DB=ohbug
hostname: postgres
ports:
- '5432:5432'

redis:
image: redis:alpine
restart: always
hostname: redis
ports:
- '6379:6379'

app:
image: ohbug/ohbug-ce:latest
ports:
- '3000:3000'
- '6660:6660'
volumes:
- ./ohbug.config.yml:/app/ohbug.config.yml
18 changes: 18 additions & 0 deletions ohbug.config.development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
http:
url: http://localhost:3000

db:
postgres:
host: localhost
port: 5432
database: ohbug
user: ohbug
password: ohbug

redis:
host: localhost
port: 6379

secret:
apikey: ohbug-apikey-s3cret
session: ohbug-session-s3cret
6 changes: 4 additions & 2 deletions ohbug.config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
http:
url: http://localhost:3000

db:
postgres:
host: postgres
Expand All @@ -11,6 +14,5 @@ db:
port: 6379

secret:
session: super-duper-s3cret
apikey: ohbug-apikey-s3cret
nextauth: ohbug-nextauth-s3cret
session: ohbug-session-s3cret
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"update:deps": "pnpm update -i -r --latest",
"prepare": "husky install",
"prebuild": "tsx scripts/createEnv.ts",
"build": "pnpm -r run build",
"docker": "docker-compose up -d",
"dev": "pnpm run docker && pnpm run dev:setup && pnpm --parallel -r run dev",
"dev:web": "pnpm run docker && pnpm --parallel --filter web --filter web run dev",
"dev:server": "pnpm run docker && pnpm --parallel --filter server --filter config run dev",
"dev:setup": "pnpm run prebuild && npx prisma migrate dev && npx prisma db seed",
"start": "pnpm run setup && pnpm --parallel -r run start",
"setup": "pnpm run prebuild && npx prisma migrate deploy && npx prisma db seed",
"build": "cross-env NODE_ENV=production pnpm -r run build",
"docker": "docker-compose -f ./docker-compose.development.yml up -d",
"dev": "cross-env NODE_ENV=development pnpm run docker && pnpm run dev:setup && pnpm --parallel -r run dev",
"dev:web": "cross-env NODE_ENV=development pnpm run docker && pnpm --parallel --filter web --filter web run dev",
"dev:server": "cross-env NODE_ENV=development pnpm run docker && pnpm --parallel --filter server --filter config run dev",
"dev:setup": "cross-env NODE_ENV=development pnpm run prebuild && npx prisma migrate dev && npx prisma db seed",
"start": "cross-env NODE_ENV=production pnpm run setup && pnpm --parallel -r run start",
"setup": "cross-env NODE_ENV=production pnpm run prebuild && npx prisma migrate deploy && npx prisma db seed",
"studio": "npx prisma studio",
"lint": "eslint . --fix",
"prepublishOnly": "pnpm run build",
Expand Down
4 changes: 2 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"sideEffects": false,
"scripts": {
"prebuild": "rimraf dist",
"dev": "tsup src/index.ts --dts --format esm,cjs --watch",
"build": "tsup src/index.ts --dts --format esm,cjs"
"dev": "tsup src/index.ts --dts --format esm,cjs --watch --env.NODE_ENV development",
"build": "tsup src/index.ts --dts --format esm,cjs --env.NODE_ENV production"
}
}
4 changes: 2 additions & 2 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"sideEffects": false,
"scripts": {
"prebuild": "rimraf dist",
"dev": "tsup src/index.ts --dts --format esm,cjs --watch",
"build": "tsup src/index.ts --dts --format esm,cjs"
"dev": "tsup src/index.ts --dts --format esm,cjs --watch --env.NODE_ENV development",
"build": "tsup src/index.ts --dts --format esm,cjs --env.NODE_ENV production"
}
}
9 changes: 6 additions & 3 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { cwd } from 'node:process'
import * as yaml from 'js-yaml'

export interface Config {
http: {
url: string
}
db: {
postgres: {
host: string
Expand All @@ -19,14 +22,14 @@ export interface Config {
}
}
secret?: {
session?: string
apikey?: string
nextauth?: string
session?: string
}
}
const YAML_CONFIG_FILENAME = 'ohbug.config.yml'
const DEVELOP_YAML_CONFIG_FILENAME = 'ohbug.config.development.yml'
export function getConfig(): Config {
const configPath = join(cwd(), '../../', YAML_CONFIG_FILENAME)
const configPath = join(cwd(), '../../', process.env.NODE_ENV === 'development' ? DEVELOP_YAML_CONFIG_FILENAME : YAML_CONFIG_FILENAME)
const config = yaml.load(readFileSync(configPath, 'utf8')) as Config
const postgresUrl = `postgresql://${config.db.postgres.user}:${config.db.postgres.password}@${config.db.postgres.host}:${config.db.postgres.port}/${config.db.postgres.database}`
config.db.postgres.url = postgresUrl
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getPrisma } from '~/db'
export const getAuthOptions = async(): Promise<NextAuthOptions> => {
const setting = await serviceGetSetting()
const options: NextAuthOptions = {
secret: getConfig().secret?.nextauth ?? 'ohbug-nextauth-s3cret',
secret: getConfig().secret?.session ?? 'ohbug-session-s3cret',
adapter: PrismaAdapter(getPrisma()),
providers: [],
callbacks: {
Expand Down
10 changes: 7 additions & 3 deletions scripts/createEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import * as yaml from 'js-yaml'
import type { Config } from '../packages/config'

const YAML_CONFIG_FILENAME = 'ohbug.config.yml'
const DEVELOP_YAML_CONFIG_FILENAME = 'ohbug.config.development.yml'
const FILE_NAME = '.env'

async function main() {
const filePath = join(cwd(), FILE_NAME)
const configPath = join(cwd(), YAML_CONFIG_FILENAME)
const rootFilePath = join(cwd(), FILE_NAME)
const webFilePath = join(cwd(), 'packages/web', FILE_NAME)
const configPath = join(cwd(), process.env.NODE_ENV === 'development' ? DEVELOP_YAML_CONFIG_FILENAME : YAML_CONFIG_FILENAME)
const config = yaml.load(await readFile(configPath, 'utf8')) as Config

const fileContents = `DB_USER=${config.db.postgres.user}
DB_PASSWORD=${config.db.postgres.password}
DB_NAME=${config.db.postgres.database}
DATABASE_URL="postgresql://${config.db.postgres.user}:${config.db.postgres.password}@${config.db.postgres.host}:${config.db.postgres.port}/${config.db.postgres.database}"
NEXTAUTH_URL=${config.http.url}
`
await writeFile(filePath, fileContents)
await writeFile(rootFilePath, fileContents)
await writeFile(webFilePath, fileContents)
}

main()

0 comments on commit d5cc2e2

Please sign in to comment.