Skip to content

Commit

Permalink
Merge branch 'release/v0.8.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Luphia committed Jul 20, 2022
2 parents eaa5790 + 1abd046 commit c6feb4e
Show file tree
Hide file tree
Showing 21 changed files with 361 additions and 550 deletions.
15 changes: 11 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#
# common
XLSFOLDER_DIR = /xls/
JSONFILE_DIR = /server/playlist.json

# google gmail api api config
GMAIL_CLIENT_SECRET = "test_client_secret"
GMAIL_CLIENT_ID = "test_client_id"
# google gmail api settings
GOOGLE_CLIENT_ID = "test_client_id"
GOOGLE_CLIENT_PASSWORD = "test_client_password"

# http and https config
HTTP_ENABLE = true
HTTPS_ENABLE = true

HTTP_PORT = 80
HTTPS_PORT = 443
306 changes: 38 additions & 268 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "isuntv-live",
"version": "0.8.0",
"version": "0.8.1",
"private": true,
"dependencies": {
"@nestjs/cli": "^8.2.6",
Expand All @@ -27,12 +27,12 @@
"nestjs-i18n": "^9.1.3",
"node-tika": "^0.0.1",
"nodemailer": "^6.7.6",
"pem": "^1.14.6",
"queue": "^6.0.2",
"react": "^18.1.0",
"react-cookie": "^4.1.1",
"react-dom": "^18.1.0",
"react-hls-player": "^3.0.7",
"react-i18next": "^11.18.0",
"react-player": "^2.10.1",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
Expand Down Expand Up @@ -77,7 +77,7 @@
"@typescript-eslint/parser": "^5.30.6",
"eslint": "^8.19.0",
"eslint-plugin-react": "^7.30.1",
"video.js": "^7.19.2",
"react-i18next": "^11.18.0"
"react-i18next": "^11.18.1",
"video.js": "^7.19.2"
}
}
2 changes: 1 addition & 1 deletion server/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AppModule implements NestModule {
* set the default constructor without param
*/
constructor() {

// nothing to do
}

//the function of getting current time
Expand Down
43 changes: 1 addition & 42 deletions server/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Injectable ,OnModuleInit} from '@nestjs/common';
import * as fs from 'fs';
import * as hound from 'hound';
import { Injectable } from '@nestjs/common';

// need to be modified
@Injectable()
Expand All @@ -16,45 +14,6 @@ class AppService {
this.Content=null;

}

dostuff(){
// Create a file watcher.
const watcher = hound.watch(this.xlsFolder)

// Add callbacks for file and directory events. The change event only applies
// to files.
watcher.on('create', function(file, stats) {

const data = this.chinasunService.getUpdatedData();
let jsonFile=process.cwd()+'/playlist.json';
fs.writeFile(jsonFile, JSON.stringify(data), {
encoding:"utf8",
flag:"w"
},
(err) => {

if (err) {

console.log(err);

} else {

console.log("File written successfully\n");
console.log("The written has the following contents:");
console.log(fs.readFileSync(jsonFile, "utf8"));

}
});
console.log(file + ' was created');
})
watcher.on('change', function(file, stats) {
// 確認否為最新的file 若為最新的 就更新json檔案
console.log(file + ' was changed')
})
watcher.on('delete', function(file) {
console.log(file + ' was deleted')
})
}
}

