From f690c536071e74c38a90c684658b613e7359f492 Mon Sep 17 00:00:00 2001 From: maru Date: Tue, 5 Dec 2023 13:28:08 -0500 Subject: [PATCH] Add changes so far --- api/account.go | 69 +++++++++++++++++++++++++++++++++++++++++++++ api/generic.go | 18 ++++++++++++ api/savedata.go | 30 ++++++++++++++++++++ db/account.go | 20 +++++++++++++ db/db.go | 20 +++++++++++++ pokerogue-server.go | 50 ++++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 api/account.go create mode 100644 api/generic.go create mode 100644 api/savedata.go create mode 100644 db/account.go create mode 100644 db/db.go create mode 100644 pokerogue-server.go diff --git a/api/account.go b/api/account.go new file mode 100644 index 0000000..e8ccba4 --- /dev/null +++ b/api/account.go @@ -0,0 +1,69 @@ +package api + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "net/http" + + "github.com/Flashfyre/pokerogue-server/db" +) + +// /api/account/info - get account info + +type AccountInfoResponse struct{ + Username string `json:"string"` +} + +func HandleAccountInfo(w http.ResponseWriter, r *http.Request) { + token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization")) + if err != nil { + http.Error(w, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest) + return + } + + username, err := db.GetAccountInfoFromToken(token) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + response, err := json.Marshal(AccountInfoResponse{Username: username}) + if err != nil { + http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + return + } + + w.Write(response) +} + +// /api/account/register - register account + +type AccountRegisterRequest GenericAuthRequest +type AccountRegisterResponse GenericAuthResponse + +func HandleAccountRegister(w http.ResponseWriter, r *http.Request) { + var request AccountRegisterRequest + err := json.NewDecoder(r.Body).Decode(&request) + if err != nil { + http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) + return + } + + +} + +// /api/account/login - log into account + +type AccountLoginRequest GenericAuthRequest +type AccountLoginResponse GenericAuthResponse + +func HandleAccountLogin(w http.ResponseWriter, r *http.Request) { + +} + +// /api/account/logout - log out of account + +func HandleAccountLogout(w http.ResponseWriter, r *http.Request) { + +} diff --git a/api/generic.go b/api/generic.go new file mode 100644 index 0000000..d09a4b0 --- /dev/null +++ b/api/generic.go @@ -0,0 +1,18 @@ +package api + +// error + +type ErrorResponse struct{ + Message string `json:"message"` +} + +// auth + +type GenericAuthRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type GenericAuthResponse struct { + Token string `json:"token"` +} diff --git a/api/savedata.go b/api/savedata.go new file mode 100644 index 0000000..174337d --- /dev/null +++ b/api/savedata.go @@ -0,0 +1,30 @@ +package api + +import "net/http" + +// /api/savedata/get - get save data + +type SavedataGetRequest struct{} +type SavedataGetResponse struct{} + +func HandleSavedataGet(w http.ResponseWriter, r *http.Request) { + +} + +// /api/savedata/update - update save data + +type SavedataUpdateRequest struct{} +type SavedataUpdateResponse struct{} + +func HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) { + +} + +// /api/savedata/delete - delete save date + +type SavedataDeleteRequest struct{} +type SavedataDeleteResponse struct{} + +func HandleSavedataDelete(w http.ResponseWriter, r *http.Request) { + +} diff --git a/db/account.go b/db/account.go new file mode 100644 index 0000000..e21098f --- /dev/null +++ b/db/account.go @@ -0,0 +1,20 @@ +package db + +import ( + "database/sql" + "fmt" +) + +func GetAccountInfoFromToken(token []byte) (string, error) { + var username string + err := handle.QueryRow("SELECT username FROM accounts WHERE uuid IN (SELECT uuid FROM sessions WHERE token = ? AND expire > UTC_TIMESTAMP())").Scan(&username) + if err != nil { + if err == sql.ErrNoRows { + return "", fmt.Errorf("invalid token") + } + + return "", fmt.Errorf("query failed: %s", err) + } + + return username, nil +} diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..4d88574 --- /dev/null +++ b/db/db.go @@ -0,0 +1,20 @@ +package db + +import ( + "database/sql" + "fmt" +) + +var handle *sql.DB + +func Init(username, password, protocol, address, database string) error { + db, err := sql.Open("mysql", username+":"+password+"@"+protocol+"("+address+")/"+database) + if err != nil { + return fmt.Errorf("failed to open database connection: %s", err) + } + + handle = db + + return nil +} + diff --git a/pokerogue-server.go b/pokerogue-server.go new file mode 100644 index 0000000..01683d2 --- /dev/null +++ b/pokerogue-server.go @@ -0,0 +1,50 @@ +package main + +import ( + "flag" + "log" + "net" + "net/http" + + "github.com/Flashfyre/pokerogue-server/api" + "github.com/Flashfyre/pokerogue-server/db" +) + +func main() { + network := flag.String("network", "tcp", "network type for server to listen on (tcp, unix)") + address := flag.String("address", "0.0.0.0", "network address for server to listen on") + + dbuser := flag.String("dbuser", "pokerogue", "database username") + dbpass := flag.String("dbpass", "", "database password") + dbproto := flag.String("dbproto", "tcp", "protocol for database connection") + dbaddr := flag.String("dbaddr", "127.0.0.1", "database address") + dbname := flag.String("dbname", "pokerogue", "database name") + + flag.Parse() + + err := db.Init(*dbuser, *dbpass, *dbproto, *dbaddr, *dbname) + if err != nil { + log.Fatalf("failed to initialize database: %s", err) + } + + listener, err := net.Listen(*network, *address) + if err != nil { + log.Fatalf("failed to create net listener: %s", err) + } + + // account + http.HandleFunc("/api/account/info", api.HandleAccountInfo) + http.HandleFunc("/api/account/register", api.HandleAccountRegister) + http.HandleFunc("/api/account/login", api.HandleAccountLogin) + http.HandleFunc("/api/account/logout", api.HandleAccountLogout) + + // savedata + http.HandleFunc("/api/savedata/get", api.HandleSavedataGet) + http.HandleFunc("/api/savedata/update", api.HandleSavedataUpdate) + http.HandleFunc("/api/savedata/delete", api.HandleSavedataDelete) + + err = http.Serve(listener, nil) + if err != nil { + log.Fatalf("failed to create http server or server errored: %s", err) + } +}