2021-02-04 10:36:02 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.mindeco.de/http/render"
|
|
|
|
|
2021-02-04 15:54:30 +00:00
|
|
|
"github.com/ssb-ngi-pointer/gossb-rooms/internal/repo"
|
|
|
|
"github.com/ssb-ngi-pointer/gossb-rooms/web"
|
|
|
|
"github.com/ssb-ngi-pointer/gossb-rooms/web/handlers/news"
|
|
|
|
"github.com/ssb-ngi-pointer/gossb-rooms/web/i18n"
|
|
|
|
"github.com/ssb-ngi-pointer/gossb-rooms/web/router"
|
2021-02-04 10:36:02 +00:00
|
|
|
)
|
|
|
|
|
2021-02-04 16:21:21 +00:00
|
|
|
// New initializes the whole web stack for rooms, with all the sub-modules and routing.
|
2021-02-04 15:52:55 +00:00
|
|
|
func New(m *mux.Router, repo repo.Interface) (http.Handler, error) {
|
2021-02-04 10:36:02 +00:00
|
|
|
if m == nil {
|
|
|
|
m = router.CompleteApp()
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:52:55 +00:00
|
|
|
locHelper, err := i18n.New(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-04 16:21:21 +00:00
|
|
|
r, err := render.New(web.Templates,
|
2021-02-04 10:36:02 +00:00
|
|
|
render.BaseTemplates("/base.tmpl"),
|
|
|
|
render.AddTemplates(append(news.HTMLTemplates,
|
|
|
|
"/landing/index.tmpl",
|
|
|
|
"/landing/about.tmpl",
|
|
|
|
"/error.tmpl")...),
|
2021-02-04 11:00:12 +00:00
|
|
|
render.FuncMap(web.TemplateFuncs(m)),
|
2021-02-04 16:21:21 +00:00
|
|
|
// TODO: add plural and template data variants
|
|
|
|
// TODO: move these to the i18n helper pkg
|
2021-02-04 15:52:55 +00:00
|
|
|
render.InjectTemplateFunc("i18n", func(r *http.Request) interface{} {
|
|
|
|
lang := r.FormValue("lang")
|
|
|
|
accept := r.Header.Get("Accept-Language")
|
|
|
|
loc := locHelper.NewLocalizer(lang, accept)
|
|
|
|
return loc.LocalizeSimple
|
|
|
|
}),
|
2021-02-04 10:36:02 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("web Handler: failed to create renderer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hookup handlers to the router
|
|
|
|
m.PathPrefix("/news").Handler(http.StripPrefix("/news", news.Handler(m, r)))
|
|
|
|
|
|
|
|
m.Get(router.CompleteIndex).Handler(r.StaticHTML("/landing/index.tmpl"))
|
|
|
|
m.Get(router.CompleteAbout).Handler(r.StaticHTML("/landing/about.tmpl"))
|
|
|
|
|
|
|
|
m.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
fmt.Fprintf(rw, "404: url not found")
|
|
|
|
})
|
2021-02-04 15:52:55 +00:00
|
|
|
|
|
|
|
// TODO: disable in non-dev
|
|
|
|
return r.GetReloader()(m), nil
|
2021-02-04 10:36:02 +00:00
|
|
|
}
|