From e614ab79fa9df6a408c4af946a8ddf055a57b4f0 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Sep 2022 22:33:44 +0100 Subject: [PATCH] Use viper instead of the clusterfuck of json i was trying to use. --- api/announcements.go | 13 +------ api/form.go | 24 +++--------- api/settings.go | 90 -------------------------------------------- main.go | 1 - 4 files changed, 7 insertions(+), 121 deletions(-) delete mode 100644 api/settings.go diff --git a/api/announcements.go b/api/announcements.go index 95903bf..4b37903 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -2,7 +2,6 @@ package api import ( "errors" - "fmt" "io" "io/ioutil" "log" @@ -16,19 +15,11 @@ import ( var ( authToken = config.AuthToken() + resAnn = config.OptAnn() ) func CheckAnn() { - jsonFile, err := os.Open("./data/options.json") - if err != nil { - fmt.Println(err) - } - defer jsonFile.Close() - byteValue, _ := ioutil.ReadAll(jsonFile) - var result map[string]interface{} - json.Unmarshal([]byte(byteValue), &result) - res := result["Announcements"] - if res == "true" { + if resAnn == "true" { Announcements() } else { log.Println("Announcements disabled") diff --git a/api/form.go b/api/form.go index 1a73e93..9edce97 100644 --- a/api/form.go +++ b/api/form.go @@ -1,21 +1,15 @@ package api import ( - "io/ioutil" + "fmt" + "io" "log" "net/http" - "os" - - "github.com/goccy/go-json" - "github.com/kataras/hcaptcha" - - "fmt" - - "io" "net/url" "github.com/ProjectSegfault/segfautils/config" "github.com/ProjectSegfault/segfautils/utils" + "github.com/kataras/hcaptcha" ) var ( @@ -23,19 +17,11 @@ var ( secretKey = config.HCaptchaSecretKey() webhookURL = config.WebhookURL() client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */ + resForm = config.OptForm() ) func FormCheck() { - jsonFile, err := os.Open("./data/options.json") - if err != nil { - fmt.Println(err) - } - defer jsonFile.Close() - byteValue, _ := ioutil.ReadAll(jsonFile) - var result map[string]interface{} - json.Unmarshal([]byte(byteValue), &result) - res := result["Form"] - if res == "true" { + if resForm == "true" { Form() } else { log.Println("Forms disabled") diff --git a/api/settings.go b/api/settings.go deleted file mode 100644 index eed2485..0000000 --- a/api/settings.go +++ /dev/null @@ -1,90 +0,0 @@ -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) - } -} diff --git a/main.go b/main.go index 19fdeba..c6f22de 100644 --- a/main.go +++ b/main.go @@ -46,7 +46,6 @@ func main() { http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "static/announcements.html") }) - api.Settings() api.FormCheck() api.CheckAnn() log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!")