refactor: extract api setup to private package
This commit is contained in:
parent
1dd0f6b735
commit
ee1d1c07df
4 changed files with 26 additions and 71 deletions
67
app.go
67
app.go
|
@ -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
4
go.mod
|
@ -1,3 +1,5 @@
|
|||
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
6
go.sum
Normal 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
20
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
httpKit "forge.karnov.club/marc/http-api-kit"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
@ -14,8 +15,8 @@ var routeMap = map[string]http.HandlerFunc{
|
|||
"/ping": healthcheck,
|
||||
}
|
||||
|
||||
var bgTasks = []BackgroundTask{
|
||||
{refreshFeeds, 10 * time.Minute},
|
||||
var bgTasks = []httpKit.BackgroundTask{
|
||||
{Handler: refreshFeeds, Interval: 10 * time.Minute},
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -27,5 +28,18 @@ func main() {
|
|||
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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue