From c5b3c2bd9893118ee8650587f57ddfde51550d97 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 21:50:46 +0100 Subject: [PATCH] Initial work. --- api/settings.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 api/settings.go diff --git a/api/settings.go b/api/settings.go new file mode 100644 index 0000000..eed2485 --- /dev/null +++ b/api/settings.go @@ -0,0 +1,90 @@ +package api + +import ( + "errors" + "io" + "io/ioutil" + "log" + "net/http" + "os" + + "github.com/ProjectSegfault/segfautils/config" + "github.com/goccy/go-json" +) + +var ( + announcements = config.OptAnn() + form = config.OptForm() +) + +func Settings() { + CheckSet() + http.HandleFunc("/api/options", getOpt) +} + +func CheckSet() { + os.Remove("./data/options.json") + if form == "true" && announcements == "false" { + data := map[string]interface{}{ + "Announcements": "false", + "Form": "true", + } + + jsonData, err := json.Marshal(data) + if err != nil { + log.Printf("Could not marshal json : %s\n", err) + return + } + + ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) + + } else if form == "true" && announcements == "true" { + data := map[string]interface{}{ + "Announcements": "true", + "Form": "true", + } + + jsonData, err := json.Marshal(data) + if err != nil { + log.Printf("Could not marshal json : %s\n", err) + return + } + + ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) + + } else if form == "false" && announcements == "true" { + data := map[string]interface{}{ + "Announcements": "true", + "Form": "false", + } + + jsonData, err := json.Marshal(data) + if err != nil { + log.Printf("Could not marshal json : %s\n", err) + return + } + + ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm) + + } else { + resp := []byte("The fuck do you want me to do then?") + ioutil.WriteFile("./data/options.json", resp, os.ModePerm) + } +} +func getOpt(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + if _, err := os.Stat("./data/options.json"); errors.Is(err, os.ErrNotExist) { + http.Error(w, "There is nothing to see here.", http.StatusNotFound) + return + } else { + f, err := os.Open("./data/options.json") + if err != nil { + log.Fatal(err) + } + defer f.Close() + io.Copy(w, f) + } +}