Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
config/*
node_modules/*
node_modules/*
database/data
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
# Runtime database
pids
*.pid
*.seed
Expand Down Expand Up @@ -105,8 +105,9 @@ dist
# TernJS port file
.tern-port

# do not copy data
# do not copy database
*.db

# do not copy temp files
*~
.idea/
19 changes: 19 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,29 @@ services:
volumes:
- config:/usr/src/bot/config
env_file: ./.env
environment:
- API_KEY
database:
container_name: alterbot_db
build:
context: ./database
dockerfile: ./Dockerfile
restart: unless-stopped
volumes:
- database:/var/database
env_file: ./.env
environment:
- API_KEY
volumes:
config:
driver: local
driver_opts:
type: none
device: ${PWD}/config
o: bind
database:
driver: local
driver_opts:
type: none
device: ${PWD}/database/data
o: bind
1 change: 1 addition & 0 deletions database/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
10 changes: 10 additions & 0 deletions database/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:latest
LABEL authors="awing"
RUN apt update && apt install -y sqlite3 libsqlite3-dev
RUN mkdir -p /var/database
WORKDIR /var/database
COPY package.json /var/database
RUN npm install --build-from-source --sqlite=/usr/local
COPY . /var/database

ENTRYPOINT ["node", "index.js"]
68 changes: 68 additions & 0 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const http = require('http')
const {wordleDatabase} = require("./wordle_database")
require("dotenv").config();

const API_KEY = process.env.API_KEY;
if (!API_KEY) {
console.error("API_KEY is not set in the environment variables.");
process.exit(1);
}

async function sendResponseWithCode(response, code, res){
res.writeHead(code)
const message = JSON.stringify({
code: code,
message: response
})
res.end(message)
}

async function validateParameters(parameterList, inputBody){
const requiredParameters = parameterList.filter(item => !item.endsWith("?"))
return requiredParameters.every(item => item in inputBody.params) && inputBody.params.every(item => item in parameterList)
}

const server = http.createServer(async (req, res) => {
if (req.headers["content-type"] !== "application/json"){
await sendResponseWithCode("API support only application/json", 400, res)
return
}
switch (req.method.toUpperCase()) {
case "GET":
case "PUT":
let body = "";
req.on('data', (chunk) => {
body += chunk;
})
let bodyObject
try {
bodyObject = JSON.parse(body);
} catch (e) {
console.warn(e)
await sendResponseWithCode("API couldn't parse the json data, please verify syntax", 400, res)
}
if (!req.url in wordleDatabase.routes[req.method.toUpperCase()]){
await sendResponseWithCode("Ressource not found", 404, res)
return
}
const parameterForRoute = wordleDatabase.routes[req.method.toUpperCase()][req.url.slice(1)].params
if(!await validateParameters(parameterForRoute, bodyObject)) {
await sendResponseWithCode("Missing or invalid parameters", 400, res)
return
}
wordleDatabase.routes[req.method.toUpperCase()][req.url.slice(1)].func(bodyObject).then(ret => {
sendResponseWithCode(ret, 200, res)
}).catch(err => {
console.error(err)
sendResponseWithCode(err, 500, res)
})
break
default:
await sendResponseWithCode("Allowed method are : GET/PUT", 405, res)
return
}
})

server.listen(25175, "0.0.0.0", () =>{
console.log(`Server started on port ${server.address().port}`);
})
18 changes: 18 additions & 0 deletions database/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "alterbot_wordle_database",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/awing-ding/AlterBot.git"
},
"private": true,
"dependencies": {
"dotenv": "^17.2.2",
"sqlite3": "^5.1.7"
}
}
Loading