diff --git a/api/announcements.go b/api/announcements.go index d6703dc..7200f7f 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -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) diff --git a/config/optionannounce.go b/config/optionannounce.go new file mode 100644 index 0000000..06b1836 --- /dev/null +++ b/config/optionannounce.go @@ -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 +} diff --git a/config/optionform.go b/config/optionform.go new file mode 100644 index 0000000..1f81ea7 --- /dev/null +++ b/config/optionform.go @@ -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 +} diff --git a/data/config.example.toml b/data/config.example.toml index 84aad0c..a172f14 100644 --- a/data/config.example.toml +++ b/data/config.example.toml @@ -5,4 +5,8 @@ auth_token = "YOURAUTHTOKEN" [hcaptcha] site_key = "YOURSITEKEY" -secret_key = "YOURSECRETKEY" \ No newline at end of file +secret_key = "YOURSECRETKEY" + +[options] +announce = true +form = false \ No newline at end of file diff --git a/data/options.json b/data/options.json new file mode 100755 index 0000000..3866b67 --- /dev/null +++ b/data/options.json @@ -0,0 +1 @@ +{"Announcements":"false","Form":"true"} \ No newline at end of file diff --git a/main.go b/main.go index a5a5ca8..810f05f 100644 --- a/main.go +++ b/main.go @@ -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)) }