export default AppService;
55 changes: 45 additions & 10 deletions server/controller/chinasun.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import FormatterService from 'server/utils/Formatter.service';
import ChinasunService from '../service/chinasun.service';
Expand All @@ -19,7 +19,7 @@ class ChinasunController {
* set the default configservice and initialize the chinasun service
* @param configService options to let user use config in the controller
*/
constructor(private readonly configService: ConfigService) {
constructor(private configService: ConfigService) {

this.chinasunService = new ChinasunService();
this.initialize();
Expand All @@ -42,18 +42,53 @@ class ChinasunController {
* handle the chinasun/updated_files route
* @controller ChinasunController
*/
//get programlist and programlist?timestamp=
@Get('programlist')
async getUpdated_details() {
// apigetprogramlist
async apiGetProgramlist(@Query() query) {
//get the latest data
let data;
let result;
// get the uodated data and handle the error
try {
data = await this.chinasunService.getUpdatedData();
result = FormatterService.formatData(true,ERROR_CODE.SUCCESS, "programlist", data);
} catch(e) {
result = FormatterService.formatData(true, e.code, e.message, data);
}

if(typeof query.timestamp !== "undefined") {
// if user get timestamp without value
if (query.timestamp === "") {
try {
data = await this.chinasunService.getProgramlist();
// check data in this week or not
result = FormatterService.formatData(true, ERROR_CODE.SUCCESS, "programlist", data);
} catch (e) {

result = FormatterService.formatData(true, e.code, e.message, data);
}
} else {
try {
data = await this.chinasunService.getProgramlistWithTimestamp(query.timestamp);

// check data in this week or not
result = FormatterService.formatData(true, ERROR_CODE.SUCCESS, "programlist", data);
} catch (e) {
result = FormatterService.formatData(true, e.code, e.message, data);
}

}

} else {
try {

data = await this.chinasunService.getProgramlist();

// check data in this week or not
result = FormatterService.formatData(true, ERROR_CODE.SUCCESS, "programlist", data);

} catch (e) {

result = FormatterService.formatData(true, e.code, e.message, data);
}

}

// get the updated data and handle the error

return result;
}
Expand Down
7 changes: 2 additions & 5 deletions server/controller/sendmail.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ class SendMailController {

const CONFIG = {

USER: "[email protected]",
REFRESH_TOKEN: this.configService.get('REFRESH_TOKEN'),
ACCESS_TOKEN: this.configService.get('ACCESS_TOKEN'),
CLIENT_ID: this.configService.get('CLIENT_ID'),
CLIENT_SECRET: this.configService.get('CLIENT_SECRET')
GOOGLE_CLIENT_ID: this.configService.get('GOOGLE_CLIENT_ID'),
GOOGLE_CLIENT_PASSWORD: this.configService.get('GOOGLE_CLIENT_PASSWORD')

}

Expand Down
39 changes: 34 additions & 5 deletions server/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import { NestFactory } from '@nestjs/core';
import AppModule from './app.module';

import express from 'express';
import { ExpressAdapter } from '@nestjs/platform-express';
import http from 'http';
import https from 'https';
import pem from 'pem';
import { ConfigService } from '@nestjs/config';
//
async function bootstrap() {

const SERVER = express();

// initialize
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, new ExpressAdapter(SERVER));
app.setGlobalPrefix('api/v1');
await app.init();

const configService = app.get(ConfigService);
const HTTPS_ENABLE = configService.get('HTTPS_ENABLE');
const HTTP_ENABLE = configService.get('HTTP_ENABLE');
const HTTP_PORT = configService.get('HTTP_PORT');
const HTTPS_PORT = configService.get('HTTPS_PORT');

if (HTTPS_ENABLE == "true") {

pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
if (err) {
throw err
}
https.createServer({ key: keys.serviceKey, cert: keys.certificate }, SERVER).listen(parseInt(HTTPS_PORT));
})

}

if (HTTP_ENABLE == "true") {

http.createServer(SERVER).listen(parseInt(HTTP_PORT));

}

await app.listen(3000);
console.log(`Application is running on: ${await app.getUrl()}`);

}

Expand Down
13 changes: 2 additions & 11 deletions server/middleware/middlewaremain.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MiddlemainMiddleware implements NestMiddleware {
use(req: IncomingMessage, res: ServerResponse, next:any ) {

// didn't use query

// Filter
if(req.url.slice(0,4) === "/api") {
// if api is '/api/v1/i18n/'.. then return i18n realted response
if(req.url.slice(0,13) === "/api/v1/i18n/") {
Expand All @@ -47,15 +47,8 @@ class MiddlemainMiddleware implements NestMiddleware {
res.end();
}

}else if(req.url.slice(0,17) === "/api/v1/chinasun/") {
} else if(req.url.slice(0,28) === "/api/v1/chinasun/programlist") {
// if the prefix = '/api/v1/chinasun/' then call controller and let middleware response the error message;
if(req.url!="/api/v1/chinasun/programlist" && req.url!="/api/v1/chinasun/programlist/") {

res.writeHead(200, { 'content-type': 'application/json' });
res.write(JSON.stringify(FormatterService.formatData(false,ERROR_CODE.API_NOT_SUPPORT_ERROR,"api not support",{})))
res.end();

} else {

try{

Expand All @@ -68,8 +61,6 @@ class MiddlemainMiddleware implements NestMiddleware {
res.end();

}

}

} else if(req.url === "/api/v1/sendmail/" || req.url === "/api/v1/sendmail") {
// handle the path isn't i18n or chinasun
Expand Down
30 changes: 0 additions & 30 deletions server/module/api.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Module, OnModuleInit } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigService } from '@nestjs/config';
import * as hound from 'hound';
import fs from 'fs';
import ChinasunController from '../controller/chinasun.controller'
import ChinasunService from '../service/chinasun.service';
import SendMailController from '../controller/sendmail.controller';
Expand All @@ -13,7 +11,6 @@ import {
QueryResolver,
} from 'nestjs-i18n';
import AppService from 'server/app.service';
import ProgramlistLoader from 'server/utils/ProgramListLoader.service';

// import ConfigModule, I18nModule, Controllers
@Module({
Expand Down Expand Up @@ -48,8 +45,6 @@ import ProgramlistLoader from 'server/utils/ProgramListLoader.service';
*/
class ApiModule implements OnModuleInit {

static chinasunService = ChinasunService;

constructor(private readonly configService: ConfigService) {

}
Expand All @@ -58,31 +53,6 @@ class ApiModule implements OnModuleInit {
* return @param {string} result store the current yyyymmdd string
*/
onModuleInit() {
// register the watcher
const watcher = hound.watch(process.cwd() + this.configService.get('XLSFOLDER_DIR'));

watcher.on('create', async(file, stats) => {

const data = await ProgramlistLoader.getLatestProgramList(process.cwd() + '/xls/');
global.playlist = data;
console.log(file + ' was created');

});

watcher.on('change', async(file, stats) => {

const data = await ProgramlistLoader.getLatestProgramList(process.cwd() + '/xls/');
global.playlist = data;
console.log(file + ' was created');

})

watcher.on('delete', async(file) => {

const data = await ProgramlistLoader.getLatestProgramList(process.cwd() + '/xls/');
global.playlist = data;

})

}

Expand Down
Loading

0 comments on commit c6feb4e

Please sign in to comment.