feat: creation of new user
This commit is contained in:
		| @@ -1,17 +1,9 @@ | |||||||
| package session | package game | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" |  | ||||||
| 
 |  | ||||||
| 	petname "github.com/dustinkirkland/golang-petname" | 	petname "github.com/dustinkirkland/golang-petname" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type User struct { |  | ||||||
| 	Name       string |  | ||||||
| 	PublicKey  string |  | ||||||
| 	PrivateKey string |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| var lastSessionId int = 0 | var lastSessionId int = 0 | ||||||
| var sessionStorage []Session = []Session{} | var sessionStorage []Session = []Session{} | ||||||
| var nameToSession map[string]*Session = make(map[string]*Session) | var nameToSession map[string]*Session = make(map[string]*Session) | ||||||
| @@ -35,11 +27,13 @@ func NewSession() Session { | |||||||
| 	nameToSession[sessionName] = &s | 	nameToSession[sessionName] = &s | ||||||
| 	lastSessionId++ | 	lastSessionId++ | ||||||
| 
 | 
 | ||||||
| 	fmt.Println(sessionName) |  | ||||||
| 
 |  | ||||||
| 	return s | 	return s | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func GetSession(name string) *Session { | func GetSession(name string) *Session { | ||||||
| 	return nameToSession[name] | 	return nameToSession[name] | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (s *Session) AddUser(user User) { | ||||||
|  | 	s.Users = append(s.Users, user) | ||||||
|  | } | ||||||
							
								
								
									
										23
									
								
								internal/game/user.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								internal/game/user.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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, | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -3,11 +3,11 @@ package rest_handler | |||||||
| import ( | import ( | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  |  | ||||||
| 	"gitea.elara.ws/Hazel/hangman/internal/session" | 	"gitea.elara.ws/Hazel/hangman/internal/game" | ||||||
| 	"github.com/labstack/echo/v4" | 	"github.com/labstack/echo/v4" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func CreateSession(c echo.Context) error { | func CreateSession(c echo.Context) error { | ||||||
| 	s := session.NewSession() | 	s := game.NewSession() | ||||||
| 	return c.JSON(http.StatusOK, s) | 	return c.JSON(http.StatusOK, s) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										26
									
								
								internal/rest_handler/create_user.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								internal/rest_handler/create_user.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||||
|  | } | ||||||
							
								
								
									
										5
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.go
									
									
									
									
									
								
							| @@ -37,9 +37,10 @@ func main() { | |||||||
| 		templates: templates, | 		templates: templates, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	e.GET("/", view_handler.CreateSession) | 	e.POST("/api/session", rest_handler.CreateSession) | ||||||
| 	e.GET("/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.GET("/:name", view_handler.CreateUser) | ||||||
|  |  | ||||||
| 	e.Logger.Fatal(e.Start(":1323")) | 	e.Logger.Fatal(e.Start(":1323")) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user