move middleware and routes/ app into internal dir
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package app
|
||||
|
||||
import (
|
||||
"crowsnest/internal/model"
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package app
|
||||
|
||||
import (
|
||||
"crowsnest/internal/model"
|
||||
32
src/internal/app/app.go
Normal file
32
src/internal/app/app.go
Normal 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
|
||||
}
|
||||
19
src/internal/middleware/logging.go
Normal file
19
src/internal/middleware/logging.go
Normal 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))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user