diff --git a/internal/session/session.go b/internal/game/session.go similarity index 82% rename from internal/session/session.go rename to internal/game/session.go index 07b7e1a..ec14cc9 100644 --- a/internal/session/session.go +++ b/internal/game/session.go @@ -1,17 +1,9 @@ -package session +package game import ( - "fmt" - petname "github.com/dustinkirkland/golang-petname" ) -type User struct { - Name string - PublicKey string - PrivateKey string -} - var lastSessionId int = 0 var sessionStorage []Session = []Session{} var nameToSession map[string]*Session = make(map[string]*Session) @@ -35,11 +27,13 @@ func NewSession() Session { nameToSession[sessionName] = &s lastSessionId++ - fmt.Println(sessionName) - return s } func GetSession(name string) *Session { return nameToSession[name] } + +func (s *Session) AddUser(user User) { + s.Users = append(s.Users, user) +} diff --git a/internal/game/user.go b/internal/game/user.go new file mode 100644 index 0000000..2ff18bc --- /dev/null +++ b/internal/game/user.go @@ -0,0 +1,23 @@ +package game + +import ( + "crypto/ed25519" + "crypto/rand" +) + +type User struct { + Name string + PublicKey ed25519.PublicKey + PrivateKey ed25519.PrivateKey +} + +func NewUser(name string) User { + // ed25519 + + public, private, _ := ed25519.GenerateKey(rand.Reader) + return User{ + Name: name, + PublicKey: public, + PrivateKey: private, + } +} diff --git a/internal/rest_handler/create_session.go b/internal/rest_handler/create_session.go index e091f85..d9cc38f 100644 --- a/internal/rest_handler/create_session.go +++ b/internal/rest_handler/create_session.go @@ -3,11 +3,11 @@ package rest_handler import ( "net/http" - "gitea.elara.ws/Hazel/hangman/internal/session" + "gitea.elara.ws/Hazel/hangman/internal/game" "github.com/labstack/echo/v4" ) func CreateSession(c echo.Context) error { - s := session.NewSession() + s := game.NewSession() return c.JSON(http.StatusOK, s) } diff --git a/internal/rest_handler/create_user.go b/internal/rest_handler/create_user.go new file mode 100644 index 0000000..7754238 --- /dev/null +++ b/internal/rest_handler/create_user.go @@ -0,0 +1,26 @@ +package rest_handler + +import ( + "net/http" + + "gitea.elara.ws/Hazel/hangman/internal/game" + "github.com/labstack/echo/v4" +) + +func CreateUser(c echo.Context) error { + session := game.GetSession(c.Param("session")) + + type BodyContent struct { + Name string + } + var bodyContent BodyContent + + err := c.Bind(&bodyContent) + if err != nil { + return c.String(http.StatusBadRequest, err.Error()) + } + + user := game.NewUser(bodyContent.Name) + session.AddUser(user) + return c.JSON(http.StatusOK, user) +} diff --git a/main.go b/main.go index 66f308a..f634b47 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,10 @@ func main() { templates: templates, } - e.GET("/", view_handler.CreateSession) - e.GET("/api/session", rest_handler.CreateSession) + e.POST("/api/session", rest_handler.CreateSession) + e.POST("/api/:session/user", rest_handler.CreateUser) + e.GET("/", view_handler.CreateSession) e.GET("/:name", view_handler.CreateUser) e.Logger.Fatal(e.Start(":1323"))