diff --git a/app.go b/app.go deleted file mode 100644 index f93b132..0000000 --- a/app.go +++ /dev/null @@ -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 -} diff --git a/go.mod b/go.mod index 6570b09..c126487 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c9dba4b --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 214430c..270c673 100644 --- a/main.go +++ b/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") }