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,59 +0,0 @@
package main
import (
"crowsnest/internal/model"
"html/template"
"net/http"
"strconv"
)
// List the latest articles using the base template.
func (app *App) Index(w http.ResponseWriter, req *http.Request) {
const pageSize = 15
var limit, offset, pageId uint64 = pageSize, 0, 0
var err error
// get page number
if pageId, err = strconv.ParseUint(req.PathValue("id"), 10, 32); err == nil {
pageId--
offset = pageId * pageSize
}
// get articles
articles, err := app.articles.All(int(limit), int(offset))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// get count of total articles
totalCount, err := app.articles.CountAll()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
totalCount /= pageSize
// convert to viewmodel
articleVMs := make([]*model.ArticleViewModel, 0, len(articles))
for _, a := range articles {
articleVMs = append(articleVMs, a.ViewModel())
}
// render template
t := template.Must(template.ParseFiles(
"assets/templates/article.html",
"assets/templates/layout.html",
"assets/templates/components/pagination.html"))
data := map[string]interface{}{
"SelectedNavItemArticle": true,
"ArticleVMs": &articleVMs,
"Paginations": model.NewPaginationViewModel(uint(pageId+1), totalCount+1),
}
err = t.ExecuteTemplate(w, "base", data)
if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
return
}
}

View File

@@ -1,40 +0,0 @@
package main
import (
"crowsnest/internal/model"
"html/template"
"net/http"
)
// Enpoint that returns a list of articles given search terms in the post
// request of a search form. Uses the content template.
func (app *App) UpSearch(w http.ResponseWriter, req *http.Request) {
// construct search query
searchTerms := req.FormValue("search")
if searchTerms == "" {
app.Index(w, req)
return
}
// get articles
articles, err := app.articles.Search(searchTerms)
if err != nil {
// treat as no result
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// convert to viewmodel
articleVMs := make([]*model.ArticleViewModel, 0, len(articles))
for _, a := range articles {
articleVMs = append(articleVMs, a.ViewModel())
}
// render template
t := template.Must(template.ParseFiles("assets/templates/article.html"))
err = t.ExecuteTemplate(w, "content", articleVMs)
if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
return
}
}

View File

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

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
}