aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/api/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/api.go')
-rw-r--r--api/api.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/api/api.go b/api/api.go
new file mode 100644
index 0000000..d735235
--- /dev/null
+++ b/api/api.go
@@ -0,0 +1,70 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "log"
+ "net/http"
+ "os"
+)
+
+var logger *log.Logger
+
+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 && logger != nil {
+ 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 != nil {
+ logger.Println("Campaign Website is offline")
+ }
+ respondOnline(w, URL, isOnline)
+}
+
+
+func init() {
+ logger = log.New(os.Stdout, "Api: ", log.Ldate | log.Ltime | log.Lshortfile)
+}