-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔄 synced file(s) with circlefin/pw-sdk-nodejs-server-internal (#3)
synced local file(s) with [circlefin/pw-sdk-nodejs-server-internal](https://github.com/circlefin/pw-sdk-nodejs-server-internal). <details> <summary>Changed files</summary> <ul> <li>synced local <code>package.json</code> with remote <code>package.json</code></li><li>synced local <code>yarn.lock</code> with remote <code>yarn.lock</code></li><li>synced local <code>README.md</code> with remote <code>README.md</code></li><li>synced local directory <code>src/</code> with remote directory <code>src/</code></li><li>synced local <code>LICENSE</code> with remote <code>LICENSE</code></li> </ul> </details> --- This PR was created automatically by the [repo-file-sync-action](https://github.com/BetaHuhn/repo-file-sync-action) workflow run [#8842310024](https://github.com/circlefin/pw-sdk-nodejs-server-internal/actions/runs/8842310024)
- Loading branch information
Showing
11 changed files
with
268 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import { server } from './app'; | ||
import { app } from './app'; | ||
import { initDB, cleanupDB } from './services/db/sqlite/sqliteDB'; | ||
import { | ||
logger, | ||
registerLogger, | ||
SampleServerLogger | ||
} from './services/logging/logger'; | ||
|
||
registerLogger(new SampleServerLogger()); | ||
initDB(); | ||
|
||
const port = process.env.PORT ?? 8080; | ||
const server = app.listen(port, () => { | ||
logger.info(`Server is running at http://localhost:${port}`); | ||
}); | ||
|
||
process.on('SIGINT', function () { | ||
cleanupDB(); | ||
server.close(); | ||
console.log('http server closed successfully.'); | ||
logger.info('Server closed successfully'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import { Database } from 'sqlite3'; | ||
import { registerUserDAO } from '../dao'; | ||
import { SqliteUserDAO } from './sqliteUserDAO'; | ||
import { logger } from '../../logging/logger'; | ||
|
||
const client = new Database(process.env.DATABASE_FILENAME ?? ':memory:'); | ||
const userDAO = new SqliteUserDAO(client); | ||
|
||
export const createUserTable = (db: Database) => { | ||
db.exec( | ||
'CREATE TABLE IF NOT EXISTS users (userId TEXT PRIMARY KEY, email TEXT UNIQUE, password TEXT, createdAt TEXT DEFAULT CURRENT_TIMESTAMP)' | ||
); | ||
db.serialize(() => { | ||
db.exec( | ||
'CREATE TABLE IF NOT EXISTS users (userId TEXT PRIMARY KEY, email TEXT UNIQUE, password TEXT, createdAt TEXT DEFAULT CURRENT_TIMESTAMP)' | ||
); | ||
}); | ||
}; | ||
|
||
export const initDB = () => { | ||
registerUserDAO(userDAO); | ||
createUserTable(client); | ||
logger.info('Created users table'); | ||
}; | ||
|
||
export const cleanupDB = () => { | ||
client.close((err) => { | ||
if (err) { | ||
return console.error(err.message); | ||
return logger.error(err.message); | ||
} | ||
console.log('Close the database connection.'); | ||
logger.info('Database connection closed successfully'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export interface Logger { | ||
info: (message: string) => void; | ||
warn: (message: string) => void; | ||
error: (message: string) => void; | ||
debug: (message: string) => void; | ||
} | ||
|
||
export class SampleServerLogger implements Logger { | ||
info(message: string) { | ||
console.log('[SampleServerLogger]: ' + message); | ||
} | ||
warn(message: string) { | ||
console.log('[SampleServerLogger]: ' + message); | ||
} | ||
error(message: string) { | ||
console.log('[SampleServerLogger]: ' + message); | ||
} | ||
debug(message: string) { | ||
console.log('[SampleServerLogger]: ' + message); | ||
} | ||
} | ||
|
||
export let logger: Logger; | ||
|
||
export const registerLogger = (newLogger: Logger) => { | ||
logger = newLogger; | ||
}; |
Oops, something went wrong.