aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/nonsense-time.go
blob: 14a402a704c82a5dfa2d5488751c79e704b5bd7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

const VTT_URL string = "http://73.188.175.49:30000"

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 {
		log.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 {
		log.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)
		defer cancel()

		next.ServeHTTP(w, r.WithContext(ctx))
	})
}

func main() {

	port := flag.Int("p", 8080, "the port to listen on")
	bindAddr := flag.String("b", "", "the adress to bind to (leave empty for all interfaces)")
	waitTime := flag.Duration("w", 2*time.Second, "the maximum  time for a request, unit defaults to ns")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
		fmt.Fprintln(os.Stderr, "Options:")
		flag.PrintDefaults()
	}

	flag.Parse()

	addr := fmt.Sprintf("%s:%d", *bindAddr, *port)

	mux := http.NewServeMux()

	vtt := timeoutMiddleware(http.HandlerFunc(vttOnline), *waitTime)
	site := timeoutMiddleware(http.HandlerFunc(siteOnline), *waitTime)

	mux.Handle("GET /vtt/status", vtt)
	mux.HandleFunc("GET /vtt", vttRedirect)
	mux.Handle("GET /site/status", site)

	log.Println("Listening on ", addr)
	log.Fatal(http.ListenAndServe(addr, mux))
}