aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/nonsense-time.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2024-10-02 12:12:07 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2024-10-02 12:12:07 -0400
commit7e8ad97094fbd7753116254f1021f96447e4ec9c (patch)
tree4a46f7be4571e1777a314a32191f82768087e6d3 /nonsense-time.go
parentceac344f562dfb771796c45b31cd3b85241e3324 (diff)
Refactor api to seperate package
Diffstat (limited to 'nonsense-time.go')
-rw-r--r--nonsense-time.go61
1 files changed, 4 insertions, 57 deletions
diff --git a/nonsense-time.go b/nonsense-time.go
index bbf4932..6c625a1 100644
--- a/nonsense-time.go
+++ b/nonsense-time.go
@@ -2,71 +2,18 @@ package main
import (
"context"
- "encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
+ "nonsense-time/api"
)
-const VTT_URL string = "http://73.188.175.49:30000"
var logger *log.Logger
-func remoteOnline(ctx context.Context, url string) bool {
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
- if err != nil {
- panic(err)
- }
-
- _, err = http.DefaultClient.Do(req)
- if err != nil {
- return false
- }
-
- return true
-}
-
-func respondOnline(w http.ResponseWriter, url string, isOnline bool) {
- data := struct {
- Online bool `json:"online"`
- Site string `json:"url"`
- }{isOnline, url}
-
- jsonData, err := json.Marshal(data)
- if err != nil {
- http.Error(w, "Error constructing response", http.StatusInternalServerError)
- return
- }
-
- w.Header().Set("Content-Type", "application/json")
- w.Write(jsonData)
-}
-
-func vttOnline(w http.ResponseWriter, req *http.Request) {
- isOnline := remoteOnline(req.Context(), VTT_URL)
-
- if !isOnline {
- logger.Println("Foundry VTT is offline")
- }
- respondOnline(w, VTT_URL, isOnline)
-}
-
-func vttRedirect(w http.ResponseWriter, req *http.Request) {
- http.Redirect(w, req, VTT_URL, http.StatusMovedPermanently)
-}
-
-func siteOnline(w http.ResponseWriter, req *http.Request) {
- const URL string = "https://dnd.jpappel.xyz"
- isOnline := remoteOnline(req.Context(), URL)
- if !isOnline {
- logger.Println("Campaign Website is offline")
- }
- respondOnline(w, URL, isOnline)
-}
-
func timeoutMiddleware(next http.Handler, duration time.Duration) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), duration)
@@ -102,11 +49,11 @@ func main() {
}
logger = log.New(os.Stdout, loggerPrefix, loggerFlags)
- vtt := timeoutMiddleware(http.HandlerFunc(vttOnline), *waitTime)
- site := timeoutMiddleware(http.HandlerFunc(siteOnline), *waitTime)
+ vtt := timeoutMiddleware(http.HandlerFunc(api.VttOnline), *waitTime)
+ site := timeoutMiddleware(http.HandlerFunc(api.SiteOnline), *waitTime)
mux.Handle("GET /vtt/status", vtt)
- mux.HandleFunc("GET /vtt", vttRedirect)
+ mux.HandleFunc("GET /vtt", api.VttRedirect)
mux.Handle("GET /site/status", site)
logger.Println("Listening on ", addr)