Skip to content

Commit

Permalink
[optimize] auto load Environment variables
Browse files Browse the repository at this point in the history
[fix] 4 Database detail bugs
  • Loading branch information
TechQuery committed Aug 15, 2024
1 parent 5bda9ff commit 0ff03ae
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ node_modules/
package-lock.json
dist/
type/*.d.ts
.env
.env*
.data/
.vscode/settings.json
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

| Name | Usage |
| :------------: | :--------------------------: |
| `APP_SECRET` | encrypt Password & Token |
| `JWT_SECRET` | encrypt Password & Token |
| `DATABASE_URL` | PostgreSQL connection string |

## Development
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=${APP_SECRET}
- POSTGRES_PASSWORD=${JWT_SECRET}
volumes:
- database-data:/var/lib/postgresql/data/
networks:
Expand All @@ -21,7 +21,7 @@ services:
image: kaiyuanshe/openhackathon-service
environment:
- NODE_ENV=production
- DATABASE_URL=postgres://postgres:${APP_SECRET}@postgres:5432/postgres
- DATABASE_URL=postgres://postgres:${JWT_SECRET}@postgres:5432/postgres
- PORT=8080
networks:
- kaiyuanshe
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@koa/cors": "^5.0.0",
"@koa/multer": "^3.0.2",
"@koa/router": "^12.0.1",
"@koa/router": "^13.0.0",
"@octokit/openapi-types": "^22.2.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down Expand Up @@ -54,6 +54,7 @@
"@typescript-eslint/parser": "^8.1.0",
"eslint": "^8.57.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"get-git-folder": "^0.1.2",
"husky": "^9.1.4",
"lint-staged": "^15.2.9",
"prettier": "^3.3.3",
Expand All @@ -72,6 +73,7 @@
},
"scripts": {
"prepare": "husky || true",
"install": "get-git-folder https://github.com/kaiyuanshe/service-configuration main OpenHackathon-Web || true",
"dev": "ts-node-dev source/",
"test": "lint-staged",
"build": "rm -rf dist/ type/*.d.ts && tsc && mv dist/model/*.d.ts type/",
Expand Down
363 changes: 351 additions & 12 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions source/controller/OAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export class OauthController {
const response = await fetch('https://api.github.com/user', {
headers: { Authorization: `Bearer ${accessToken}` }
});
const { email, name, avatar_url } =
const { email, login, avatar_url } =
(await response.json()) as GitHubUser;
const user =
(await store.findOneBy({ email })) ||
(await UserController.signUp({ email, password: accessToken }));
const newProfile = { name, avatar_url },
const newProfile = { name: login, avatar: avatar_url },
oldPofile = { name: user.name, avatar: user.avatar };

if (!isDeepStrictEqual(oldPofile, newProfile)) {
Expand Down
20 changes: 10 additions & 10 deletions source/controller/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
UserFilter,
UserListChunk
} from '../model';
import { APP_SECRET, searchConditionOf } from '../utility';
import { JWT_SECRET, searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';

const store = dataSource.getRepository(User);
Expand All @@ -36,23 +36,23 @@ const store = dataSource.getRepository(User);
export class UserController {
static encrypt = (raw: string) =>
createHash('sha1')
.update(APP_SECRET + raw)
.update(JWT_SECRET + raw)
.digest('hex');

static sign = (user: User): User => ({
...user,
token: sign({ ...user }, APP_SECRET)
token: sign({ ...user }, JWT_SECRET)
});

static async signUp(data: SignInData) {
static async signUp({ email, password }: SignInData) {
const sum = await store.count();

const { password, ...user } = await store.save(
Object.assign(new User(), data, {
password: UserController.encrypt(data.password),
roles: [sum ? Role.Client : Role.Administrator]
})
);
const { password: _, ...user } = await store.save({
name: email,
email,
password: UserController.encrypt(password),
roles: [sum ? Role.Client : Role.Administrator]
});
await ActivityLogController.logCreate(user, 'User', user.id);

return user;
Expand Down
4 changes: 2 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
UserController
} from './controller';
import { dataSource } from './model';
import { APP_SECRET, isProduct, PORT } from './utility';
import { JWT_SECRET, isProduct, PORT } from './utility';

const HOST = `localhost:${PORT}`,
app = new Koa()
.use(KoaLogger())
.use(swagger({ exposeSpec: true }))
.use(jwt({ secret: APP_SECRET, passthrough: true }));
.use(jwt({ secret: JWT_SECRET, passthrough: true }));

if (!isProduct) app.use(mocker());

Expand Down
2 changes: 1 addition & 1 deletion source/model/Hackathon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Hackathon extends UserBase {
@Min(0)
@VirtualColumn({
query: alias =>
`SELECT COUNT(*) FROM "Enrollment" WHERE hackathonId = ${alias}.id`
`SELECT COUNT(*) FROM "enrollment" WHERE "enrollment"."hackathonId" = ${alias}.id`
})
enrollment: number;

Expand Down
3 changes: 2 additions & 1 deletion source/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionO
import { DATABASE_URL, isProduct } from '../utility';
import { User } from './User';
import { PlatformAdmin } from './PlatformAdmin';
import { ActivityLog } from './ActivityLog';
import { ActivityLog, UserRank } from './ActivityLog';
import { Hackathon } from './Hackathon';
import { Staff } from './Staff';
import { Organizer } from './Organizer';
Expand Down Expand Up @@ -35,6 +35,7 @@ const commonOptions: Pick<
User,
PlatformAdmin,
ActivityLog,
UserRank,
Hackathon,
Staff,
Organizer,
Expand Down
2 changes: 1 addition & 1 deletion source/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FindOptionsWhere, Like } from 'typeorm';

import { Base } from './model';

export const { NODE_ENV, PORT = 8080, DATABASE_URL, APP_SECRET } = process.env;
export const { NODE_ENV, PORT = 8080, DATABASE_URL, JWT_SECRET } = process.env;

export const isProduct = NODE_ENV === 'production';

Expand Down

0 comments on commit 0ff03ae

Please sign in to comment.