2024-09-13 03:03:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
Path string
|
|
|
|
Handler http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
2024-09-13 04:38:46 +00:00
|
|
|
type BackgroundTask struct {
|
|
|
|
Handler func()
|
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
Routes []Route
|
|
|
|
BackgroundTasks []BackgroundTask
|
|
|
|
StaticRoot string
|
2024-09-13 03:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 04:38:46 +00:00
|
|
|
func (a App) Start(addr string) {
|
2024-09-13 03:03:39 +00:00
|
|
|
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))))
|
|
|
|
}
|
|
|
|
|
2024-09-13 04:38:46 +00:00
|
|
|
for _, task := range a.BackgroundTasks {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
task.Handler()
|
|
|
|
time.Sleep(task.Interval)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-09-13 03:03:39 +00:00
|
|
|
http.ListenAndServe(addr, nil)
|
|
|
|
}
|
|
|
|
|
2024-09-13 04:38:46 +00:00
|
|
|
func (a *App) AddRoute(path string, handler http.HandlerFunc) {
|
2024-09-13 03:03:39 +00:00
|
|
|
a.Routes = append(a.Routes, Route{Path: path, Handler: handler})
|
|
|
|
}
|
2024-09-13 04:38:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|