diff --git a/src/cmd/frontend/main.go b/src/cmd/frontend/main.go index ec6e36a..d9e73d6 100644 --- a/src/cmd/frontend/main.go +++ b/src/cmd/frontend/main.go @@ -1,6 +1,8 @@ package main import ( + "crowsnest/internal/app" + "crowsnest/internal/middleware" "crowsnest/internal/model/database" "log" "net/http" @@ -8,10 +10,6 @@ import ( _ "github.com/lib/pq" ) -type App struct { - articles *database.ArticleModel -} - func main() { db, err := database.DbConnection() if err != nil { @@ -19,14 +17,12 @@ func main() { } // define app - app := &App{ - articles: &database.ArticleModel{DB: db}, - } + webapp := app.NewApp(db) // start web server server := http.Server{ Addr: ":8080", - Handler: app.routes(), + Handler: middleware.Logging(webapp.Routes()), } log.Println("server started, listening on :8080") diff --git a/src/cmd/frontend/routes.go b/src/cmd/frontend/routes.go deleted file mode 100644 index ef0c054..0000000 --- a/src/cmd/frontend/routes.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "log" - "net/http" - "time" -) - -// LoggingMiddleware logs details about each incoming HTTP request. -func LoggingMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - - // Call the next handler - next.ServeHTTP(w, r) - - log.Printf("[request] %s %s from %s (%v)", r.URL.Path, r.Method, r.RemoteAddr, time.Since(start)) - }) -} - -func (app *App) routes() http.Handler { - mux := http.NewServeMux() - - // dynamic routes - mux.Handle("GET /", LoggingMiddleware(http.HandlerFunc(app.Index))) - mux.Handle("GET /page/{id}", LoggingMiddleware(http.HandlerFunc(app.Index))) - mux.Handle("POST /up/search", LoggingMiddleware(http.HandlerFunc(app.UpSearch))) - - // serve files from the "static" directory - fs := http.FileServer(http.Dir("assets/static")) - mux.Handle("GET /static/", http.StripPrefix("/", fs)) - - return mux -} diff --git a/src/cmd/frontend/Index.go b/src/internal/app/Index.go similarity index 99% rename from src/cmd/frontend/Index.go rename to src/internal/app/Index.go index 2a79ae6..aa4f88f 100644 --- a/src/cmd/frontend/Index.go +++ b/src/internal/app/Index.go @@ -1,4 +1,4 @@ -package main +package app import ( "crowsnest/internal/model" diff --git a/src/cmd/frontend/UpSearch.go b/src/internal/app/UpSearch.go similarity index 98% rename from src/cmd/frontend/UpSearch.go rename to src/internal/app/UpSearch.go index 432df7a..bb1ba12 100644 --- a/src/cmd/frontend/UpSearch.go +++ b/src/internal/app/UpSearch.go @@ -1,4 +1,4 @@ -package main +package app import ( "crowsnest/internal/model" diff --git a/src/internal/app/app.go b/src/internal/app/app.go new file mode 100644 index 0000000..d8aac12 --- /dev/null +++ b/src/internal/app/app.go @@ -0,0 +1,32 @@ +package app + +import ( + "crowsnest/internal/model/database" + "database/sql" + "net/http" +) + +type App struct { + articles *database.ArticleModel +} + +func NewApp(db *sql.DB) *App { + return &App{ + articles: &database.ArticleModel{DB: db}, + } +} + +func (app *App) Routes() http.Handler { + mux := http.NewServeMux() + + // dynamic routes + mux.Handle("GET /", http.HandlerFunc(app.Index)) + mux.Handle("GET /page/{id}", http.HandlerFunc(app.Index)) + mux.Handle("POST /up/search", http.HandlerFunc(app.UpSearch)) + + // serve files from the "static" directory + fs := http.FileServer(http.Dir("assets/static")) + mux.Handle("GET /static/", http.StripPrefix("/", fs)) + + return mux +} diff --git a/src/internal/middleware/logging.go b/src/internal/middleware/logging.go new file mode 100644 index 0000000..cc1dc66 --- /dev/null +++ b/src/internal/middleware/logging.go @@ -0,0 +1,19 @@ +package middleware + +import ( + "log" + "net/http" + "time" +) + +// LoggingMiddleware logs details about each incoming HTTP request. +func Logging(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + // Call the next handler + next.ServeHTTP(w, r) + + log.Printf("[request] %s %s from %s (%v)", r.URL.Path, r.Method, r.RemoteAddr, time.Since(start)) + }) +}