@@ -0,0 +1,46 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package vote
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"tomatentum.net/outfit-voting-abi26/internal/database"
|
||||
)
|
||||
|
||||
func GetVoteResultEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
entries, err := database.GetVotes()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(entries)
|
||||
}
|
||||
|
||||
func GetVoteResultSingleEndoint(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
entry, err := database.GetVote(id)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(entry)
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package vote
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"tomatentum.net/outfit-voting-abi26/internal/database"
|
||||
)
|
||||
|
||||
type SetVoteRequest struct {
|
||||
Votes int
|
||||
}
|
||||
|
||||
type AddVoteEntryRequest struct {
|
||||
Name string
|
||||
Category string
|
||||
}
|
||||
|
||||
type BulkAddVoteEntryRequest struct {
|
||||
Category string
|
||||
names []string
|
||||
}
|
||||
|
||||
func GetVoteOptionsEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
entries, err := database.GetVoteOptions()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(entries)
|
||||
}
|
||||
|
||||
func SetVoteEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
var request SetVoteRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
if err := database.SetVote(id, request.Votes); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func IncVoteEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
if err := database.IncVote(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func DelVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
if err := database.DelVoteEntry(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func BulkDelVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var ids []string
|
||||
if err := json.NewDecoder(r.Body).Decode(&ids); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
if err := database.BulkDelVoteEntry(ids); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func AddVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var request AddVoteEntryRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
entry, err := database.InsertVoteEntry(request.Name, request.Category)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(entry)
|
||||
}
|
||||
|
||||
func BulkVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var request BulkAddVoteEntryRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
entry, err := database.BulkInsertVoteEntry(request.names, request.Category)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(entry)
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package vote
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"tomatentum.net/outfit-voting-abi26/internal/auth"
|
||||
"tomatentum.net/outfit-voting-abi26/internal/database"
|
||||
)
|
||||
|
||||
type StateChangeRequest struct {
|
||||
Value bool
|
||||
}
|
||||
|
||||
func SetVoteLockedEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var request StateChangeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
if err := database.SetVoteLocked(request.Value); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Updated Vote Lock state to %t\n", request.Value)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func SetResultLockedEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var request StateChangeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
if err := database.SetResultLocked(request.Value); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Updated Result Lock state to %t\n", request.Value)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func VoteLockedEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
locked, err := database.GetVoteLocked()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(locked)
|
||||
}
|
||||
|
||||
func ResultLockedEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
locked, err := database.GetResultLocked()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(locked)
|
||||
}
|
||||
|
||||
func VoteLockMiddleware(next http.HandlerFunc) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ok, err := auth.IsAuth(r)
|
||||
if ok {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
log.Println("DEBUG: User not authenticated so vote lock is not bypassed", err)
|
||||
|
||||
locked, err := database.GetVoteLocked()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if locked {
|
||||
w.WriteHeader(http.StatusLocked)
|
||||
w.Write([]byte("Vote currently locked!"))
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func ResultLockMiddleware(next http.HandlerFunc) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ok, err := auth.IsAuth(r)
|
||||
if ok {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
log.Println("DEBUG: User not authenticated so result lock is not bypassed", err)
|
||||
|
||||
locked, err := database.GetResultLocked()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if locked {
|
||||
w.WriteHeader(http.StatusLocked)
|
||||
w.Write([]byte("Results currently locked!"))
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user