feat(service): new service abstraction to directly interact with DBs and operations
This should make the router be just about HTML rendering, paramaters validation and routing.
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"forge.capytal.company/capytalcode/project-comicverse/router"
|
||||
"forge.capytal.company/capytalcode/project-comicverse/service"
|
||||
"forge.capytal.company/capytalcode/project-comicverse/static"
|
||||
"forge.capytal.company/capytalcode/project-comicverse/templates"
|
||||
"forge.capytal.company/loreddev/x/tinyssert"
|
||||
@@ -91,7 +92,20 @@ func (app *app) setup() error {
|
||||
|
||||
var err error
|
||||
|
||||
service, err := service.New(service.Config{
|
||||
DB: app.db,
|
||||
S3: app.s3,
|
||||
|
||||
Assertions: app.assert,
|
||||
Logger: app.logger,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Join(errors.New("unable to initiate service"), err)
|
||||
}
|
||||
|
||||
app.handler, err = router.New(router.Config{
|
||||
Service: service,
|
||||
|
||||
Templates: templates.Templates(),
|
||||
DisableCache: app.developmentMode,
|
||||
StaticFiles: app.staticFiles,
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"forge.capytal.company/capytalcode/project-comicverse/service"
|
||||
"forge.capytal.company/loreddev/x/smalltrip"
|
||||
"forge.capytal.company/loreddev/x/smalltrip/exception"
|
||||
"forge.capytal.company/loreddev/x/smalltrip/middleware"
|
||||
@@ -14,6 +15,8 @@ import (
|
||||
)
|
||||
|
||||
type router struct {
|
||||
service service.Service
|
||||
|
||||
templates *template.Template
|
||||
staticFiles fs.FS
|
||||
cache bool
|
||||
@@ -23,11 +26,14 @@ type router struct {
|
||||
}
|
||||
|
||||
func New(cfg Config) (http.Handler, error) {
|
||||
if cfg.Service == nil {
|
||||
return nil, errors.New("service is nil")
|
||||
}
|
||||
if cfg.Templates == nil {
|
||||
return nil, errors.New("templates are nil")
|
||||
return nil, errors.New("templates is nil")
|
||||
}
|
||||
if cfg.StaticFiles == nil {
|
||||
return nil, errors.New("static files handler is nil")
|
||||
return nil, errors.New("static files is nil")
|
||||
}
|
||||
if cfg.Assertions == nil {
|
||||
return nil, errors.New("assertions is nil")
|
||||
@@ -49,6 +55,8 @@ func New(cfg Config) (http.Handler, error) {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Service service.Service
|
||||
|
||||
Templates *template.Template
|
||||
StaticFiles fs.FS
|
||||
DisableCache bool
|
||||
@@ -81,12 +89,17 @@ func (router *router) setup() http.Handler {
|
||||
r.Use(exception.Middleware())
|
||||
|
||||
r.Handle("/static", http.StripPrefix("/static/", http.FileServerFS(router.staticFiles)))
|
||||
|
||||
r.HandleFunc("/dashboard", router.dashboard)
|
||||
|
||||
r.HandleFunc("GET /projects", router.listProjects)
|
||||
r.HandleFunc("POST /projects", router.newProject)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (router *router) dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
router.assert.NotNil(router.templates)
|
||||
router.assert.NotNil(w)
|
||||
router.assert.NotNil(r)
|
||||
|
||||
@@ -96,3 +109,15 @@ func (router *router) dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
exception.InternalServerError(err).ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (router *router) listProjects(w http.ResponseWriter, r *http.Request) {
|
||||
router.assert.NotNil(router.templates)
|
||||
router.assert.NotNil(w)
|
||||
router.assert.NotNil(r)
|
||||
}
|
||||
|
||||
func (router *router) newProject(w http.ResponseWriter, r *http.Request) {
|
||||
router.assert.NotNil(router.templates)
|
||||
router.assert.NotNil(w)
|
||||
router.assert.NotNil(r)
|
||||
}
|
||||
|
||||
7
service/projects.go
Normal file
7
service/projects.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package service
|
||||
|
||||
func (s *service) NewProject() {
|
||||
}
|
||||
|
||||
func (s *service) ListProjects() {
|
||||
}
|
||||
43
service/service.go
Normal file
43
service/service.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"forge.capytal.company/loreddev/x/tinyssert"
|
||||
)
|
||||
|
||||
type service struct {
|
||||
assert tinyssert.Assertions
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func New(cfg Config) (Service, error) {
|
||||
if cfg.DB == nil {
|
||||
return nil, errors.New("database should not be a nil interface")
|
||||
}
|
||||
if cfg.S3 == nil {
|
||||
return nil, errors.New("s3 client should not be a nil interface")
|
||||
}
|
||||
if cfg.Assertions == nil {
|
||||
return nil, errors.New("assertions should not be a nil interface")
|
||||
}
|
||||
if cfg.Logger == nil {
|
||||
return nil, errors.New("logger should not be a nil pointer")
|
||||
}
|
||||
return &service{
|
||||
assert: cfg.Assertions,
|
||||
log: cfg.Logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Assertions tinyssert.Assertions
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
ListProjects()
|
||||
NewProject()
|
||||
}
|
||||
Reference in New Issue
Block a user