Compare commits

..

No commits in common. "4a4e5830f601e6abafb091f157b367fca4a46663" and "3ce6b96597c4460a35ecfaa5715daae82dc48279" have entirely different histories.

3 changed files with 11 additions and 67 deletions

7
go.mod
View File

@ -3,9 +3,6 @@ module git.araozu.dev/card-jong-be
go 1.22.2 go 1.22.2
require ( require (
github.com/gorilla/mux v1.8.1 github.com/gorilla/mux v1.8.1 // indirect
github.com/nrednav/cuid2 v1.0.0 github.com/rs/cors v1.11.0 // indirect
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,10 +1,4 @@
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=

65
main.go
View File

@ -1,32 +1,19 @@
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")
@ -46,47 +33,13 @@ func main() {
log.Fatal(http.ListenAndServe(":"+port, handler)) log.Fatal(http.ListenAndServe(":"+port, handler))
} }
func Register(writer http.ResponseWriter, request *http.Request) { type PersonInfo struct {
requestUrl := request.URL useruuid int
params, err := url.ParseQuery(requestUrl.RawQuery) username string
if err != nil { }
fmt.Printf("Error parsing URL parameters: %s\n", err)
writer.WriteHeader(http.StatusInternalServerError) func Register(writer http.ResponseWriter, request *http.Request) {
fmt.Printf("{\"error\": \"%s\"}", err) writer.Header().Set("Content-Type", "application/json")
return writer.WriteHeader(http.StatusOK)
} fmt.Fprintf(writer, "{\"yes\":\"and\"}")
usernameArr, ok := params["username"]
if !ok {
fmt.Printf("Error parsing URL parameters: %s\n", err)
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)
} }