mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-07-30 01:22:38 -04:00
- Return concrete structs - Accept interfaces - Define narrow interfaces locally where needed
50 lines
892 B
Go
50 lines
892 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"net/http"
|
|
"text/template"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi"
|
|
"github.com/go-chi/chi/middleware"
|
|
)
|
|
|
|
type handlers struct {
|
|
ctx context.Context
|
|
// Objects
|
|
db Database
|
|
runner UpdateForcer
|
|
indexTemplate *template.Template
|
|
// Mockable functions
|
|
timeNow func() time.Time
|
|
}
|
|
|
|
//go:embed ui/*
|
|
var uiFS embed.FS
|
|
|
|
func newHandler(ctx context.Context, rootURL string,
|
|
db Database, runner UpdateForcer) http.Handler {
|
|
indexTemplate := template.Must(template.ParseFS(uiFS, "ui/index.html"))
|
|
|
|
handlers := &handlers{
|
|
ctx: ctx,
|
|
db: db,
|
|
indexTemplate: indexTemplate,
|
|
// TODO build information
|
|
timeNow: time.Now,
|
|
runner: runner,
|
|
}
|
|
|
|
router := chi.NewRouter()
|
|
|
|
router.Use(middleware.Logger)
|
|
|
|
router.Get(rootURL+"/", handlers.index)
|
|
|
|
router.Get(rootURL+"/update", handlers.update)
|
|
|
|
return router
|
|
}
|