Compare commits

..

2 Commits

Author SHA1 Message Date
dc69cc447d
Initial work
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 21:50:55 +01:00
c5b3c2bd98
Initial work. 2022-09-17 21:50:46 +01:00
7 changed files with 152 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
@ -17,6 +18,23 @@ var (
authToken = config.AuthToken()
)
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" {
Announcements()
} else {
log.Println("Announcements disabled")
}
}
func Announcements() {
http.HandleFunc("/api/announcements", getAnnouncements)
http.HandleFunc("/api/announcements/post", handleAnnouncements)

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

18
config/optionannounce.go Normal file
View File

@ -0,0 +1,18 @@
package config
import (
"log"
"github.com/spf13/viper"
)
func OptForm() string {
viper.SetConfigName("config")
viper.AddConfigPath("./data")
err := viper.ReadInConfig()
if err != nil {
log.Println("Error reading config for getting options.form", err.Error())
}
result := viper.GetString("options.form")
return result
}

18
config/optionform.go Normal file
View File

@ -0,0 +1,18 @@
package config
import (
"log"
"github.com/spf13/viper"
)
func OptAnn() string {
viper.SetConfigName("config")
viper.AddConfigPath("./data")
err := viper.ReadInConfig()
if err != nil {
log.Println("Error reading config for getting options.announce", err.Error())
}
result := viper.GetString("options.announce")
return result
}

View File

@ -5,4 +5,8 @@ auth_token = "YOURAUTHTOKEN"
[hcaptcha]
site_key = "YOURSITEKEY"
secret_key = "YOURSECRETKEY"
secret_key = "YOURSECRETKEY"
[options]
announce = true
form = false

1
data/options.json Executable file
View File

@ -0,0 +1 @@
{"Announcements":"false","Form":"true"}

View File

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