Making smaller functions to make switch simpler

This commit is contained in:
collin 2025-06-30 15:38:18 +02:00
parent a0786860e2
commit d431f34e72
No known key found for this signature in database

View file

@ -32,37 +32,6 @@ func getStaticFile(relPath string, contentType string, w http.ResponseWriter) {
w.Write(file) w.Write(file)
} }
func SetupAuth() {
var username string
var password string
for _, entry := range os.Environ() {
split := strings.Split(entry, "=")
switch split[0] {
case "USERNAME":
username = split[1]
case "PASSWORD":
password = split[1]
}
if username != "" && password != "" {
break
}
}
if username == "" || password == "" {
log.Fatal("no authorization details")
}
sb := new(strings.Builder)
encoder := base64.NewEncoder(base64.StdEncoding, sb)
encoder.Write(fmt.Appendf(nil, "%s:%s", username, password))
encoder.Close()
token = sb.String()
fmt.Println("auth ready")
}
func isAuthorized(r *http.Request) bool { func isAuthorized(r *http.Request) bool {
auth := r.Header.Get("Authorization") auth := r.Header.Get("Authorization")
@ -73,35 +42,7 @@ func isAuthorized(r *http.Request) bool {
return auth == fmt.Sprintf("Basic %s", token) return auth == fmt.Sprintf("Basic %s", token)
} }
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) getRsvps(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%s - [%s] (%s) %s\n", time.Now().Format(time.RFC3339), r.RemoteAddr, r.Method, r.URL)
switch true {
case r.Method == "GET" && r.URL.Path == "/":
getStaticFile("./client/index.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/rsvp":
getStaticFile("./client/rsvp.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/rsvp_confirmed":
getStaticFile("./client/rsvp_confirmed.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/rsvps_list":
getStaticFile("./client/rsvps_list.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/index.js":
getStaticFile("./client/index.js", "text/javascript", w)
case r.Method == "GET" && r.URL.Path == "/rsvp.js":
getStaticFile("./client/rsvp.js", "text/javascript", w)
case r.Method == "GET" && r.URL.Path == "/rsvps_list.js":
getStaticFile("./client/rsvps_list.js", "text/javascript", w)
case r.Method == "GET" && r.URL.Path == "/style.css":
getStaticFile("./client/style.css", "text/css", w)
case r.Method == "GET" && r.URL.Path == "/api/rsvps":
if !isAuthorized(r) { if !isAuthorized(r) {
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
return return
@ -116,8 +57,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%#v", rsvps) fmt.Fprintf(w, "%#v", rsvps)
}
case r.Method == "POST" && r.URL.Path == "/api/rsvps": func (h *Handler) createRsvp(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
@ -167,6 +109,73 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
w.Header().Add("Location", "/rsvp_confirmed") w.Header().Add("Location", "/rsvp_confirmed")
w.WriteHeader(http.StatusSeeOther) w.WriteHeader(http.StatusSeeOther)
}
func SetupAuth() {
var username string
var password string
for _, entry := range os.Environ() {
split := strings.Split(entry, "=")
switch split[0] {
case "USERNAME":
username = split[1]
case "PASSWORD":
password = split[1]
}
if username != "" && password != "" {
break
}
}
if username == "" || password == "" {
log.Fatal("no authorization details")
}
sb := new(strings.Builder)
encoder := base64.NewEncoder(base64.StdEncoding, sb)
encoder.Write(fmt.Appendf(nil, "%s:%s", username, password))
encoder.Close()
token = sb.String()
fmt.Println("auth ready")
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%s - [%s] (%s) %s\n", time.Now().Format(time.RFC3339), r.RemoteAddr, r.Method, r.URL)
switch true {
case r.Method == "GET" && r.URL.Path == "/":
getStaticFile("./client/index.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/rsvp":
getStaticFile("./client/rsvp.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/rsvp_confirmed":
getStaticFile("./client/rsvp_confirmed.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/rsvps_list":
getStaticFile("./client/rsvps_list.html", "text/html", w)
case r.Method == "GET" && r.URL.Path == "/index.js":
getStaticFile("./client/index.js", "text/javascript", w)
case r.Method == "GET" && r.URL.Path == "/rsvp.js":
getStaticFile("./client/rsvp.js", "text/javascript", w)
case r.Method == "GET" && r.URL.Path == "/rsvps_list.js":
getStaticFile("./client/rsvps_list.js", "text/javascript", w)
case r.Method == "GET" && r.URL.Path == "/style.css":
getStaticFile("./client/style.css", "text/css", w)
case r.Method == "GET" && r.URL.Path == "/api/rsvps":
h.getRsvps(w, r)
case r.Method == "POST" && r.URL.Path == "/api/rsvps":
h.createRsvp(w, r)
default: default:
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }