|
|
@ -7,7 +7,6 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/account"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/account"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/daily"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/daily"
|
|
|
@ -16,308 +15,301 @@ import (
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
|
|
|
|
Debug bool
|
|
|
|
|
|
|
|
Exit *sync.RWMutex
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
The caller of endpoint handler functions are responsible for extracting the necessary data from the request.
|
|
|
|
The caller of endpoint handler functions are responsible for extracting the necessary data from the request.
|
|
|
|
Handler functions are responsible for checking the validity of this data and returning a result or error.
|
|
|
|
Handler functions are responsible for checking the validity of this data and returning a result or error.
|
|
|
|
Handlers should not return serialized JSON, instead return the struct itself.
|
|
|
|
Handlers should not return serialized JSON, instead return the struct itself.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
|
|
|
// kind of misusing the RWMutex but it doesn't matter
|
|
|
|
log.Printf("%s: %s\n", r.URL.Path, err)
|
|
|
|
s.Exit.RLock()
|
|
|
|
http.Error(w, err.Error(), code)
|
|
|
|
defer s.Exit.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if s.Debug {
|
|
|
|
// account
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
|
|
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "*")
|
|
|
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
username, err := getUsernameFromRequest(r)
|
|
|
|
return
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
uuid, err := getUUIDFromRequest(r) // lazy
|
|
|
|
// /account
|
|
|
|
if err != nil {
|
|
|
|
case "/account/info":
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
username, err := getUsernameFromRequest(r)
|
|
|
|
return
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uuid, err := getUUIDFromRequest(r) // lazy
|
|
|
|
response, err := account.Info(username, uuid)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response, err := account.Info(username, uuid)
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
case "/account/register":
|
|
|
|
err := r.ParseForm()
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = account.Register(r.Form.Get("username"), r.Form.Get("password"))
|
|
|
|
err = account.Register(r.Form.Get("username"), r.Form.Get("password"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
case "/account/login":
|
|
|
|
}
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response, err := account.Login(r.Form.Get("username"), r.Form.Get("password"))
|
|
|
|
func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err != nil {
|
|
|
|
err := r.ParseForm()
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
response, err := account.Login(r.Form.Get("username"), r.Form.Get("password"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
case "/account/logout":
|
|
|
|
if err != nil {
|
|
|
|
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode token: %s", err), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = account.Logout(token)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode token: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// /game
|
|
|
|
err = account.Logout(token)
|
|
|
|
case "/game/playercount":
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(strconv.Itoa(playerCount)))
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
case "/game/titlestats":
|
|
|
|
return
|
|
|
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
|
|
|
}
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
|
|
|
|
BattleCount: battleCount,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
case "/game/classicsessioncount":
|
|
|
|
}
|
|
|
|
w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /savedata
|
|
|
|
// game
|
|
|
|
case "/savedata/get", "/savedata/update", "/savedata/delete", "/savedata/clear":
|
|
|
|
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datatype := -1
|
|
|
|
func handleGamePlayerCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Query().Has("datatype") {
|
|
|
|
w.Write([]byte(strconv.Itoa(playerCount)))
|
|
|
|
datatype, err = strconv.Atoi(r.URL.Query().Get("datatype"))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var slot int
|
|
|
|
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Query().Has("slot") {
|
|
|
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
|
|
|
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
if err != nil {
|
|
|
|
BattleCount: battleCount,
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var save any
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body
|
|
|
|
}
|
|
|
|
if r.URL.Path != "/savedata/get" && r.URL.Path != "/savedata/delete" {
|
|
|
|
|
|
|
|
if datatype == 0 {
|
|
|
|
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
save = system
|
|
|
|
return
|
|
|
|
// /savedata/clear doesn't specify datatype, it is assumed to be 1 (session)
|
|
|
|
}
|
|
|
|
} else if datatype == 1 || r.URL.Path == "/savedata/clear" {
|
|
|
|
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
datatype := -1
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
if r.URL.Query().Has("datatype") {
|
|
|
|
if err != nil {
|
|
|
|
datatype, err = strconv.Atoi(r.URL.Query().Get("datatype"))
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
save = session
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var token []byte
|
|
|
|
var slot int
|
|
|
|
token, err = getTokenFromRequest(r)
|
|
|
|
if r.URL.Query().Has("slot") {
|
|
|
|
|
|
|
|
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var active bool
|
|
|
|
var save any
|
|
|
|
if r.URL.Path == "/savedata/get" {
|
|
|
|
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body
|
|
|
|
err = db.UpdateActiveSession(uuid, token)
|
|
|
|
if r.URL.Path != "/savedata/get" && r.URL.Path != "/savedata/delete" {
|
|
|
|
|
|
|
|
if datatype == 0 {
|
|
|
|
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
active, err = db.IsActiveSession(token)
|
|
|
|
save = system
|
|
|
|
|
|
|
|
// /savedata/clear doesn't specify datatype, it is assumed to be 1 (session)
|
|
|
|
|
|
|
|
} else if datatype == 1 || r.URL.Path == "/savedata/clear" {
|
|
|
|
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: make this not suck
|
|
|
|
save = session
|
|
|
|
if !active && r.URL.Path != "/savedata/clear" {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
var token []byte
|
|
|
|
case "/savedata/get":
|
|
|
|
token, err = getTokenFromRequest(r)
|
|
|
|
save, err = savedata.Get(uuid, datatype, slot)
|
|
|
|
if err != nil {
|
|
|
|
case "/savedata/update":
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
err = savedata.Update(uuid, slot, save)
|
|
|
|
return
|
|
|
|
case "/savedata/delete":
|
|
|
|
}
|
|
|
|
err = savedata.Delete(uuid, datatype, slot)
|
|
|
|
|
|
|
|
case "/savedata/clear":
|
|
|
|
|
|
|
|
if !active {
|
|
|
|
|
|
|
|
// TODO: make this not suck
|
|
|
|
|
|
|
|
save = savedata.ClearResponse{Error: "session out of date"}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s, ok := save.(defs.SessionSaveData)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
err = fmt.Errorf("save data is not type SessionSaveData")
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// doesn't return a save, but it works
|
|
|
|
var active bool
|
|
|
|
save, err = savedata.Clear(uuid, slot, daily.Seed(), s)
|
|
|
|
if r.URL.Path == "/savedata/get" {
|
|
|
|
|
|
|
|
err = db.UpdateActiveSession(uuid, token)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
active, err = db.IsActiveSession(token)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if save == nil || r.URL.Path == "/savedata/update" {
|
|
|
|
// TODO: make this not suck
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if !active && r.URL.Path != "/savedata/clear" {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(save)
|
|
|
|
switch r.URL.Path {
|
|
|
|
if err != nil {
|
|
|
|
case "/savedata/get":
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
save, err = savedata.Get(uuid, datatype, slot)
|
|
|
|
return
|
|
|
|
case "/savedata/update":
|
|
|
|
|
|
|
|
err = savedata.Update(uuid, slot, save)
|
|
|
|
|
|
|
|
case "/savedata/delete":
|
|
|
|
|
|
|
|
err = savedata.Delete(uuid, datatype, slot)
|
|
|
|
|
|
|
|
case "/savedata/clear":
|
|
|
|
|
|
|
|
if !active {
|
|
|
|
|
|
|
|
// TODO: make this not suck
|
|
|
|
|
|
|
|
save = savedata.ClearResponse{Error: "session out of date"}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
s, ok := save.(defs.SessionSaveData)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
err = fmt.Errorf("save data is not type SessionSaveData")
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// /daily
|
|
|
|
// doesn't return a save, but it works
|
|
|
|
case "/daily/seed":
|
|
|
|
save, err = savedata.Clear(uuid, slot, daily.Seed(), s)
|
|
|
|
w.Write([]byte(daily.Seed()))
|
|
|
|
}
|
|
|
|
case "/daily/rankings":
|
|
|
|
if err != nil {
|
|
|
|
var err error
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var category int
|
|
|
|
if save == nil || r.URL.Path == "/savedata/update" {
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
|
|
|
return
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
page := 1
|
|
|
|
err = json.NewEncoder(w).Encode(save)
|
|
|
|
if r.URL.Query().Has("page") {
|
|
|
|
if err != nil {
|
|
|
|
page, err = strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
httpError(w, r, fmt.Errorf("failed to convert page: %s", err), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rankings, err := daily.Rankings(category, page)
|
|
|
|
// daily
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleDailySeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.Write([]byte(daily.Seed()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var category int
|
|
|
|
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(rankings)
|
|
|
|
page := 1
|
|
|
|
|
|
|
|
if r.URL.Query().Has("page") {
|
|
|
|
|
|
|
|
page, err = strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to convert page: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
rankings, err := daily.Rankings(category, page)
|
|
|
|
case "/daily/rankingpagecount":
|
|
|
|
if err != nil {
|
|
|
|
var category int
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
return
|
|
|
|
var err error
|
|
|
|
}
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = json.NewEncoder(w).Encode(rankings)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
count, err := daily.RankingPageCount(category)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
var category int
|
|
|
|
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.Write([]byte(strconv.Itoa(count)))
|
|
|
|
count, err := daily.RankingPageCount(category)
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("unknown endpoint"), http.StatusNotFound)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
|
|
|
w.Write([]byte(strconv.Itoa(count)))
|
|
|
|
log.Printf("%s: %s\n", r.URL.Path, err)
|
|
|
|
|
|
|
|
http.Error(w, err.Error(), code)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|