diff --git a/cmd/api/main.go b/cmd/api/main.go new file mode 100644 index 0000000..36a5dc5 --- /dev/null +++ b/cmd/api/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "elTesto/internal/database" + "elTesto/internal/routes" + + "github.com/gin-gonic/gin" +) + +func main() { + database.ConnectDB() + router := gin.Default() + routes.SetupRoutes(router) + router.Run(":8080") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2f402c8 --- /dev/null +++ b/go.mod @@ -0,0 +1,42 @@ +module elTesto + +go 1.18 + +require ( + github.com/gin-gonic/gin v1.8.1 + github.com/go-sql-driver/mysql v1.6.0 + github.com/golang-jwt/jwt/v4 v4.4.2 + github.com/joho/godotenv v1.4.0 + golang.org/x/crypto v0.0.0-20220622213112-05c28c697c88 +) + +require ( + github.com/a-h/templ v0.2.476 // indirect + github.com/a-h/lexical v0.3.1 // indirect + github.com/a-h/segment v0.2.0 // indirect + github.com/a-h/segment/v2 v2.0.0 // indirect + github.com/a-h/z v0.2.0 // indirect + github.com/a-h/z/v2 v2.0.0 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.11.1 // indirect + github.com/goccy/go-json v0.9.0 // indirect + github.com/golang-migrate/migrate/v4 v4.15.2 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/jmoiron/sqlx v1.3.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/lib/pq v1.10.6 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.5 // indirect + github.com/ugorji/go/codec v1.2.7 // indirect + golang.org/x/net v0.0.0-20220722155251-1b3c860b8c63 // indirect + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect + golang.org/x/text v0.3.7 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) \ No newline at end of file diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d8d3565 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8= +github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= diff --git a/internal/handlers/views.go b/internal/handlers/views.go new file mode 100644 index 0000000..35d5025 --- /dev/null +++ b/internal/handlers/views.go @@ -0,0 +1,16 @@ +package handlers + +import ( + "elTesto/internal/views/templates" + + "github.com/gin-gonic/gin" +) + +func ShowUsers(c *gin.Context) { + users := []map[string]interface{}{ + {"id": 1, "username": "usuario1", "role": "admin"}, + {"id": 2, "username": "usuario2", "role": "user"}, + } + + templates.Users(users).Render(c.Request.Context(), c.Writer) +} diff --git a/internal/routes/routes.go b/internal/routes/routes.go new file mode 100644 index 0000000..e2a6e55 --- /dev/null +++ b/internal/routes/routes.go @@ -0,0 +1,21 @@ +package routes + +import ( + "elTesto/internal/handlers" + "elTesto/internal/middlewares" + + "github.com/gin-gonic/gin" +) + +func SetupRoutes(router *gin.Engine) { + // Configura el directorio de archivos estáticos + router.Static("/static", "./internal/views/static") + + userRoutes := router.Group("/users") + { + userRoutes.POST("/register", handlers.RegisterUser) + userRoutes.POST("/login", handlers.LoginUser) + userRoutes.GET("/", middlewares.AuthMiddleware, handlers.GetUsers) + userRoutes.GET("/view", handlers.ShowUsers) + } +} diff --git a/internal/views/static/css/style.css b/internal/views/static/css/style.css new file mode 100644 index 0000000..9809e6b --- /dev/null +++ b/internal/views/static/css/style.css @@ -0,0 +1,10 @@ +table { + border-collapse: collapse; + width: 100%; +} + +th, td { + border: 1px solid black; + padding: 8px; + text-align: left; +} \ No newline at end of file diff --git a/internal/views/templates/users.templ b/internal/views/templates/users.templ new file mode 100644 index 0000000..4f8a238 --- /dev/null +++ b/internal/views/templates/users.templ @@ -0,0 +1,32 @@ +package templates + +templ Users(users []map[string]interface{}) { + + + + Lista de Usuarios + + + +

Lista de Usuarios

+ + + + + + + + + + for _, user := range users { + + + + + + } + +
IDUsuarioRol
{ user["id"] }{ user["username"] }{ user["role"] }
+ + +} \ No newline at end of file diff --git a/internal/views/templates/users_templ.go b/internal/views/templates/users_templ.go new file mode 100644 index 0000000..857146f --- /dev/null +++ b/internal/views/templates/users_templ.go @@ -0,0 +1,89 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.3.856 +package templates + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +func Users(users []map[string]interface{}) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "Lista de Usuarios

Lista de Usuarios

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, user := range users { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "
IDUsuarioRol
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var2 string + templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(user["id"]) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `users.templ`, Line: 23, Col: 40} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(user["username"]) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `users.templ`, Line: 24, Col: 46} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(user["role"]) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `users.templ`, Line: 25, Col: 42} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +var _ = templruntime.GeneratedTemplate