2022-08-10 05:10:47 +00:00
|
|
|
package index
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2024-09-12 02:28:52 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/models/demographic"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create the controller.
|
|
|
|
func Create() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("index.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" || r.Method != http.MethodGet {
|
|
|
|
log.Error("404 Not Found: %s", r.URL.Path)
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-12 02:28:52 +00:00
|
|
|
// Get website statistics to show on home page.
|
|
|
|
demo, err := demographic.Get()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("demographic.Get: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]interface{}{
|
|
|
|
"Demographic": demo,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
2022-08-10 05:10:47 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-08-22 00:56:14 +00:00
|
|
|
|
|
|
|
// Favicon
|
|
|
|
func Favicon() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, config.StaticPath+"/favicon.ico")
|
|
|
|
})
|
|
|
|
}
|
2023-11-26 01:15:21 +00:00
|
|
|
|
|
|
|
// PWA Manifest
|
|
|
|
func Manifest() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, config.StaticPath+"/manifest.json")
|
|
|
|
})
|
|
|
|
}
|
2024-07-21 02:44:22 +00:00
|
|
|
|
|
|
|
// Service Worker for web push.
|
|
|
|
func ServiceWorker() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "application/javascript; charset=UTF-8")
|
|
|
|
w.Header().Add("Service-Worker-Allowed", "/")
|
|
|
|
http.ServeFile(w, r, config.StaticPath+"/js/service-worker.js")
|
|
|
|
})
|
|
|
|
}
|