refactor: extract api setup to private package

This commit is contained in:
Marc 2024-10-06 10:39:07 -04:00
parent 1dd0f6b735
commit ee1d1c07df
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 26 additions and 71 deletions

67
app.go
View file

@ -1,67 +0,0 @@
package main
import (
"fmt"
"log/slog"
"net/http"
"time"
)
type Route struct {
Path string
Handler http.HandlerFunc
}
type BackgroundTask struct {
Handler func()
Interval time.Duration
}
type App struct {
Routes []Route
BackgroundTasks []BackgroundTask
StaticRoot string
}
func LoggingMiddleware(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
f(w, r)
slog.Info(fmt.Sprintf("%s - %s (%dms)", r.Method, r.URL, time.Now().Sub(start).Milliseconds()))
}
}
func (a App) Start(addr string) {
for _, route := range a.Routes {
http.HandleFunc(route.Path, LoggingMiddleware(route.Handler))
}
if a.StaticRoot != "" {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(a.StaticRoot))))
}
for _, task := range a.BackgroundTasks {
go func() {
for {
task.Handler()
time.Sleep(task.Interval)
}
}()
}
http.ListenAndServe(addr, nil)
}
func (a *App) AddRoute(path string, handler http.HandlerFunc) {
a.Routes = append(a.Routes, Route{Path: path, Handler: handler})
}
func NewApp(routes map[string]http.HandlerFunc, backgroundTasks []BackgroundTask, staticRoot string) *App {
app := App{StaticRoot: staticRoot, BackgroundTasks: backgroundTasks}
for route, handler := range routes {
app.AddRoute(route, handler)
}
return &app
}

4
go.mod
View file

@ -1,3 +1,5 @@
module rss module rss
go 1.22.2 go 1.23.1
require forge.karnov.club/marc/http-api-kit v0.0.1

6
go.sum Normal file
View file

@ -0,0 +1,6 @@
forge.karnov.club/marc/http-api-kit v0.0.0-20241006142230-32b297ad54ca h1:SRVnFmO8c2x5IZSA1wTxIK6S3fJog53/K9J12KfsCAI=
forge.karnov.club/marc/http-api-kit v0.0.0-20241006142230-32b297ad54ca/go.mod h1:AbIDCaC8ksKUlXkKegB3JLR2VNgTuIEUNBoKpa5Ck2g=
forge.karnov.club/marc/http-api-kit v0.0.0-20241006143417-e6e1e32f5f93 h1:iTZE37H45RZ2BOOQBKNJPw3KTMQ9lFGg5/oAhj/fvYw=
forge.karnov.club/marc/http-api-kit v0.0.0-20241006143417-e6e1e32f5f93/go.mod h1:AbIDCaC8ksKUlXkKegB3JLR2VNgTuIEUNBoKpa5Ck2g=
forge.karnov.club/marc/http-api-kit v0.0.1 h1:evYR9T7TZU64fDGN/ZsfohXyO8zS2yHrUN6fGmsJVFQ=
forge.karnov.club/marc/http-api-kit v0.0.1/go.mod h1:AbIDCaC8ksKUlXkKegB3JLR2VNgTuIEUNBoKpa5Ck2g=

20
main.go
View file

@ -1,6 +1,7 @@
package main package main
import ( import (
httpKit "forge.karnov.club/marc/http-api-kit"
"net/http" "net/http"
"time" "time"
) )
@ -14,8 +15,8 @@ var routeMap = map[string]http.HandlerFunc{
"/ping": healthcheck, "/ping": healthcheck,
} }
var bgTasks = []BackgroundTask{ var bgTasks = []httpKit.BackgroundTask{
{refreshFeeds, 10 * time.Minute}, {Handler: refreshFeeds, Interval: 10 * time.Minute},
} }
func main() { func main() {
@ -27,5 +28,18 @@ func main() {
SharedCache = existingStore SharedCache = existingStore
} }
NewApp(routeMap, bgTasks, "./static").Start(":9000") app := httpKit.NewApp()
for routePath, routeHandler := range routeMap {
app.AddRoute(routePath, routeHandler)
}
for _, bgTask := range bgTasks {
app.AddBackgroundTask(bgTask)
}
app.AddStaticRoute("/static/", "./static")
app.AddMiddleware(httpKit.LoggingMiddleware)
app.Start(":9000")
} }