add base-app

This commit is contained in:
lucaspalomodevelop 2022-11-10 19:58:17 +01:00
parent d02581c7ca
commit 4d96a9b23b
10 changed files with 1990 additions and 7 deletions

37
.gitignore vendored
View File

@ -5,6 +5,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
@ -41,8 +42,8 @@ build/Release
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
@ -53,6 +54,9 @@ typings/
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
@ -68,15 +72,20 @@ typings/
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
# dotenv environment variable files
.env
.env.test
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
@ -84,13 +93,17 @@ dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
@ -102,3 +115,13 @@ dist
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

8
bin/app Normal file
View File

@ -0,0 +1,8 @@
#! /usr/bin/env node
'use strict'
// Pass configuration to application
require('../')({
port: 8000,
host: 'localhost'
})

9
handlers/configured.js Normal file
View File

@ -0,0 +1,9 @@
'use strict'
module.exports = function (opts) {
return function (req, res) {
res.json({
opts: opts
})
}
}

7
handlers/simple.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
module.exports = function (req, res) {
res.json({
hello: 'world!'
})
}

91
index.js Normal file
View File

@ -0,0 +1,91 @@
'use strict'
const express = require('express')
const httpErrors = require('http-errors')
const pino = require('pino')
const pinoHttp = require('pino-http')
module.exports = function main (options, cb) {
// Set default options
const ready = cb || function () {}
const opts = Object.assign({
// Default options
}, options)
const logger = pino()
// Server state
let server
let serverStarted = false
let serverClosing = false
// Setup error handling
function unhandledError (err) {
// Log the errors
logger.error(err)
// Only clean up once
if (serverClosing) {
return
}
serverClosing = true
// If server has started, close it down
if (serverStarted) {
server.close(function () {
process.exit(1)
})
}
}
process.on('uncaughtException', unhandledError)
process.on('unhandledRejection', unhandledError)
// Create the express app
const app = express()
// Common middleware
// app.use(/* ... */)
app.use(pinoHttp({ logger }))
// Register routes
// @NOTE: require here because this ensures that even syntax errors
// or other startup related errors are caught logged and debuggable.
// Alternativly, you could setup external log handling for startup
// errors and handle them outside the node process. I find this is
// better because it works out of the box even in local development.
require('./routes')(app, opts)
// Common error handlers
app.use(function fourOhFourHandler (req, res, next) {
next(httpErrors(404, `Route not found: ${req.url}`))
})
app.use(function fiveHundredHandler (err, req, res, next) {
if (err.status >= 500) {
logger.error(err)
}
res.status(err.status || 500).json({
messages: [{
code: err.code || 'InternalServerError',
message: err.message
}]
})
})
// Start server
server = app.listen(opts.port, opts.host, function (err) {
if (err) {
return ready(err, app, server)
}
// If some other error means we should close
if (serverClosing) {
return ready(new Error('Server was closed before it could start'))
}
serverStarted = true
const addr = server.address()
logger.info(`Started at ${opts.host || addr.host || 'localhost'}:${addr.port}`)
ready(err, app, server)
})
}

View File

@ -0,0 +1 @@
CREATE DATABASE montioring;

View File

@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS 'server' (hostname varchar(50), MAC varchar(50), ip_address varchar(16) )

1814
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "monitoring_backend",
"version": "1.0.0",
"description": "",
"author": "Lucas Manuel Palomo Lauterbach <lucas.palomo@t-online.de>",
"keywords": [],
"license": "GPLv3",
"main": "index.js",
"scripts": {
"start": "node ./bin/app",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express": "^4.18.2",
"http-errors": "^2.0.0",
"pino": "^8.7.0",
"pino-http": "^8.2.1",
"pino-pretty": "^9.1.1"
}
}

9
routes.js Normal file
View File

@ -0,0 +1,9 @@
'use strict'
const simple = require('./handlers/simple')
const configured = require('./handlers/configured')
module.exports = function (app, opts) {
// Setup routes, middleware, and handlers
app.get('/', simple)
app.get('/configured', configured(opts))
}