Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
redlukas authored Oct 22, 2023
0 parents commit 627480b
Show file tree
Hide file tree
Showing 10 changed files with 1,027 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: npm
directory: "/"
schedule:
interval: "weekly"
33 changes: 33 additions & 0 deletions .github/workflows/dependamerge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Auto approve and merge PRs by dependabot

# Trigger the workflow on pull request
on:
pull_request

jobs:
autoapprove:
name: Auto Approve a PR by dependabot # Name of the job
runs-on: ubuntu-latest # Environment on which the job runs
steps:
- name: Auto approve
uses: hmarr/auto-approve-action@v3.2.1 # Custom action for auto approval already available on marketplace
# Perform the auto approve action only when the PR is raised by dependabot
if: github.actor == 'dependabot[bot]' || github.actor == 'dependabot-preview[bot]'
automerge:
name: Auto merge after successful checks
# By default, jobs run in parallel. To run the jobs sequentially, they keywords "needs" is needed.
# Auto merge action can be done only when the PR is approved, hence this automerge needs autoapprove as a prerequisite
needs: autoapprove
runs-on: ubuntu-latest
steps:
- name: Auto merge
# Custom action for auto merging already available on marketplace
uses: pascalgn/automerge-action@22948e0bc22f0aa673800da838595a3e7347e584
# Perform the auto merge action only when the PR is raised by dependabot
if: github.actor == 'dependabot[bot]' || github.actor == 'dependabot-preview[bot]'
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
# By default, whenever dependabot raises a PR, it automatically assigns a label named "dependencies"
# So, this action merges those PRs labelled "dependencies" only
MERGE_LABELS: dependencies
MERGE_METHOD: rebase
38 changes: 38 additions & 0 deletions .github/workflows/dockerhub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: dockerhub build&push

on:
push:
branches:
- 'master'

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3.6.0
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0

-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: redlukas/sample:latest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/.env
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# syntax=docker/dockerfile:1

FROM node:current-alpine
ENV NODE_ENV=production

WORKDIR /app

ENV USER=appuser
ENV UID=12345
ENV GID=23456

RUN addgroup -S appgroup

RUN adduser \
--disabled-password \
--gecos "" \
--home "$(pwd)" \
--ingroup "appgroup" \
--no-create-home \
--uid "$UID" \
"$USER"

RUN chown -R $USER /app

USER $USER

COPY ["package.json", "package-lock.json*", "./"]

USER root

RUN npm install --omit=dev

USER $USER

COPY . .

CMD [ "node", "main.js" ]
5 changes: 5 additions & 0 deletions default.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MONGO_IP=
MONGO_PORT=
MONGO_USER=
MONGO_PASSWORD=
MONGO_TABLE_NAME=
85 changes: 85 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as dotenv from "dotenv"
import log4js from "log4js"
import mongoose from "mongoose"
import axios from "axios";
import {CronJob} from "cron"
import readline from "readline";
import fs from "fs";

dotenv.config()

//////////////////LOGGER//////////////////////////////////////////////
log4js.configure({
appenders: {
console: {type: "console"}, layout: {
type: "pattern",
pattern: "%[[%d{dd.MM.yy hh:mm:ss}] [%p] %c -%] %m%n"
}
},
categories: {
default: {appenders: ["console"], level: "all"}
}
})
const logger = log4js.getLogger()
logger.level = process.env.NODE_ENV && process.env.NODE_ENV === "production" ? "info" : "debug"

////////////MONGODB//////////////////////////////

const sampleSchema = new mongoose.Schema({
hello: String
})

const Sample = mongoose.model("Sample", sampleSchema)

async function connectToMongo() {
try {
mongoose.set('strictQuery', false)
await mongoose.connect(`mongodb://${process.env.MONGO_IP}:${process.env.MONGO_PORT || 27017}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
user: process.env.MONGO_USER,
pass: process.env.MONGO_PASSWORD,
dbName: process.env.MONGO_TABLE_NAME
})
logger.info('MongoDB connected')
} catch (error) {
logger.fatal("Error connecting to mongodb:", error)
}
}

////////////////////////CRON////////////////////////////////
const weeklyRefresh = new CronJob(
'55 46 3 * * 0',
weeklyFunction,
null,
true,
'Europe/Zurich'
)

////////////////////////////////////////////////////////////
async function setup() {
await checkEnv()
await connectToMongo()
weeklyRefresh.start()
}

async function checkEnv() {
const lineReader = readline.createInterface({
input: fs.createReadStream('./default.env')
})
lineReader.on('line', function (line) {
if(line!=="") {
const variable = line.substring(0, line.indexOf("=") > 0 ? line.indexOf("=") : line.length)
if (!Object.keys(process.env).includes(variable)) {
logger.error(`the ${variable} environment variable is not set`)
process.exit(101)
}
}
})

lineReader.on('close', function () {
logger.debug("All required environment variables are in place")
})
}

setup()
Loading

0 comments on commit 627480b

Please sign in to comment.