Use viper instead of the clusterfuck of json i was trying to use.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
parent
41209f3ee3
commit
e614ab79fa
@ -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")
|
||||
|
24
api/form.go
24
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")
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
1
main.go
1
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() + "!")
|
||||
|
Reference in New Issue
Block a user