Compare commits

..

2 Commits

Author SHA1 Message Date
4a4e5830f6 Get URL params 2024-05-08 07:39:04 -05:00
eaabbafed6 Generate cuid2 & store 2024-05-08 07:25:52 -05:00
3 changed files with 64 additions and 8 deletions

7
go.mod
View File

@ -3,6 +3,9 @@ module git.araozu.dev/card-jong-be
go 1.22.2 go 1.22.2
require ( require (
github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/mux v1.8.1
github.com/rs/cors v1.11.0 // indirect github.com/nrednav/cuid2 v1.0.0
github.com/rs/cors v1.11.0
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/sys v0.20.0 // indirect
) )

6
go.sum
View File

@ -1,4 +1,10 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/nrednav/cuid2 v1.0.0 h1:27dn1oGiG+23Wa8XJ2DHeMoMa18Zs9u1+UHI9IlcGKM=
github.com/nrednav/cuid2 v1.0.0/go.mod h1:pdRH5Zrjwnv8DZ74XvHR3jX+bzJNfQjwLQ3JgSI2EmI=
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po= github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

61
main.go
View File

@ -1,19 +1,32 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url"
"os" "os"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/nrednav/cuid2"
"github.com/rs/cors" "github.com/rs/cors"
) )
type PersonInfo struct {
UserId string
Username string
}
var users map[string]string
func main() { func main() {
fmt.Println("hello SEKAI!!") fmt.Println("hello SEKAI!!")
router := mux.NewRouter().PathPrefix("/api").Subrouter() router := mux.NewRouter().PathPrefix("/api").Subrouter()
// initialize the global users map
users = make(map[string]string)
router.HandleFunc("/register", Register) router.HandleFunc("/register", Register)
port, ok := os.LookupEnv("PORT") port, ok := os.LookupEnv("PORT")
@ -33,13 +46,47 @@ func main() {
log.Fatal(http.ListenAndServe(":"+port, handler)) log.Fatal(http.ListenAndServe(":"+port, handler))
} }
type PersonInfo struct { func Register(writer http.ResponseWriter, request *http.Request) {
useruuid int requestUrl := request.URL
username string params, err := url.ParseQuery(requestUrl.RawQuery)
if err != nil {
fmt.Printf("Error parsing URL parameters: %s\n", err)
writer.WriteHeader(http.StatusInternalServerError)
fmt.Printf("{\"error\": \"%s\"}", err)
return
} }
func Register(writer http.ResponseWriter, request *http.Request) { usernameArr, ok := params["username"]
writer.Header().Set("Content-Type", "application/json") if !ok {
writer.WriteHeader(http.StatusOK) fmt.Printf("Error parsing URL parameters: %s\n", err)
fmt.Fprintf(writer, "{\"yes\":\"and\"}") writer.WriteHeader(http.StatusInternalServerError)
fmt.Printf("{\"error\": \"%s\"}", err)
return
}
username := usernameArr[0]
uid := cuid2.Generate()
// Store in the users map
users[uid] = username
// The result json
result := PersonInfo{
UserId: uid,
Username: username,
}
writer.Header().Set("Content-Type", "application/json")
jsonData, err := json.Marshal(result)
if err != nil {
fmt.Printf("Error in JSON marshal: %s\n", err)
writer.WriteHeader(http.StatusInternalServerError)
fmt.Printf("{\"error\": \"%s\"}", err)
return
}
writer.WriteHeader(http.StatusOK)
fmt.Fprintf(writer, "%s", jsonData)
} }