DEVELOPMENT ENVIRONMENT

~liljamo/felu

cecbccc31b01b8db9a924c2852227e1cd2f246ae — Jonni Liljamo 1 year, 1 month ago d2a556d
refactor: move more to handlers
M cmd/felu/main.go => cmd/felu/main.go +9 -21
@@ 13,7 13,6 @@ import (
	"strconv"
	"time"

	"git.src.quest/~skye/felu-ddns/internal/components"
	"git.src.quest/~skye/felu-ddns/internal/config"
	"git.src.quest/~skye/felu-ddns/internal/db"
	"git.src.quest/~skye/felu-ddns/internal/dns"


@@ 64,13 63,8 @@ func setupFrontendRouter() *gin.Engine {
	r.Static("/static", "./static")
	r.HTMLRender = &renderer.TemplRender{}

	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.Index())
	})

	r.GET("/login", middlewares.SessionExists(sessionManager), func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.Login())
	})
	r.GET("/", handlers.Index())
	r.GET("/login", middlewares.SessionExists(sessionManager), handlers.Login())

	auth := r.Group("/auth")
	{


@@ 78,25 72,19 @@ func setupFrontendRouter() *gin.Engine {
	}

	// routes for the actual application frontend
	manage := r.Group("/manage").Use(middlewares.SessionExists(sessionManager))
	manage := r.Group("/manage", middlewares.SessionExists(sessionManager))
	{
		manage.GET("/", func(c *gin.Context) {
			c.HTML(http.StatusOK, "", components.Manage()) //TODO: yeah move these to handlers/manage.go
		})
		manage.GET("/settings", func(c *gin.Context) {
			c.HTML(http.StatusOK, "", components.ManageSettings())
		})
		manage.GET("/", handlers.Manage())
		manage.GET("/settings", handlers.ManageSettings())

		manage.POST("/domains", handlers.PostDomain()) //TODO: move these to handlers/domains.go
		manage.POST("/domains", handlers.PostDomain())
		manage.PATCH("/domains/:id") // TODO:
		manage.DELETE("/domains/:id", handlers.DeleteDomain())

		manage.GET("/partials/domains", handlers.ManagePartialDomains())
	}
	manageAdmin := r.Group("/manage/admin").Use(
		middlewares.SessionExists(sessionManager),
		middlewares.AdminOnly(),
	)
	manageAdmin := r.Group("/manage/admin",
		middlewares.SessionExists(sessionManager), middlewares.AdminOnly())
	{
		manageAdmin.GET("/", handlers.ManageAdmin())
		manageAdmin.GET("/users", handlers.ManageAdminUsers())


@@ 104,7 92,7 @@ func setupFrontendRouter() *gin.Engine {

		manageAdmin.POST("/users", handlers.PostUser())

		manageAdmin.GET("/partials/users_list", handlers.AdminPartialsUsersList())
		manageAdmin.GET("/partials/users_list", handlers.AdminPartialUsersList())
	}

	return r

M internal/components/adminpartials.templ => internal/components/adminpartials.templ +1 -1
@@ 3,7 3,7 @@ package components
import "git.src.quest/~skye/felu-ddns/internal/db"
import "strconv"

templ AdminPartialsUsersList(users []db.User) {
templ AdminPartialUsersList(users []db.User) {
	<table class="table-auto">
		<thead>
			<tr>

M internal/components/adminpartials_templ.go => internal/components/adminpartials_templ.go +1 -1
@@ 12,7 12,7 @@ import "bytes"
import "git.src.quest/~skye/felu-ddns/internal/db"
import "strconv"

func AdminPartialsUsersList(users []db.User) templ.Component {
func AdminPartialUsersList(users []db.User) templ.Component {
	return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
		templBuffer, templIsBuffer := w.(*bytes.Buffer)
		if !templIsBuffer {

M internal/handlers/adminpartials.go => internal/handlers/adminpartials.go +2 -2
@@ 14,7 14,7 @@ import (
	"github.com/gin-gonic/gin"
)

func AdminPartialsUsersList() gin.HandlerFunc {
func AdminPartialUsersList() gin.HandlerFunc {
	return func(c *gin.Context) {
		users, err := db.FetchAllUsers()
		if err != nil {


@@ 22,7 22,7 @@ func AdminPartialsUsersList() gin.HandlerFunc {
			c.String(http.StatusInternalServerError, "Something went wrong while fetching users")
			c.Abort()
		} else {
			c.HTML(http.StatusOK, "", components.AdminPartialsUsersList(users))
			c.HTML(http.StatusOK, "", components.AdminPartialUsersList(users))
		}
	}
}

A internal/handlers/auth.go => internal/handlers/auth.go +42 -0
@@ 0,0 1,42 @@
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package handlers

import (
	"log"
	"net/http"

	"git.src.quest/~skye/felu-ddns/internal/db"
	"github.com/alexedwards/scs/v2"
	"github.com/gin-gonic/gin"
)

type postAuthLoginData struct {
	Email    string `form:"email"`
	Password string `form:"password"`
}

func AuthLogin(sm *scs.SessionManager) gin.HandlerFunc {
	return func(c *gin.Context) {
		data := &postAuthLoginData{}
		if err := c.Bind(data); err != nil {
			log.Printf("[felu] ERROR: Could not bind login details: %v", err)
			c.String(http.StatusBadRequest, "Could not bind login details")
			return
		}

		user, err := db.FetchUserWithCreds(data.Email, data.Password)
		if err != nil {
			c.String(http.StatusUnauthorized, err.Error())
			return
		}

		sm.Put(c.Request.Context(), "user_id", user.Id)

		c.Header("HX-Redirect", "/manage")
	}
}

R internal/handlers/managedomains.go => internal/handlers/domains.go +0 -0
A internal/handlers/index.go => internal/handlers/index.go +20 -0
@@ 0,0 1,20 @@
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package handlers

import (
	"net/http"

	"git.src.quest/~skye/felu-ddns/internal/components"
	"github.com/gin-gonic/gin"
)

func Index() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.Index())
	}
}

M internal/handlers/login.go => internal/handlers/login.go +3 -26
@@ 7,37 7,14 @@
package handlers

import (
	"log"
	"net/http"

	"git.src.quest/~skye/felu-ddns/internal/db"
	"github.com/alexedwards/scs/v2"
	"git.src.quest/~skye/felu-ddns/internal/components"
	"github.com/gin-gonic/gin"
)

type postLoginDetails struct {
	Email    string `form:"email"`
	Password string `form:"password"`
}

func AuthLogin(sm *scs.SessionManager) gin.HandlerFunc {
func Login() gin.HandlerFunc {
	return func(c *gin.Context) {
		data := &postLoginDetails{}
		if err := c.Bind(data); err != nil {
			log.Printf("[felu] ERROR: Could not bind login details: %v", err)
			c.String(http.StatusBadRequest, "Could not bind login details")
			return
		}

		user, err := db.FetchUserWithCreds(data.Email, data.Password)
		if err != nil {
			c.String(http.StatusUnauthorized, err.Error())
			return
		}

		sm.Put(c.Request.Context(), "user_id", user.Id)

		c.Header("HX-Redirect", "/manage")
		//c.Redirect(http.StatusFound, "/manage")
		c.HTML(http.StatusOK, "", components.Login())
	}
}

A internal/handlers/manage.go => internal/handlers/manage.go +26 -0
@@ 0,0 1,26 @@
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package handlers

import (
	"net/http"

	"git.src.quest/~skye/felu-ddns/internal/components"
	"github.com/gin-gonic/gin"
)

func Manage() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.Manage())
	}
}

func ManageSettings() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.HTML(http.StatusOK, "", components.ManageSettings())
	}
}