working on ron-example

This commit is contained in:
2024-11-20 23:09:25 +01:00
parent ea5d85bd19
commit 4f108e1b05
25 changed files with 627 additions and 147 deletions
+29 -9
View File
@@ -1,38 +1,58 @@
package handlers
import (
"context"
"fmt"
"log/slog"
"net/http"
"ron"
"ron-pets/internal/config"
"ron-pets/internal/repository"
"ron-pets/internal/sqlc"
)
type Handlers struct {
app *config.App
app *config.App
queries repository.ExtendedQuerier
}
func New(app *config.App) *Handlers {
func New(app *config.App, q repository.ExtendedQuerier) *Handlers {
return &Handlers{
app: app,
app: app,
queries: q,
}
}
func (hq *Handlers) HelloWorld(c *ron.Context) {
slog.Info("Dummy info message")
func (hq *Handlers) HelloWorld(c *ron.CTX, ctx context.Context) {
session, ok := ctx.Value("session").(*sqlc.SessionData)
if !ok || session == nil {
http.Error(c.W, "Unauthorized", http.StatusUnauthorized)
return
}
fmt.Fprintf(c.W, "User ID: %s, Role: %s", session.UserID, session.Role)
c.W.Write([]byte("hello world"))
}
func (hq *Handlers) AnotherHelloWorld(c *ron.Context) {
func (hq *Handlers) AnotherHelloWorld(c *ron.CTX) {
val := c.R.Context().Value("key")
//val := context.Background().Value("key")
slog.Info("context value", "value", val)
c.W.Write([]byte("another hello world"))
}
func (hq *Handlers) HelloWorldJSON(c *ron.Context) {
func (hq *Handlers) HelloWorldJSON(c *ron.CTX) {
id := c.R.PathValue("id")
slog.Info("path value", "id", id)
c.JSON(200, ron.Data{"message": "hello world"})
}
func (hq *Handlers) HelloWorldHTML(c *ron.Context) {
func (hq *Handlers) HelloWorldHTML(c *ron.CTX) {
//pages := ron.Pages{
// TotalElements: len(elements),
@@ -50,6 +70,6 @@ func (hq *Handlers) HelloWorldHTML(c *ron.Context) {
//c.HTML(200, "page.index.gohtml", td)
}
func (hq *Handlers) ComponentHTML(c *ron.Context) {
func (hq *Handlers) ComponentHTML(c *ron.CTX) {
c.HTML(200, "component.list.gohtml", nil)
}
+3 -3
View File
@@ -14,7 +14,7 @@ type UserPayload struct {
Role string `json:"role"`
}
func (hq *Handlers) CreateToken(c *ron.Context) {
func (hq *Handlers) CreateToken(c *ron.CTX) {
token := paseto.NewToken()
token.Set("userPayload", UserPayload{User: "pedro", Role: "admin"})
token.SetExpiration(time.Now().Add(hq.app.Security.Duration))
@@ -35,7 +35,7 @@ func (hq *Handlers) CreateToken(c *ron.Context) {
c.JSON(http.StatusOK, ron.Data{"token": signed})
}
func (hq *Handlers) ValidateTokenAuthorization(c *ron.Context) {
func (hq *Handlers) ValidateTokenAuthorization(c *ron.CTX) {
signed := c.R.Header.Get("Authorization")
split := strings.Split(signed, "Bearer ")
slog.Info("signed", "signed", split[1])
@@ -56,7 +56,7 @@ func (hq *Handlers) ValidateTokenAuthorization(c *ron.Context) {
})
}
func (hq *Handlers) ValidateTokenCookie(c *ron.Context) {
func (hq *Handlers) ValidateTokenCookie(c *ron.CTX) {
cookie, err := c.R.Cookie("token")
if err != nil {
slog.Error("error", "err", err)