From f800cafb1d0bfa31d312e830b0151b47380fb290 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 26 Jul 2023 22:09:31 +0100 Subject: [PATCH] chore(backend): polish things up a little --- backend/main.go | 11 ++++++++++- backend/user.go | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/backend/main.go b/backend/main.go index 8a1f6ab..787b9d0 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "net/http" "os" @@ -10,19 +11,26 @@ type App struct { UserHandler *UserHandler } +// Define the serve function func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) { var head string head, req.URL.Path = ShiftPath(req.URL.Path) switch head { + // Start the user handler should the requested user be found case "user": h.UserHandler.Handle(res, req) + // Return a `Not Found` if the user is not found default: http.Error(res, "Not Found", http.StatusNotFound) } } +// Run the server func main() { + // Initialise the user handler user_handler, err := NewUserHandler() + + // Log any errors if err != nil { log.Fatalln(err) } @@ -35,6 +43,7 @@ func main() { if port == "" { port = "7741" } - log.Println("Ambition going strong at port 7741") + // Log that the program has successfully started listening to the port + log.Println(fmt.Sprintf("Ambition backend listening to port %v", port)) http.ListenAndServe(":"+port, a) } diff --git a/backend/user.go b/backend/user.go index 8d6db05..8cc1ea0 100644 --- a/backend/user.go +++ b/backend/user.go @@ -1,10 +1,12 @@ package main import ( + // The standard stuff "errors" "fmt" "io" + // Encryption "crypto/ecdsa" "crypto/elliptic" "crypto/rand" @@ -14,31 +16,39 @@ import ( "github.com/golang-jwt/jwt" "golang.org/x/crypto/bcrypt" + // SQL databasing "database/sql" _ "github.com/mattn/go-sqlite3" ) +// Define the user handler struct type UserHandler struct { db *sql.DB jwt_secret *ecdsa.PrivateKey } +// Define the user request struct type UserRequest struct { Name string `json:"name"` Password string `json:"password"` } +// Define the function to create user handlers func NewUserHandler() (*UserHandler, error) { + // Initialise the database using the database file db, err := sql.Open("sqlite3", "users.db") if err != nil { return nil, err } + // Define the JSON web token jwt_secret, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + // Return any errors if err != nil { return nil, err } + // Return the user handler struct return &UserHandler{ db: db, jwt_secret: jwt_secret, @@ -53,6 +63,7 @@ func (h *UserHandler) Handle(res http.ResponseWriter, req *http.Request) { h.updateUser(res, req) case "DELETE": h.deleteUser(res, req) + // Return an error message should an invalid method be used default: http.Error(res, "Only POST, PUT, and DELETE are valid methods", http.StatusMethodNotAllowed) } @@ -78,14 +89,14 @@ func (h *UserHandler) createUser(res http.ResponseWriter, req *http.Request) { password := []byte(user_request.Password) // Password checks - // ------------------- row := h.db.QueryRow("SELECT pwdhash FROM users WHERE name=?", name) var db_pwdhash string if err = row.Scan(&db_pwdhash); err != nil { - // If no user found with name, create the user + // If no user is found with the requested name, create the user if errors.Is(err, sql.ErrNoRows) { pwdhash_bytes, err := bcrypt.GenerateFromPassword(password, 12) + // Log any errors if err != nil { http.Error(res, fmt.Sprintf("user: failed to generate password hash (%s)", err), http.StatusInternalServerError) return