Skip to content

Commit 12b7127

Browse files
committedOct 12, 2023
new database methods
1 parent d58a77d commit 12b7127

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed
 

‎api_server/src/Database.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {Pool, ResultSetHeader, RowDataPacket} from "mysql2";
1+
import {FieldPacket, Pool, ResultSetHeader, RowDataPacket} from "mysql2";
22
import {Util} from "./Util";
33
import {CONFIG} from "./config";
4+
import {IChannel} from "./types";
45

56
const mysql = require('mysql2');
67

@@ -44,8 +45,12 @@ export class Database {
4445
}
4546

4647
// contains affectedRows and lastInsertId properties.
47-
public async update() {
48-
// TODO
48+
public async insertGetId(sql: string, parameters?: unknown[]): Promise<number> {
49+
50+
const [result, fields]: [ResultSetHeader, FieldPacket[]] = await this.pool.promise()
51+
.query<ResultSetHeader>(sql, parameters);
52+
53+
return result.insertId;
4954
}
5055

5156
async ping(): Promise<boolean> {
@@ -61,22 +66,38 @@ export class Database {
6166
return rows.length && rows[0]["cnt"] == 0;
6267
}
6368

64-
async createChannel(name: string): Promise<boolean> {
69+
async createChannel(name: string, ipAddress: string): Promise<boolean> {
6570

6671
if (!await this.checkIfChannelNameAvailable(name)) {
6772
throw "Channel with name '" + name + "' is not available";
6873
}
6974

70-
const sk = Util.generateStreamKey();
75+
const streamKey: string = Util.generateStreamKey();
76+
const authKey: string = Util.randomString(32, 'ak_');
7177

72-
const result = await this.pool.promise()
73-
.execute<ResultSetHeader>("INSERT INTO channels (created_at, name, stream_key) VALUES (UTC_TIMESTAMP(), ?, ?)", [
74-
name, sk
75-
]);
78+
const result = await this.insertGetId(
79+
"INSERT INTO channels (created_at, ip_address, name, auth_key, stream_key) VALUES (UTC_TIMESTAMP(), ?, ?, ?, ?)",
80+
[ipAddress, name, authKey, streamKey]
81+
);
7682

7783
return true;
7884
}
7985

86+
async findChannelById(id: number): Promise<IChannel> {
87+
const result: IChannel[] = await this.select<IChannel>("SELECT * FROM channels WHERE id = ?", [id]);
88+
return result.length ? result[0] : null;
89+
}
90+
91+
async findChannelByAuthToken(token: string): Promise<IChannel | null> {
92+
const result = await this.select<IChannel>("SELECT * FROM channels WHERE stream_key = ? LIMIT 1", [token]);
93+
return result.length ? result[0] : null;
94+
}
95+
96+
async findChannelByName(name: string): Promise<IChannel> {
97+
const result: IChannel[] = await this.select<IChannel>("SELECT * FROM channels WHERE name = ? LIMIT 1", [name]);
98+
return result.length ? result[0] : null;
99+
}
100+
80101
async createNewStream(name: string, ipAddress: string, streamInfo: string): Promise<void> {
81102
await this.pool.promise().execute("INSERT INTO streams (name, user_ip, started_at, ffprobe_json) VALUES (?, ?, UTC_TIMESTAMP(), ?)", [
82103
name, ipAddress, streamInfo

‎api_server/src/classes/Server.ts

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ export class Server {
3333
this.app.use(this.router);
3434
this.app.use("/", routes);
3535

36+
this.app.use((req: Request, res: Response, next) => {
37+
38+
const path = req.path;
39+
40+
res.status(404).json({
41+
error: `${path} not found`
42+
});
43+
});
44+
3645
// must be registered AFTER all routes
3746
this.app.use((error: Error, req: Request, res: Response, next: any) => {
3847

‎api_server/src/config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {ConnectionOptions} from "mysql2";
22
import {URL} from "url";
33

4+
require('dotenv').config();
5+
46
const urlFromEnv: URL | null = (() => {
57

68
try {
@@ -12,7 +14,7 @@ const urlFromEnv: URL | null = (() => {
1214
})();
1315

1416
const connectionOptions: ConnectionOptions = {
15-
host: urlFromEnv?.hostname || 'db',
17+
host: process.env.DB_HOST || urlFromEnv?.hostname || 'localhost',
1618
port: +(urlFromEnv?.port || 3306),
1719
user: urlFromEnv?.username || 'root',
1820
password: urlFromEnv?.password || 'password',

‎docker-compose.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ services:
3333
- ./public/storage:/var/storage
3434
environment:
3535
- NODE_ENV=production
36-
# - MYSQL_CONNECTION=mysql://root:password@db:3306
36+
- DB_HOST=db
37+
# - MYSQL_CONNECTION=mysql://root:password@db:3306
3738
- RTMP_SERVER=rtmp
3839

3940
adminer:

0 commit comments

Comments
 (0)