2023-12-05 10:28:08 -08:00
|
|
|
package api
|
|
|
|
|
2023-12-31 14:39:11 -08:00
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"net/http"
|
2024-03-16 18:51:13 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-co-op/gocron"
|
2023-12-31 14:39:11 -08:00
|
|
|
)
|
2023-12-29 11:30:47 -08:00
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Debug bool
|
|
|
|
}
|
|
|
|
|
2024-03-16 18:51:13 -07:00
|
|
|
var (
|
|
|
|
scheduler = gocron.NewScheduler(time.UTC)
|
|
|
|
)
|
|
|
|
|
2023-12-29 11:30:47 -08:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if s.Debug {
|
2023-12-29 11:34:58 -08:00
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "*")
|
2023-12-29 11:33:11 -08:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
2023-12-31 14:39:11 -08:00
|
|
|
|
2023-12-29 11:30:47 -08:00
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-31 14:39:11 -08:00
|
|
|
gob.Register([]interface{}{})
|
|
|
|
gob.Register(map[string]interface{}{})
|
|
|
|
|
2023-12-29 11:30:47 -08:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/account/info":
|
|
|
|
s.HandleAccountInfo(w, r)
|
|
|
|
case "/account/register":
|
|
|
|
s.HandleAccountRegister(w, r)
|
|
|
|
case "/account/login":
|
|
|
|
s.HandleAccountLogin(w, r)
|
|
|
|
case "/account/logout":
|
|
|
|
s.HandleAccountLogout(w, r)
|
|
|
|
|
|
|
|
case "/savedata/get":
|
|
|
|
s.HandleSavedataGet(w, r)
|
|
|
|
case "/savedata/update":
|
|
|
|
s.HandleSavedataUpdate(w, r)
|
|
|
|
case "/savedata/delete":
|
|
|
|
s.HandleSavedataDelete(w, r)
|
2024-03-16 18:51:13 -07:00
|
|
|
case "/savedata/clear":
|
|
|
|
s.HandleSavedataClear(w, r)
|
|
|
|
|
|
|
|
case "/daily/seed":
|
|
|
|
s.HandleSeed(w, r)
|
2024-03-17 10:18:51 -07:00
|
|
|
case "/daily/rankings":
|
|
|
|
s.HandleRankings(w, r)
|
2023-12-29 11:30:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 10:28:08 -08:00
|
|
|
// auth
|
|
|
|
|
|
|
|
type GenericAuthRequest struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GenericAuthResponse struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|