Initial work.

This commit is contained in:
Midou36O 2022-09-17 21:50:46 +01:00
parent e8d8b9b096
commit c5b3c2bd98
Signed by: midou
GPG Key ID: 08063D5407090BC2
1 changed files with 90 additions and 0 deletions

90
api/settings.go Normal file
View File

@ -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)
}
}