restructuring
This commit is contained in:
41
cmd/frontend/Index.go
Normal file
41
cmd/frontend/Index.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crowsnest/internal/data"
|
||||
"crowsnest/internal/model"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// sort criteria
|
||||
type articleDateOrder struct {}
|
||||
func (ord articleDateOrder) Weight(a *model.Article) int {
|
||||
return int(a.PublishDate.Unix())
|
||||
}
|
||||
|
||||
// List the latest articles using the base template.
|
||||
func (app *App) Index(w http.ResponseWriter, req *http.Request) {
|
||||
// retrieve from repo
|
||||
fds, err := data.NewFileDatastore("persistence/spiegel100.json")
|
||||
if err != nil { http.Error(w, "Failed to load datastore", http.StatusInternalServerError); return; }
|
||||
repo, err := data.NewDefaultRepository[*model.Article](fds, "article")
|
||||
if err != nil { http.Error(w, "Failed to create repository", http.StatusInternalServerError); return; }
|
||||
articles, err := repo.GetByCriteria(articleDateOrder{})
|
||||
if err != nil { http.Error(w, "Failed to get articles", http.StatusInternalServerError); return; }
|
||||
|
||||
// truncate
|
||||
if len(articles) > 10 {
|
||||
articles = articles[:10]
|
||||
}
|
||||
|
||||
// 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"))
|
||||
err = t.ExecuteTemplate(w, "base", articleVMs)
|
||||
if err != nil { http.Error(w, "Failed to render template", http.StatusInternalServerError); return; }
|
||||
}
|
||||
67
cmd/frontend/UpSearch.go
Normal file
67
cmd/frontend/UpSearch.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crowsnest/internal/data"
|
||||
"crowsnest/internal/model"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// sort criteria
|
||||
type articleTermFrequency struct {
|
||||
terms []string
|
||||
}
|
||||
|
||||
func (ord articleTermFrequency) Weight(a *model.Article) int {
|
||||
score := 0
|
||||
for _, term := range ord.terms {
|
||||
score += strings.Count(a.Content, term)
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// parse the form data
|
||||
err := req.ParseForm()
|
||||
if err != nil { http.Error(w, "Unable to parse form", http.StatusBadRequest); return; }
|
||||
|
||||
// collect search terms
|
||||
p := regexp.MustCompile("\\s+")
|
||||
searchTerms := make([]string, 0)
|
||||
for _, elem := range p.Split(req.FormValue("search"), -1) {
|
||||
elem = strings.TrimSpace(elem)
|
||||
if elem == "" { continue }
|
||||
searchTerms = append(searchTerms, elem)
|
||||
}
|
||||
|
||||
// retrieve from repo
|
||||
fds, _ := data.NewFileDatastore("persistence/spiegel100.json")
|
||||
if err != nil { http.Error(w, "Failed to read datastore", http.StatusInternalServerError); return; }
|
||||
|
||||
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
|
||||
if err != nil { http.Error(w, "Failed to create repository", http.StatusInternalServerError); return; }
|
||||
|
||||
var articles []*model.Article
|
||||
if len(searchTerms) != 0 {
|
||||
articles, err = repo.GetByCriteria(articleTermFrequency{ terms: searchTerms })
|
||||
} else {
|
||||
app.Index(w, req)
|
||||
return
|
||||
}
|
||||
if err != nil { http.Error(w, "Failed to get articles from repo", 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; }
|
||||
}
|
||||
@@ -1,20 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crowsnest/internal/routes"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
type App struct {}
|
||||
|
||||
func main() {
|
||||
// routes
|
||||
http.HandleFunc("GET /", routes.Index)
|
||||
http.HandleFunc("POST /up/search", routes.UpSearch)
|
||||
app := &App{}
|
||||
|
||||
// serve files from the "static" directory
|
||||
fs := http.FileServer(http.Dir("assets/static"))
|
||||
http.Handle("GET /static/", http.StripPrefix("/", fs))
|
||||
server := http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: app.routes(),
|
||||
}
|
||||
|
||||
// start server
|
||||
http.ListenAndServe(":8080", nil)
|
||||
server.ListenAndServe()
|
||||
log.Println("server started, listening on :8080")
|
||||
}
|
||||
|
||||
19
cmd/frontend/routes.go
Normal file
19
cmd/frontend/routes.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *App) routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// dynamic routes
|
||||
mux.HandleFunc("GET /", app.Index)
|
||||
mux.HandleFunc("POST /up/search", app.UpSearch)
|
||||
|
||||
// serve files from the "static" directory
|
||||
fs := http.FileServer(http.Dir("assets/static"))
|
||||
mux.Handle("GET /static/", http.StripPrefix("/", fs))
|
||||
|
||||
return mux
|
||||
}
|
||||
Reference in New Issue
Block a user