Use viper instead of the clusterfuck of json i was trying to use.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Midou36O 2022-09-17 22:33:44 +01:00
parent 41209f3ee3
commit e614ab79fa
Signed by: midou
GPG Key ID: 08063D5407090BC2
4 changed files with 7 additions and 121 deletions

View File

@ -2,7 +2,6 @@ package api
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -16,19 +15,11 @@ import (
var ( var (
authToken = config.AuthToken() authToken = config.AuthToken()
resAnn = config.OptAnn()
) )
func CheckAnn() { func CheckAnn() {
jsonFile, err := os.Open("./data/options.json") if resAnn == "true" {
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" {
Announcements() Announcements()
} else { } else {
log.Println("Announcements disabled") log.Println("Announcements disabled")

View File

@ -1,21 +1,15 @@
package api package api
import ( import (
"io/ioutil" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os"
"github.com/goccy/go-json"
"github.com/kataras/hcaptcha"
"fmt"
"io"
"net/url" "net/url"
"github.com/ProjectSegfault/segfautils/config" "github.com/ProjectSegfault/segfautils/config"
"github.com/ProjectSegfault/segfautils/utils" "github.com/ProjectSegfault/segfautils/utils"
"github.com/kataras/hcaptcha"
) )
var ( var (
@ -23,19 +17,11 @@ var (
secretKey = config.HCaptchaSecretKey() secretKey = config.HCaptchaSecretKey()
webhookURL = config.WebhookURL() webhookURL = config.WebhookURL()
client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */ client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */
resForm = config.OptForm()
) )
func FormCheck() { func FormCheck() {
jsonFile, err := os.Open("./data/options.json") if resForm == "true" {
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" {
Form() Form()
} else { } else {
log.Println("Forms disabled") log.Println("Forms disabled")

View File

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

View File

@ -46,7 +46,6 @@ func main() {
http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/announcements.html") http.ServeFile(w, r, "static/announcements.html")
}) })
api.Settings()
api.FormCheck() api.FormCheck()
api.CheckAnn() api.CheckAnn()
log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!")