Add healthcheck endpoints and scripts
This commit is contained in:
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
port, exists := os.LookupEnv("PORT")
|
||||
if !exists {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
client := http.Client{
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := client.Get("http://localhost:" + port + "/ping")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Print the HTTP Status Code and Status Name
|
||||
fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
|
||||
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
|
||||
fmt.Println("HTTP Status is in the 2xx range")
|
||||
} else {
|
||||
fmt.Println("Argh! Broken")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@ -42,5 +42,11 @@ func main() {
|
||||
"now": tm,
|
||||
})
|
||||
})
|
||||
r.Run() // listen and serve on 0.0.0.0:8080
|
||||
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
tm = database.GetTime(c)
|
||||
c.JSON(200, "pong")
|
||||
})
|
||||
|
||||
r.Run() // listen and serve on 0.0.0.0:8080 (or "PORT" env var)
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
var http = require('http');
|
||||
|
||||
var options = {
|
||||
timeout: 2000,
|
||||
host: 'localhost',
|
||||
port: process.env.PORT || 3000,
|
||||
path: '/ping',
|
||||
};
|
||||
|
||||
var request = http.request(options, (res) => {
|
||||
console.info('STATUS: ' + res.statusCode);
|
||||
process.exitCode = res.statusCode === 200 ? 0 : 1;
|
||||
process.exit();
|
||||
});
|
||||
|
||||
request.on('error', function (err) {
|
||||
console.error('ERROR', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
request.end();
|
||||
@ -4,7 +4,7 @@ const express = require('express');
|
||||
const morgan = require('morgan');
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
// setup the logger
|
||||
app.use(morgan('tiny'));
|
||||
@ -16,6 +16,10 @@ app.get('/', async (req, res) => {
|
||||
res.send(response);
|
||||
});
|
||||
|
||||
app.get('/ping', async (_, res) => {
|
||||
res.send('pong');
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`);
|
||||
});
|
||||
|
||||
@ -1,5 +1,10 @@
|
||||
server {
|
||||
listen 80;
|
||||
location /ping {
|
||||
access_log off;
|
||||
add_header 'Content-Type' 'text/plain';
|
||||
return 200 "pong";
|
||||
}
|
||||
location /api/golang/ {
|
||||
resolver 127.0.0.1;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
|
||||
Reference in New Issue
Block a user