move middleware and routes/ app into internal dir
This commit is contained in:
59
src/internal/app/Index.go
Normal file
59
src/internal/app/Index.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package app
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
40
src/internal/app/UpSearch.go
Normal file
40
src/internal/app/UpSearch.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package app
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
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