Partial working form and announcements settings.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Midou36O 2022-09-17 23:12:06 +01:00
parent e614ab79fa
commit 9a504d1638
Signed by: midou
GPG Key ID: 08063D5407090BC2
3 changed files with 36 additions and 20 deletions

View File

@ -21,14 +21,24 @@ var (
func CheckAnn() { func CheckAnn() {
if resAnn == "true" { if resAnn == "true" {
Announcements() Announcements()
AnnPage()
} else { } else {
log.Println("Announcements disabled") log.Println("Announcements disabled")
http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Announcements are disabled.", http.StatusNotFound) http.Error(w, "Announcements are disabled.", http.StatusNotFound)
}) })
http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "{\"enabled\": \"false\"}", http.StatusNotFound)
})
} }
} }
func AnnPage() {
http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/announcements.html")
})
}
func Announcements() { func Announcements() {
http.HandleFunc("/api/announcements", getAnnouncements) http.HandleFunc("/api/announcements", getAnnouncements)
http.HandleFunc("/api/announcements/post", handleAnnouncements) http.HandleFunc("/api/announcements/post", handleAnnouncements)

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"text/template"
"github.com/ProjectSegfault/segfautils/config" "github.com/ProjectSegfault/segfautils/config"
"github.com/ProjectSegfault/segfautils/utils" "github.com/ProjectSegfault/segfautils/utils"
@ -22,6 +23,7 @@ var (
func FormCheck() { func FormCheck() {
if resForm == "true" { if resForm == "true" {
FormPage()
Form() Form()
} else { } else {
log.Println("Forms disabled") log.Println("Forms disabled")
@ -31,6 +33,22 @@ func FormCheck() {
} }
} }
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() { func Form() {
http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode)) http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode))
} }

26
main.go
View File

@ -12,18 +12,13 @@ import (
) )
type StaticThingy struct { type StaticThingy struct {
Port string Port string
HCaptchaSiteKey string
} }
var port string
var shit bool
func main() { func main() {
log.Println("[Segfautils] Starting") log.Println("[Segfautils] Starting")
utils.CheckConfig() utils.CheckConfig()
log.Println("[HTTP] Starting server")
hcaptcha_site_key := config.HCaptchaSiteKey()
tmpl := template.Must(template.ParseFiles("static/index.html")) tmpl := template.Must(template.ParseFiles("static/index.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := StaticThingy{ data := StaticThingy{
@ -32,22 +27,15 @@ func main() {
tmpl.Execute(w, data) tmpl.Execute(w, data)
}) })
tmpl_form := template.Must(template.ParseFiles("static/form.html")) log.Println("[HTTP] Starting server")
http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) { api.CheckAnn()
data := StaticThingy{ api.FormCheck()
HCaptchaSiteKey: hcaptcha_site_key,
}
tmpl_form.Execute(w, data)
})
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "welcome to hell") 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.FormCheck()
api.CheckAnn()
log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!") log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!")
log.Println(http.ListenAndServe(":"+config.Port(), nil)) log.Println(http.ListenAndServe(":"+config.Port(), nil))
} }