feat: introduce controller layer for application logic

This commit is contained in:
2025-05-09 13:21:59 -04:00
parent 55ae55108e
commit a824fa90a5
16 changed files with 193 additions and 16 deletions

View File

@@ -4,24 +4,20 @@
package main
import (
"github.com/sonr-io/motr/handlers/auth"
"github.com/sonr-io/motr/handlers/landing"
"github.com/sonr-io/motr/controller"
"github.com/sonr-io/motr/internal/config"
"github.com/sonr-io/motr/internal/middleware"
"github.com/sonr-io/motr/routes"
)
func main() {
// Setup config
e, c := config.New()
e.Use(middleware.UseSession(c), middleware.UseCloudflareCache(c))
// Register controllers
if err := landing.Register(c, e); err != nil {
cn, err := controller.New(c, e)
if err != nil {
panic(err)
}
if err := auth.Register(c, e); err != nil {
panic(err)
}
// Start server
routes.SetupRoutes(cn)
e.Serve()
}

View File

@@ -4,21 +4,20 @@
package main
import (
"github.com/sonr-io/motr/handlers/auth"
"github.com/sonr-io/motr/controller"
"github.com/sonr-io/motr/internal/config"
"github.com/sonr-io/motr/internal/middleware"
"github.com/sonr-io/motr/routes"
)
func main() {
// Setup config
e, c := config.New()
e.Use(middleware.UseSession(c), middleware.UseCloudflareCache(c))
// Register controllers
if err := auth.Register(c, e); err != nil {
cn, err := controller.New(c, e)
if err != nil {
panic(err)
}
// Start server
routes.SetupRoutes(cn)
e.Serve()
}

View File

@@ -1 +1,37 @@
//go:build js && wasm
// +build js,wasm
package controller
import (
"github.com/sonr-io/motr/internal/config"
"github.com/sonr-io/motr/internal/sink/models"
"github.com/syumai/workers/cloudflare/kv"
)
type Controller struct {
DB models.Querier
Handles *kv.Namespace
Sessions *kv.Namespace
Server *config.Server
}
func New(cfg config.Config, s *config.Server) (*Controller, error) {
q, err := cfg.DB.GetQuerier()
if err != nil {
return nil, err
}
hkv, err := cfg.KV.GetHandles()
if err != nil {
return nil, err
}
skv, err := cfg.KV.GetSessions()
if err != nil {
return nil, err
}
return create(q, hkv, skv, s), nil
}
func create(q models.Querier, hkv *kv.Namespace, skv *kv.Namespace, srv *config.Server) *Controller {
return &Controller{DB: q, Handles: hkv, Sessions: skv, Server: srv}
}

View File

@@ -1,6 +1,6 @@
package vault
import "github.com/sonr-io/motr/controllers/credential"
import "github.com/sonr-io/motr/controller/credential"
type LoginOptions struct {
Account string

View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleDefaultExpired(c echo.Context) error {
return middleware.Render(c, views.HomeView())
}

View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleDefaultInitial(c echo.Context) error {
return middleware.Render(c, views.HomeView())
}

11
handlers/default_valid.go Normal file
View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleDefaultValid(c echo.Context) error {
return middleware.Render(c, views.HomeView())
}

11
handlers/login_check.go Normal file
View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleLoginCheck(c echo.Context) error {
return middleware.Render(c, views.LoginView())
}

11
handlers/login_finish.go Normal file
View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleLoginFinish(c echo.Context) error {
return middleware.Render(c, views.LoginView())
}

11
handlers/login_initial.go Normal file
View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleLoginInitial(c echo.Context) error {
return middleware.Render(c, views.LoginView())
}

11
handlers/login_start.go Normal file
View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleLoginStart(c echo.Context) error {
return middleware.Render(c, views.LoginView())
}

View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleRegisterCheck(c echo.Context) error {
return middleware.Render(c, views.RegisterView())
}

View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleRegisterFinish(c echo.Context) error {
return middleware.Render(c, views.RegisterView())
}

View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleRegisterInitial(c echo.Context) error {
return middleware.Render(c, views.RegisterView())
}

View File

@@ -0,0 +1,11 @@
package handlers
import (
"github.com/labstack/echo/v4"
"github.com/sonr-io/motr/components/views"
"github.com/sonr-io/motr/internal/middleware"
)
func HandleRegisterStart(c echo.Context) error {
return middleware.Render(c, views.RegisterView())
}

25
routes/routes.go Normal file
View File

@@ -0,0 +1,25 @@
//go:build js && wasm
// +build js,wasm
package routes
import (
"github.com/sonr-io/motr/controller"
"github.com/sonr-io/motr/handlers"
)
func SetupRoutes(c *controller.Controller) {
c.Server.GET("/", handlers.HandleDefaultInitial)
c.Server.GET("/expired", handlers.HandleDefaultExpired)
c.Server.GET("/valid", handlers.HandleDefaultValid)
c.Server.GET("/login", handlers.HandleLoginInitial)
c.Server.GET("/login/:handle", handlers.HandleLoginStart)
c.Server.POST("/login/:handle/check", handlers.HandleLoginCheck)
c.Server.POST("/login/:handle/finish", handlers.HandleLoginFinish)
c.Server.GET("/register", handlers.HandleRegisterInitial)
c.Server.GET("/register/:handle", handlers.HandleRegisterStart)
c.Server.POST("/register/:handle/check", handlers.HandleRegisterCheck)
c.Server.POST("/register/:handle/finish", handlers.HandleRegisterFinish)
}