feat/views (#8)

* chore: relocate controller package to sink directory

* refactor: adjust vault package to use updated credential location

* feat: implement session-based context for request handling

* refactor: relocate controllers to middleware directory

* fix: correct file paths in templ error messages

* refactor: adjust vault controller to new credential location
This commit is contained in:
2025-05-11 11:04:30 -04:00
committed by GitHub
parent 427c887ed3
commit 8540fb5dd1
47 changed files with 68 additions and 85 deletions

View File

@@ -5,7 +5,6 @@ package main
import (
"github.com/sonr-io/motr/config"
"github.com/sonr-io/motr/controller"
"github.com/sonr-io/motr/middleware"
"github.com/sonr-io/motr/routes"
)
@@ -13,11 +12,14 @@ import (
func main() {
// Setup config
e, c := config.New()
e.Use(middleware.UseSession(c), middleware.UseCloudflareCache(c))
cn, err := controller.New(c, e)
if err != nil {
panic(err)
}
routes.SetupRoutes(cn)
// Setup middleware
e.Use(
middleware.UseSession(c),
middleware.UseCloudflareCache(c),
)
// Setup routes
routes.SetupRoutes(e)
e.Serve()
}

View File

@@ -5,7 +5,6 @@ package main
import (
"github.com/sonr-io/motr/config"
"github.com/sonr-io/motr/controller"
"github.com/sonr-io/motr/middleware"
"github.com/sonr-io/motr/routes"
)
@@ -14,10 +13,6 @@ func main() {
// Setup config
e, c := config.New()
e.Use(middleware.UseSession(c), middleware.UseCloudflareCache(c))
cn, err := controller.New(c, e)
if err != nil {
panic(err)
}
routes.SetupRoutes(cn)
routes.SetupRoutes(e)
e.Serve()
}

View File

@@ -1,37 +0,0 @@
//go:build js && wasm
// +build js,wasm
package controller
import (
"github.com/sonr-io/motr/config"
"github.com/sonr-io/motr/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

@@ -6,6 +6,6 @@ import (
"github.com/sonr-io/motr/middleware"
)
func HandleDefaultInitial(c echo.Context) error {
func HandleDefaultIndex(c echo.Context) error {
return middleware.Render(c, views.HomeView())
}

View File

@@ -10,21 +10,29 @@ import (
"github.com/segmentio/ksuid"
"github.com/sonr-io/motr/config"
"github.com/sonr-io/motr/sink"
"github.com/sonr-io/motr/sink/models"
"github.com/syumai/workers/cloudflare/kv"
)
type SessionContext struct {
echo.Context
ID string
Config config.Config
Status *sink.Status
ID string
DB models.Querier
Config config.Config
Handles *kv.Namespace
Sessions *kv.Namespace
Status *sink.Status
}
func NewSession(c echo.Context, cfg config.Config) *SessionContext {
func NewSession(c echo.Context, cfg config.Config, q models.Querier, hkv *kv.Namespace, skv *kv.Namespace) *SessionContext {
id := getOrCreateSessionID(c)
return &SessionContext{
Context: c,
ID: id,
Config: cfg,
Context: c,
ID: id,
Config: cfg,
DB: q,
Handles: hkv,
Sessions: skv,
Status: &sink.Status{
SessionID: id,
Expires: cfg.KV.GetSessionExpiry(time.Now()),

View File

@@ -38,7 +38,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(handle)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/account/card.templ`, Line: 11, Col: 43}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/account/card.templ`, Line: 11, Col: 43}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
@@ -51,7 +51,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(addr)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/account/card.templ`, Line: 18, Col: 42}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/account/card.templ`, Line: 18, Col: 42}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
@@ -64,7 +64,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(block)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/account/card.templ`, Line: 23, Col: 47}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/account/card.templ`, Line: 23, Col: 47}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
@@ -77,7 +77,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/account/card.templ`, Line: 27, Col: 32}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/account/card.templ`, Line: 27, Col: 32}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
@@ -120,7 +120,7 @@ func optionComponent(account models.Account) templ.Component {
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(account.Address)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/account/card.templ`, Line: 37, Col: 19}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/account/card.templ`, Line: 37, Col: 19}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
@@ -163,7 +163,7 @@ func rowComponent(account models.Account) templ.Component {
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(account.Address)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/account/card.templ`, Line: 45, Col: 19}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/account/card.templ`, Line: 45, Col: 19}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {

View File

@@ -38,7 +38,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(handle)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/activity/card.templ`, Line: 11, Col: 43}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/activity/card.templ`, Line: 11, Col: 43}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
@@ -51,7 +51,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(addr)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/activity/card.templ`, Line: 18, Col: 42}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/activity/card.templ`, Line: 18, Col: 42}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
@@ -64,7 +64,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(block)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/activity/card.templ`, Line: 23, Col: 47}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/activity/card.templ`, Line: 23, Col: 47}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
@@ -77,7 +77,7 @@ func cardComponent(handle, addr, block, name string) templ.Component {
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/activity/card.templ`, Line: 27, Col: 32}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/activity/card.templ`, Line: 27, Col: 32}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
@@ -120,7 +120,7 @@ func optionComponent(account models.Activity) templ.Component {
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(account.Status)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/activity/card.templ`, Line: 37, Col: 18}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/activity/card.templ`, Line: 37, Col: 18}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
@@ -163,7 +163,7 @@ func rowComponent(account models.Activity) templ.Component {
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(account.Status)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `controller/activity/card.templ`, Line: 45, Col: 18}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `middleware/controllers/activity/card.templ`, Line: 45, Col: 18}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {

View File

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

View File

@@ -10,9 +10,21 @@ import (
// UseSession is a middleware that adds a new key to the context
func UseSession(cnfg config.Config) echo.MiddlewareFunc {
q, err := cnfg.DB.GetQuerier()
if err != nil {
return nil
}
hkv, err := cnfg.KV.GetHandles()
if err != nil {
return nil
}
skv, err := cnfg.KV.GetSessions()
if err != nil {
return nil
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := NewSession(c, cnfg)
ctx := NewSession(c, cnfg, q, hkv, skv)
return next(ctx)
}
}

View File

@@ -4,22 +4,25 @@
package routes
import (
"github.com/sonr-io/motr/controller"
"github.com/sonr-io/motr/config"
"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)
func SetupRoutes(c *config.Server) {
// Home
c.GET("/", handlers.HandleDefaultIndex)
c.GET("/expired", handlers.HandleDefaultExpired)
c.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)
// Login
c.GET("/login", handlers.HandleLoginInitial)
c.GET("/login/:handle", handlers.HandleLoginStart)
c.POST("/login/:handle/check", handlers.HandleLoginCheck)
c.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)
// Register
c.GET("/register", handlers.HandleRegisterInitial)
c.GET("/register/:handle", handlers.HandleRegisterStart)
c.POST("/register/:handle/check", handlers.HandleRegisterCheck)
c.POST("/register/:handle/finish", handlers.HandleRegisterFinish)
}