Fix logging, fix a few bugs, add docker file, move files a bit and add auto build and publish to docker

This commit is contained in:
Jonatan Nilsson 2021-10-11 03:39:01 +00:00
parent 799c4419dc
commit d2e263afbe
11 changed files with 30 additions and 29 deletions

View file

@ -18,11 +18,16 @@ jobs:
name: Run tests name: Run tests
command: | command: |
npm run test:linux npm run test:linux
# - deploy: - setup_remote_docker
# name: Push to docker - run:
# command: | name: Build docker image
# docker login -u $DOCKER_USER -p $DOCKER_PASS command: |
# docker push ${di} docker build -t ${di}:build_${CIRCLE_BUILD_NUM} -t ${di}:${CIRCLE_SHA1} -t ${di}:${dtag} .
- deploy:
name: Push to docker
command: |
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker push ${di}
workflows: workflows:
version: 2 version: 2

2
.gitignore vendored
View file

@ -58,7 +58,7 @@ typings/
.env .env
# Local development config file # Local development config file
config/config.json config/*.json
# Public folder should be ignored # Public folder should be ignored
public/* public/*

View file

@ -1,18 +1,14 @@
FROM node:alpine FROM alpine
ARG NODE=development
ENV HOME=/app \ ENV HOME=/app \
NODE_ENV=${NODE} NODE_ENV=production
COPY package.json $HOME/
WORKDIR $HOME WORKDIR $HOME
RUN npm install COPY package.json $HOME/
COPY . $HOME/ RUN apk update && apk upgrade && apk add nodejs npm && npm install --production
EXPOSE 4020 COPY api $HOME/api
CMD ["npm", "start"] CMD ["npm", "start"]

View file

@ -52,7 +52,7 @@ if (nconf.get('NODE_ENV') === 'production') {
nconf.file('main', path.resolve(path.join(__dirname, configFile))) nconf.file('main', path.resolve(path.join(__dirname, configFile)))
// Load defaults // Load defaults
nconf.file('default', path.resolve(path.join(__dirname, '../config/config.default.json'))) nconf.file('default', path.resolve(path.join(__dirname, '../api/config.default.json')))
// Final sanity checks // Final sanity checks

View file

@ -20,10 +20,3 @@ for (let i = 0; i < settings.streams.length; i++) {
const log = bunyan.createLogger(settings) const log = bunyan.createLogger(settings)
export default log export default log
log.logMiddleware = () =>
(ctx, next) => {
ctx.log = log
return next()
}

View file

@ -14,6 +14,8 @@ export default class MediaRoutes {
let result = await this.formidable.uploadFile(ctx, site) let result = await this.formidable.uploadFile(ctx, site)
ctx.log.info(`Uploaded ${result.filename}`)
ctx.body = { ctx.body = {
filename: result.filename, filename: result.filename,
path: `/${site}/${result.filename}` path: `/${site}/${result.filename}`

View file

@ -13,6 +13,9 @@ const app = new Flaska({
app.before(function(ctx) { app.before(function(ctx) {
ctx.__started = performance.now() ctx.__started = performance.now()
ctx.log = ctx.log.child({
ip: ctx.req.headers['x-forwarded-for'] || ctx.req.connection.remoteAddress,
})
}) })
app.after(function(ctx) { app.after(function(ctx) {
@ -21,11 +24,11 @@ app.after(function(ctx) {
if (ctx.status >= 400) { if (ctx.status >= 400) {
logger = ctx.log.warn logger = ctx.log.warn
} }
logger({ logger.apply(ctx.log, [{
path: ctx.url, path: ctx.url,
status: ctx.status, status: ctx.status,
ms: Math.round(ended), ms: Math.round(ended),
}, 'Request finished') }, 'Request finished'])
}) })
app.onerror(function(err, ctx) { app.onerror(function(err, ctx) {

View file

@ -4,8 +4,8 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"dev": "nodemon index.js", "start": "node api/server.mjs",
"start": "node --experimental-modules api/server.mjs", "start:bunyan": "node api/server.mjs | bunyan",
"test": "set NODE_ENV=test&& eltro test/**/*.test.mjs -r dot", "test": "set NODE_ENV=test&& eltro test/**/*.test.mjs -r dot",
"test:linux": "NODE_ENV=test eltro 'test/**/*.test.mjs' -r dot" "test:linux": "NODE_ENV=test eltro 'test/**/*.test.mjs' -r dot"
}, },
@ -21,7 +21,7 @@
"homepage": "https://github.com/nfp-projects/storage-upload#readme", "homepage": "https://github.com/nfp-projects/storage-upload#readme",
"dependencies": { "dependencies": {
"bunyan-lite": "^1.1.1", "bunyan-lite": "^1.1.1",
"flaska": "^0.9.6", "flaska": "^0.9.7",
"formidable": "^1.2.2", "formidable": "^1.2.2",
"nconf-lite": "^2.0.0" "nconf-lite": "^2.0.0"
}, },

View file

@ -8,8 +8,11 @@ serv.log = {
warn: stub(), warn: stub(),
info: stub(), info: stub(),
error: stub(), error: stub(),
child: stub(),
} }
serv.log.child.returns(serv.log)
export const server = serv export const server = serv
export function createClient() { export function createClient() {

View file

@ -5,7 +5,6 @@ import path from 'path'
import { server, resetLog } from '../helper.server.mjs' import { server, resetLog } from '../helper.server.mjs'
import Client from '../helper.client.mjs' import Client from '../helper.client.mjs'
import config from '../../api/config.mjs'
import encode from '../../api/jwt/encode.mjs' import encode from '../../api/jwt/encode.mjs'
let __dirname = path.dirname(fileURLToPath(import.meta.url)) let __dirname = path.dirname(fileURLToPath(import.meta.url))