Commit 761c1ee6 authored by Lakr Aream's avatar Lakr Aream 👼🏽

首次连接数据库

parent 98cb715d
......@@ -152,6 +152,7 @@ echo $signStr;
- Located At: users/c
- Actions: create, delete, modify.element, list, info
- User type: root(unique), admin, developer, user
<details>
<summary>Create User</summary>
......
......@@ -15,8 +15,8 @@ app:
# Database Config For Backend Application
database:
host: "db" # database host name
host: "127.0.0.1" # database host name
port: 3306 # database port
user: "root" # database user name
table: "Candiay" # database table name
pass: "6FAE553E-20AF-49FB-8C34-B76509CABA44" # database password for your user
\ No newline at end of file
base: "Candiay" # database name
pass: "00000000" # database password for your user
\ No newline at end of file
......@@ -113,6 +113,11 @@
"@types/mime": "*"
}
},
"@types/yaml": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@types/yaml/-/yaml-1.2.0.tgz",
"integrity": "sha512-GW8b9qM+ebgW3/zjzPm0I1NxMvLaz/YKT9Ph6tTb+Fkeyzd9yLTvQ6ciQ2MorTRmb/qXmfjMerRpG4LviixaqQ=="
},
"accepts": {
"version": "1.3.7",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
......
......@@ -4,6 +4,7 @@
"description": "Candiay Backend",
"main": "dist/main.js",
"dependencies": {
"@types/yaml": "^1.2.0",
"express": "^4.17.1",
"mysql": "^2.18.1",
"yaml": "^1.8.3"
......@@ -15,7 +16,7 @@
},
"scripts": {
"build": "tsc",
"start": "cp -f ./config/config.yml /etc/Candiay/coreBackend/config/config.yml && tsc && node dist/main.js",
"start": "tsc && node dist/main.js",
"docker": "cd docker && ./start.sh"
},
"repository": {
......
import yaml from "yaml"
import fs from "fs"
export class ConfigManager {
static shared = new ConfigManager()
public location: string
public object: any
private verison: number
private level: number
public portToBind: number
public rootTicket: string | null
public accpetAddr: string[]
public requireSign: boolean
constructor() {
console.log("ConfigManager constructor");
this.location = "/etc/Candiay/coreBackend/config/config.yml";
this.object = yaml.parse(fs.readFileSync(this.location).toString());
this.portToBind = this.object.app.port;
this.rootTicket = this.object.app.root;
if (this.rootTicket === "00000") {
this.rootTicket = null;
}
this.accpetAddr = this.object.app.accpet;
this.requireSign = this.object.app.use_development_ticket;
}
healthCheck() {
console.log("ConfigManager dummy init");
console.log(this.object);
}
}
\ No newline at end of file
import mysql from "mysql"
import { ConfigManager } from "./config"
export class DataBase {
static shared = new DataBase()
private host: string
private port: number
private user: string
private pass: string
private base: string // which database to use
private url: string
private connection: any
constructor() {
console.log("DataBase constructor");
const object = ConfigManager.shared.object;
this.host = object.database.host;
this.port = object.database.port;
this.user = object.database.user;
this.pass = object.database.pass;
this.base = object.database.base;
const uuu = String(this.host + ":" + String(this.port));
this.url = uuu;
this.connection = mysql.createConnection({
host : this.host,
user : this.user,
password : this.pass,
database : this.base,
});
}
healthCheck() {
console.log("ConfigManager dummy init");
console.log("DataBase Inf - host " + this.host + "");
console.log("DataBase Inf - port " + this.port + "");
console.log("DataBase Inf - user " + this.user + "");
console.log("DataBase Inf - pass " + this.pass + "");
console.log("DataBase Inf - base " + this.base + "");
console.log("DataBase Inf - url " + this.url + "");
}
connect() {
this.connection.connect()
}
}
\ No newline at end of file
import express from "express"
import yaml from "yaml"
import mysql from "mysql"
import fs from "fs"
import { ConfigManager } from "./config"
import { DataBase } from "./database"
const coreApp = express()
let port = 3030;
let dbUser = "root";
let dbTable = "Candiay";
let dbPass = "";
let dbHost = "127.0.0.1";
let dbPort = 3306;
// Load config form /etc/Candiay/coreBackend/config/config.yml
const config = yaml.parse(fs.readFileSync("/etc/Candiay/coreBackend/config/config.yml").toString());
console.log(config)
port = config.app.port
dbUser = config.database.user
dbTable = config.database.table
dbPass = config.database.pass
dbHost = config.database.host
dbPort = config.database.port
ConfigManager.shared.healthCheck()
DataBase.shared.healthCheck()
DataBase.shared.connect()
const dbUrl = String(dbHost + ":" + String(dbPort));
console.log(dbUrl);
const databaseConnection = mysql.createConnection({
host : dbUrl,
user : dbUser,
password : dbPass,
database : dbTable,
});
databaseConnection.connect()
const coreApp = express()
const port = ConfigManager.shared.portToBind
// Register enterpoints
coreApp.get("/", (req, res) => {
res.send("Candiay Core");
})
// Start servers
coreApp.listen(port, err => {
if (err) {
return console.error(err);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment