move middleware and routes/ app into internal dir

This commit is contained in:
2025-01-11 19:10:18 +01:00
parent f083496ebc
commit 20edc0f2f9
6 changed files with 57 additions and 44 deletions

View File

@@ -1,6 +1,8 @@
package main package main
import ( import (
"crowsnest/internal/app"
"crowsnest/internal/middleware"
"crowsnest/internal/model/database" "crowsnest/internal/model/database"
"log" "log"
"net/http" "net/http"
@@ -8,10 +10,6 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
type App struct {
articles *database.ArticleModel
}
func main() { func main() {
db, err := database.DbConnection() db, err := database.DbConnection()
if err != nil { if err != nil {
@@ -19,14 +17,12 @@ func main() {
} }
// define app // define app
app := &App{ webapp := app.NewApp(db)
articles: &database.ArticleModel{DB: db},
}
// start web server // start web server
server := http.Server{ server := http.Server{
Addr: ":8080", Addr: ":8080",
Handler: app.routes(), Handler: middleware.Logging(webapp.Routes()),
} }
log.Println("server started, listening on :8080") log.Println("server started, listening on :8080")

View File

@@ -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
}

View File

@@ -1,4 +1,4 @@
package main package app
import ( import (
"crowsnest/internal/model" "crowsnest/internal/model"

View File

@@ -1,4 +1,4 @@
package main package app
import ( import (
"crowsnest/internal/model" "crowsnest/internal/model"

32
src/internal/app/app.go Normal file
View File

@@ -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
}

View File

@@ -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))
})
}