diff --git a/server.ts b/server.ts index f006ceb..d8fa26a 100644 --- a/server.ts +++ b/server.ts @@ -8,7 +8,11 @@ import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { AppServerModule } from './src/main.server'; import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens"; - +var bodyParser = require('body-parser') +var jsonParser = bodyParser.json() +import * as fs from 'fs'; +import {readFileSync} from "fs"; +import {properties} from "./src/environments/environment"; // The Express app is exported so that it can be used by serverless Functions. export function app(): express.Express { const server = express(); @@ -28,6 +32,46 @@ export function app(): express.Express { // Example Express Rest API endpoints // server.get('/api/**', (req, res) => { }); // Serve static files from /browser + server.get('/log/', (req, res) => { + res.status(200).json(getLogs(null)); + }); + server.get('/log/:id', (req, res) => { + res.status(200).json(getLogs(req.params['id'])); + }); + function getLogs(id){ + const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8'); + return JSON.parse("[" + file.substring(0,file.length - 2) + "]"); + } + server.get('/logFiles', (req, res) => { + fs.readdir(properties.logFilesPath, (err, files) => { + res.status(200).json(files); + }); + + }); + + + server.get('/prettyLog', (req, res) => { + const file = readFileSync('/var/log/irish-log/foo.txt', 'utf-8'); + let logs = JSON.parse("[" + file.substring(0,file.length - 2) + "]"); + let prettyLog = ""; + for(let i = logs.length -1; i>=0; i--){ + prettyLog += "On " + (logs[i].date?logs[i].date:"") + " " /*+ logs[i].action + " "*/ + logs[i].message+ "
"; + } + res.status(200).send( prettyLog); + }); + server.post('/logAction', jsonParser,(req, res) => { + let log = req.body; + log.date = new Date(); + fs.appendFile('/var/log/irish-log/foo.txt', JSON.stringify(log)+ ",\n", 'utf8', function(err) { + if (err) { + return console.error(err); + } + }); + res.status(200).send({ + code: 200, + message: 'action received!' + }); + }); server.get('*.*', express.static(distFolder, { maxAge: '1y' })); diff --git a/src/app/upload-dois/upload-dois.component.ts b/src/app/upload-dois/upload-dois.component.ts index 2a4fc77..39e419f 100644 --- a/src/app/upload-dois/upload-dois.component.ts +++ b/src/app/upload-dois/upload-dois.component.ts @@ -7,6 +7,7 @@ import {properties} from "../../environments/environment"; import {SearchResearchResultsService} from "../openaireLibrary/services/searchResearchResults.service"; import {map} from "rxjs/operators"; import {Filter} from "../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class"; +import {LogService} from "../openaireLibrary/utils/log/log.service"; @Component({ selector: 'upload-dois', @@ -68,7 +69,7 @@ export class UploadDoisComponent implements OnInit { keyword = ""; loading = false; showFound = true; - constructor(private _searchResearchResultsService: SearchResearchResultsService) { + constructor(private _searchResearchResultsService: SearchResearchResultsService, private _logService: LogService) { } @@ -157,6 +158,9 @@ export class UploadDoisComponent implements OnInit { } this.enableUpload = true; + if(properties.logFilesService) { + this.subscriptions.push(this._logService.logUploadDOIs(properties, this.allIds.length).subscribe(res => { })); + } this.fetchAllResults(); }, (error) => { this.enableUpload = true; diff --git a/src/app/upload-dois/upload-dois.module.ts b/src/app/upload-dois/upload-dois.module.ts index f31600d..88208df 100644 --- a/src/app/upload-dois/upload-dois.module.ts +++ b/src/app/upload-dois/upload-dois.module.ts @@ -8,12 +8,13 @@ import {SearchInputModule} from "../openaireLibrary/sharedComponents/search-inpu import {IconsModule} from "../openaireLibrary/utils/icons/icons.module"; import {DropdownFilterModule} from "../openaireLibrary/utils/dropdown-filter/dropdown-filter.module"; import {SearchFilterModule} from "../openaireLibrary/searchPages/searchUtils/searchFilter.module"; +import {LogServiceModule} from "../openaireLibrary/utils/log/LogService.module"; @NgModule({ declarations: [UploadDoisComponent], imports: [ - CommonModule, UploadDoisRoutingModule, SearchResearchResultsServiceModule, PagingModule, SearchInputModule, IconsModule, DropdownFilterModule, SearchFilterModule + CommonModule, UploadDoisRoutingModule, SearchResearchResultsServiceModule, PagingModule, SearchInputModule, IconsModule, DropdownFilterModule, SearchFilterModule, LogServiceModule ], })