Add debug mode bool

pull/1/head
maru 2023-12-29 14:30:47 -05:00
parent cd2118dfec
commit 1dee0cbc3a
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D
4 changed files with 45 additions and 19 deletions

View File

@ -29,7 +29,7 @@ type AccountInfoResponse struct{
Username string `json:"string"` Username string `json:"string"`
} }
func HandleAccountInfo(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleAccountInfo(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") == "" { if r.Header.Get("Authorization") == "" {
http.Error(w, "missing token", http.StatusBadRequest) http.Error(w, "missing token", http.StatusBadRequest)
return return
@ -60,7 +60,7 @@ func HandleAccountInfo(w http.ResponseWriter, r *http.Request) {
type AccountRegisterRequest GenericAuthRequest type AccountRegisterRequest GenericAuthRequest
func HandleAccountRegister(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleAccountRegister(w http.ResponseWriter, r *http.Request) {
var request AccountRegisterRequest var request AccountRegisterRequest
err := json.NewDecoder(r.Body).Decode(&request) err := json.NewDecoder(r.Body).Decode(&request)
if err != nil { if err != nil {
@ -108,7 +108,7 @@ func HandleAccountRegister(w http.ResponseWriter, r *http.Request) {
type AccountLoginRequest GenericAuthRequest type AccountLoginRequest GenericAuthRequest
type AccountLoginResponse GenericAuthResponse type AccountLoginResponse GenericAuthResponse
func HandleAccountLogin(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleAccountLogin(w http.ResponseWriter, r *http.Request) {
var request AccountLoginRequest var request AccountLoginRequest
err := json.NewDecoder(r.Body).Decode(&request) err := json.NewDecoder(r.Body).Decode(&request)
if err != nil { if err != nil {
@ -167,7 +167,7 @@ func HandleAccountLogin(w http.ResponseWriter, r *http.Request) {
// /api/account/logout - log out of account // /api/account/logout - log out of account
func HandleAccountLogout(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleAccountLogout(w http.ResponseWriter, r *http.Request) {
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization")) token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest) http.Error(w, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest)

View File

@ -1,5 +1,40 @@
package api package api
import "net/http"
type Server struct {
Debug bool
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.Debug {
w.Header().Add("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
}
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)
}
}
// auth // auth
type GenericAuthRequest struct { type GenericAuthRequest struct {

View File

@ -7,7 +7,7 @@ import "net/http"
type SavedataGetRequest struct{} type SavedataGetRequest struct{}
type SavedataGetResponse struct{} type SavedataGetResponse struct{}
func HandleSavedataGet(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleSavedataGet(w http.ResponseWriter, r *http.Request) {
} }
@ -16,7 +16,7 @@ func HandleSavedataGet(w http.ResponseWriter, r *http.Request) {
type SavedataUpdateRequest struct{} type SavedataUpdateRequest struct{}
type SavedataUpdateResponse struct{} type SavedataUpdateResponse struct{}
func HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
} }
@ -25,6 +25,6 @@ func HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
type SavedataDeleteRequest struct{} type SavedataDeleteRequest struct{}
type SavedataDeleteResponse struct{} type SavedataDeleteResponse struct{}
func HandleSavedataDelete(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleSavedataDelete(w http.ResponseWriter, r *http.Request) {
} }

View File

@ -12,6 +12,8 @@ import (
) )
func main() { func main() {
debug := flag.Bool("debug", false, "debug mode")
proto := flag.String("proto", "tcp", "protocol for api to use (tcp, unix)") proto := flag.String("proto", "tcp", "protocol for api to use (tcp, unix)")
addr := flag.String("addr", "0.0.0.0", "network address for api to listen on") addr := flag.String("addr", "0.0.0.0", "network address for api to listen on")
@ -41,18 +43,7 @@ func main() {
os.Chmod(*addr, 0777) os.Chmod(*addr, 0777)
} }
// account err = http.Serve(listener, &api.Server{Debug: *debug})
http.HandleFunc("/account/info", api.HandleAccountInfo)
http.HandleFunc("/account/register", api.HandleAccountRegister)
http.HandleFunc("/account/login", api.HandleAccountLogin)
http.HandleFunc("/account/logout", api.HandleAccountLogout)
// savedata
http.HandleFunc("/savedata/get", api.HandleSavedataGet)
http.HandleFunc("/savedata/update", api.HandleSavedataUpdate)
http.HandleFunc("/savedata/delete", api.HandleSavedataDelete)
err = http.Serve(listener, nil)
if err != nil { if err != nil {
log.Fatalf("failed to create http server or server errored: %s", err) log.Fatalf("failed to create http server or server errored: %s", err)
} }