Files
outfit-voting-abi26/internal/http/server.go
T
tueem 85e4df52a5
build / Go-Build (push) Successful in 1m1s
feat(*): initial commit
2026-06-24 00:16:21 +02:00

47 lines
1.8 KiB
Go

package http
import (
"net/http"
"tomatentum.net/outfit-voting-abi26/internal/auth"
"tomatentum.net/outfit-voting-abi26/internal/http/vote"
)
var mux *http.ServeMux = http.NewServeMux()
func Start() error {
mux.Handle("/", auth.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
mux.HandleFunc("/api/auth/logout", auth.LogoutEndpoint)
mux.HandleFunc("GET /api/vote/locked", vote.VoteLockedEndpoint)
mux.HandleFunc("GET /api/result/locked", vote.ResultLockedEndpoint)
mux.Handle("PATCH /api/vote/locked", auth.AuthMiddleware(vote.SetVoteLockedEndpoint))
mux.Handle("PATCH /api/result/locked", auth.AuthMiddleware(vote.SetResultLockedEndpoint))
mux.Handle("GET /api/vote", vote.VoteLockMiddleware(vote.GetVoteOptionsEndpoint))
mux.Handle("GET /api/result", vote.ResultLockMiddleware(vote.GetVoteResultEndpoint))
mux.Handle("GET /api/result/{id}", vote.ResultLockMiddleware(vote.GetVoteResultSingleEndoint))
mux.Handle("PATCH /api/vote/{id}/override", auth.AuthMiddleware(vote.SetVoteEndpoint))
mux.Handle("PATCH /api/vote/{id}/inc", vote.VoteLockMiddleware(vote.IncVoteEndpoint))
mux.Handle("DELETE /api/vote/{id}", auth.AuthMiddleware(vote.DelVoteEntryEndpoint))
mux.Handle("POST /api/vote", auth.AuthMiddleware(vote.AddVoteEntryEndpoint))
auth.CallbackHandler(mux)
return http.ListenAndServe(":4000", corsMiddleware(mux))
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}