back_end e front_end, registro, dados etc. '-'

This commit is contained in:
2025-06-18 00:25:32 -03:00
parent fc2c573111
commit 9a818eee5f
13 changed files with 315 additions and 102 deletions

Binary file not shown.

View File

@@ -39,6 +39,11 @@ func main() {
Addr: "127.0.0.1:6379",
})
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://192.168.1.102"},
AllowCredentials: true,
}))
log.Println("Definindo configurações de Rate Limit")
rateLimiterStore := middleware.NewRateLimiterMemoryStoreWithConfig(
middleware.RateLimiterMemoryStoreConfig{
@@ -82,6 +87,7 @@ func main() {
e.GET("/", online)
e.GET("roll", roll_get)
e.POST("roll", roll)
e.GET("register", badreq)
e.POST("register", registrar)
e.POST("login", login, timeoutMiddleware, rateLimit)

View File

@@ -5,10 +5,12 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/labstack/echo/v4"
@@ -32,7 +34,7 @@ func badreq(c echo.Context) error {
func registrar(c echo.Context) error {
player := c.FormValue("player")
password := c.FormValue("password")
confpass := c.FormValue("confirmpassowrd")
confpass := c.FormValue("confirmpassword")
playerlore := c.FormValue("playerlore")
if !regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString(player) {
@@ -198,6 +200,56 @@ func login(c echo.Context) error {
})
}
func roll(c echo.Context) error {
userSession, err := ValidSession(c)
if err != nil {
return c.JSON(http.StatusUnauthorized, resp_json{
Status: "Error",
Message: err.Error(),
})
}
roll, err := strconv.Atoi(c.FormValue("roll"))
if err != nil {
return c.JSON(http.StatusBadRequest, resp_json{
Status: "Error",
Message: "algo deu errado ao rola seu dado, tente novamente",
})
}
var dado int
switch roll {
case 6:
dado = rand.Intn(roll) + 1
case 10:
dado = rand.Intn(roll) + 1
case 20:
dado = rand.Intn(roll) + 1
case 40:
dado = rand.Intn(roll) + 1
case 50:
dado = rand.Intn(roll) + 1
case 100:
dado = rand.Intn(roll) + 1
default:
return c.JSON(http.StatusBadRequest, resp_json{
Status: "Error",
Message: "você so pode rodar dados de 6, 10, 20, 40, 50 ou 100",
})
}
var value string = fmt.Sprintf("%s rolou 1d%v e o resultado é %v", userSession["playerName"], roll, dado)
_, err = db.Exec("INSERT INTO roll_logs (id, log) VALUES (NULL, ?);", value)
if err != nil {
return c.JSON(http.StatusInternalServerError, resp_json{
Status: "Error",
Message: "não foi possivel inserir o log do da rolagem no banco de dados",
})
}
return c.JSON(http.StatusOK, resp_json{
Status: "Success",
Message: value,
})
}
func roll_get(c echo.Context) error {
row, err := db.Query("SELECT * FROM roll_logs ORDER BY id DESC LIMIT 5;")
if err != nil {