diff --git a/.gitignore b/.gitignore index 9184d62..649bd21 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ data/config.toml +data/announcements.json \ No newline at end of file diff --git a/.woodpecker.yml b/.woodpecker.yml index d7ada65..5bca7f5 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -7,9 +7,12 @@ pipeline: - go build -o segfautils dockerize_n_publish: when: + branch : [master] event: [push] name: dockerize and publish - image: plugins/docker + image: plugins/docker + registry: git.projectsegfau.lt + repo: git.projectsegfau.lt/projectsegfault/segfautils settings: username: from_secret: username @@ -17,6 +20,20 @@ pipeline: from_secret: password repo: projectsegfault/segfautils dockerfile: Dockerfile - + dockerize_dev: + when: + event: [push] + image: plugins/docker + name: dockerize and publish dev + registry: git.projectsegfau.lt + repo: git.projectsegfau.lt/projectsegfault/segfautils + settings: + username: + from_secret: username + password: + from_secret: password + repo: projectsegfau.lt/segfautils + tags: dev + dockerfile: Dockerfile diff --git a/api/announcements.go b/api/announcements.go index d6703dc..176dcc1 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -15,8 +15,30 @@ import ( var ( authToken = config.AuthToken() + resAnn = config.OptAnn() ) +func AnnCheck() { + if resAnn == "false" { + log.Println("[Segfautils] ℹ Announcements are disabled") + http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Announcements are disabled.", http.StatusServiceUnavailable) + }) + http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "{\"enabled\": \"false\"}", http.StatusServiceUnavailable) + }) + } else { + AnnPage() + Announcements() + } +} + +func AnnPage() { + http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "static/announcements.html") + }) +} + func Announcements() { http.HandleFunc("/api/announcements", getAnnouncements) http.HandleFunc("/api/announcements/post", handleAnnouncements) @@ -39,6 +61,7 @@ func handleAnnouncements(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) now := time.Now().Unix() data := map[string]interface{}{ + "enabled": "true", "title": r.FormValue("title"), "link": r.FormValue("link"), "severity": r.FormValue("severity"), diff --git a/api/form.go b/api/form.go index dade019..a576564 100644 --- a/api/form.go +++ b/api/form.go @@ -1,18 +1,16 @@ package api import ( + "fmt" + "io" "log" "net/http" - - "github.com/kataras/hcaptcha" - - "fmt" - - "io" "net/url" + "text/template" "github.com/ProjectSegfault/segfautils/config" "github.com/ProjectSegfault/segfautils/utils" + "github.com/kataras/hcaptcha" ) var ( @@ -20,8 +18,37 @@ var ( secretKey = config.HCaptchaSecretKey() webhookURL = config.WebhookURL() client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */ + resForm = config.OptForm() ) +func FormCheck() { + if resForm == "false" { + log.Println("[Segfautils] ℹ Contact form is disabled") + http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Form is disabled.", http.StatusServiceUnavailable) + }) + } else { + FormPage() + Form() + } +} + +func FormPage() { + type StaticThing struct { + HCaptchaSiteKey string + } + + tmpl_form := template.Must(template.ParseFiles("static/form.html")) + http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) { + + hcaptcha_site_key := config.HCaptchaSiteKey() + data := StaticThing{ + HCaptchaSiteKey: hcaptcha_site_key, + } + tmpl_form.Execute(w, data) + }) +} + func Form() { http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode)) } diff --git a/config/authtoken.go b/config/authtoken.go index f9b7b2f..e332783 100644 --- a/config/authtoken.go +++ b/config/authtoken.go @@ -15,7 +15,7 @@ func AuthToken() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting segfautils.auth_token", err.Error()) + log.Println("Error reading config. Error getting: segfautils.auth_token", err.Error()) } result := viper.GetString("segfautils.auth_token") return result diff --git a/config/hcaptchasecretkey.go b/config/hcaptchasecretkey.go index e1cedca..b4834ed 100644 --- a/config/hcaptchasecretkey.go +++ b/config/hcaptchasecretkey.go @@ -11,7 +11,7 @@ func HCaptchaSecretKey() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting hcaptcha.secret_key", err.Error()) + log.Println("Error reading config. Error getting: hcaptcha.secret_key", err.Error()) } result := viper.GetString("hcaptcha.secret_key") return result diff --git a/config/hcaptchasitekey.go b/config/hcaptchasitekey.go index 599d81e..214ce6d 100644 --- a/config/hcaptchasitekey.go +++ b/config/hcaptchasitekey.go @@ -11,7 +11,7 @@ func HCaptchaSiteKey() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting hcaptcha.site_key", err.Error()) + log.Println("Error reading config. Error getting: hcaptcha.site_key", err.Error()) } result := viper.GetString("hcaptcha.site_key") return result diff --git a/config/optionannounce.go b/config/optionannounce.go new file mode 100644 index 0000000..7cb3bba --- /dev/null +++ b/config/optionannounce.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. Error getting: options.announce", err.Error()) + } + result := viper.GetString("options.announce") + return result +} diff --git a/config/optionform.go b/config/optionform.go new file mode 100644 index 0000000..db2f184 --- /dev/null +++ b/config/optionform.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. Error getting: options.form", err.Error()) + } + result := viper.GetString("options.form") + return result +} diff --git a/config/port.go b/config/port.go index 0830001..66f57b2 100644 --- a/config/port.go +++ b/config/port.go @@ -12,7 +12,7 @@ func Port() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting segfautils.port", err.Error()) + log.Println("Error reading config. Error getting: segfautils.port", err.Error()) } result := strconv.Itoa(viper.GetInt("segfautils.port")) return result diff --git a/config/webhook.go b/config/webhook.go index 6c6b03a..2324f18 100644 --- a/config/webhook.go +++ b/config/webhook.go @@ -11,7 +11,7 @@ func WebhookURL() string { viper.AddConfigPath("./data") err := viper.ReadInConfig() if err != nil { - log.Println("Error reading config for getting segfautils.webhook_url", err.Error()) + log.Println("Error reading config. Error getting: segfautils.webhook_url", err.Error()) } result := viper.GetString("segfautils.webhook_url") 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/main.go b/main.go index a5a5ca8..14384b3 100644 --- a/main.go +++ b/main.go @@ -12,18 +12,13 @@ import ( ) type StaticThingy struct { - Port string - HCaptchaSiteKey string + Port string } -var port string -var shit bool - func main() { log.Println("[Segfautils] Starting") utils.CheckConfig() - log.Println("[HTTP] Starting server") - hcaptcha_site_key := config.HCaptchaSiteKey() + tmpl := template.Must(template.ParseFiles("static/index.html")) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { data := StaticThingy{ @@ -32,22 +27,14 @@ func main() { tmpl.Execute(w, data) }) - tmpl_form := template.Must(template.ParseFiles("static/form.html")) - http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) { - data := StaticThingy{ - HCaptchaSiteKey: hcaptcha_site_key, - } - tmpl_form.Execute(w, data) - }) + log.Println("[HTTP] Starting server") + api.AnnCheck() + api.FormCheck() http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "welcome to hell") }) - http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "static/announcements.html") - }) - api.Form() - api.Announcements() + log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println(http.ListenAndServe(":"+config.Port(), nil)) }