This commit is contained in:
parent
da7d75641d
commit
81f86acc7b
7 changed files with 148 additions and 18 deletions
|
|
@ -1,16 +1,19 @@
|
|||
import "reflect-metadata";
|
||||
import { SAMPLE } from "@repo/common/sample";
|
||||
import path from "path"
|
||||
import { fileURLToPath } from 'url';
|
||||
import express from "express";
|
||||
import crypto from "crypto";
|
||||
import fs from "fs";
|
||||
import { appDataSource } from "./config/dbconfig.js";
|
||||
import { Lemma, WordForm, Lect, Definition, Example } from "./db/dbmodel.js";
|
||||
import { Lemma, WordForm, Lect, Definition, Example, Role, User } from "./db/dbmodel.js";
|
||||
import "@total-typescript/ts-reset";
|
||||
import { Like, In } from "typeorm";
|
||||
import cors from "cors";
|
||||
|
||||
const RELOAD_SHEET_ON_START = false;
|
||||
const SOURCE_FILE = "res/sample.tsv";
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
appDataSource
|
||||
.initialize()
|
||||
|
|
@ -19,15 +22,86 @@ appDataSource
|
|||
|
||||
if (RELOAD_SHEET_ON_START) {
|
||||
await loadSheet();
|
||||
await ensureDefaultRoles();
|
||||
}
|
||||
})
|
||||
.catch((error) => console.log(error));
|
||||
|
||||
async function ensureDefaultRoles(){
|
||||
const ADMIN_NAME = "admin"
|
||||
const EDITOR_NAME = "editor"
|
||||
const READER_NAME = "reader"
|
||||
|
||||
let admin_role = new Role();
|
||||
admin_role.role_name = ADMIN_NAME;
|
||||
|
||||
let editor_role = new Role();
|
||||
editor_role.role_name = EDITOR_NAME;
|
||||
|
||||
let reader_role = new Role();
|
||||
reader_role.role_name = READER_NAME;
|
||||
|
||||
Role.save([admin_role, editor_role, reader_role]);
|
||||
|
||||
let admin_user: User;
|
||||
let editor_user: User;
|
||||
let reader_user: User;
|
||||
|
||||
await User.findOne({where:
|
||||
{user_name:ADMIN_NAME}
|
||||
}).then(async (u)=>{
|
||||
//if no admin user is detected, generate one with the admin role
|
||||
if (!u){
|
||||
admin_user = new User();
|
||||
admin_user.user_name = ADMIN_NAME;
|
||||
admin_user.roles = [admin_role];
|
||||
await admin_user
|
||||
.save()
|
||||
.then((u)=>{
|
||||
admin_user = u;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
await User.findOne({where:
|
||||
{user_name:EDITOR_NAME}
|
||||
}).then(async (u)=>{
|
||||
if (!u){
|
||||
editor_user = new User();
|
||||
editor_user.user_name = EDITOR_NAME;
|
||||
editor_user.roles = [editor_role];
|
||||
await editor_user
|
||||
.save()
|
||||
.then((u)=>{
|
||||
editor_user = u;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
await User.findOne({where:
|
||||
{user_name:READER_NAME}
|
||||
}).then(async (u)=>{
|
||||
if (!u){
|
||||
reader_user = new User();
|
||||
reader_user.user_name = READER_NAME;
|
||||
reader_user.roles = [reader_role];
|
||||
await reader_user
|
||||
.save()
|
||||
.then((u)=>{
|
||||
reader_user = u;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function initExpress() {
|
||||
const app = express();
|
||||
const PORT = 1225;
|
||||
|
||||
app.use(cors(), express.json());
|
||||
console.info(path.join(__dirname, '../../ktb-static'));
|
||||
|
||||
app.use(express.json(), express.static(path.join(__dirname, '../../ktb-static')));
|
||||
|
||||
app.get("/sample", (_req, res) => {
|
||||
res.status(200).send(SAMPLE);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue