Skip to content

Commit

Permalink
add health check and openapi spec
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Nov 30, 2024
1 parent 2be2a99 commit ceb5b00
Show file tree
Hide file tree
Showing 3 changed files with 1,348 additions and 756 deletions.
102 changes: 76 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
const fs = require('fs');
const http = require('http');
const express = require("express");
const swaggerjsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const app = express();
const cors = require('cors')
const { GameDig } = require('gamedig');
const mcache = require('memory-cache');
const port = process.env.PORT || 3035;

const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
title: 'GameDig API',
description: 'GameDig API, used within widgets.gametools.network and the gamedig status bot available on GitHub.',
contact: {
name: 'Zefanja Jobse'
},
},
servers: [
{
url: "https://gamedig.gametools.network", description: "Main production environment"
},
{
url: "http://localhost:3035", description: "Development environment"
}
],
},
apis: ['./index.js']
}

app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET');
Expand Down Expand Up @@ -34,32 +58,37 @@ const cache = (duration) => {
}
}

app.get("/amg/1", cache(120), (req, res, next) => {
res.set('Cache-Control', 'public, max-age=120');
GameDig.query({
type: "forrest",
host: "157.90.7.148",
port: "10015",
}).then((state) => {
res.json(state);
}).catch((error) => {
res.json(error);
});
});

app.get("/amg/2", cache(120), (req, res, next) => {
res.set('Cache-Control', 'public, max-age=120');
GameDig.query({
type: "rust",
host: "51.77.77.129",
port: "27030",
}).then((state) => {
res.json(state);
}).catch((error) => {
res.json(error);
});
});

/**
* @openapi
* /game/{gamename}/{host}/{port}:
* get:
* summary: Get game data from GameDig.
* description: Get game data from GameDig.
* parameters:
* - in: path
* name: gamename
* schema:
* type: string
* required: true
* description: the name of the game from gamedig, for example hll. all games are available on https://github.com/gamedig/node-gamedig/blob/master/GAMES_LIST.md
* - in: path
* name: host
* schema:
* type: string
* required: true
* description: Ip address of the server you want to request data of
* - in: path
* name: port
* schema:
* type: integer
* required: true
* description: Port of the server you want to request data of
* responses:
* '200':
* description: A successful response
* '500':
* description: Internal server error
*/
app.get('/game/:gamename/:host/:port', cache(120), (req, res, next) => {
res.set('Cache-Control', 'public, max-age=120');
GameDig.query({
Expand All @@ -73,6 +102,27 @@ app.get('/game/:gamename/:host/:port', cache(120), (req, res, next) => {
});
});


/**
* @openapi
* /health_check:
* get:
* summary: Healthcheck to check if this service is running
* description: Healthcheck to check if this service is running
* responses:
* '200':
* description: A successful response
* '500':
* description: Internal server error
*/
app.get('/health_check', cache(120), (req, res, next) => {
res.set('Cache-Control', 'public, max-age=120');
res.json({ "status": "ok" });
});

const swaggerDocs = swaggerjsdoc(swaggerOptions)
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocs))

var httpsServer = http.createServer({}, app);

const server = httpsServer.listen(port, () => console.log(`App listening on port ${port}!`));
Expand Down
Loading

0 comments on commit ceb5b00

Please sign in to comment.