dnote/pkg/server/controllers/static.go
Sung Won Cho 5bba57fd29
Remove dependency on packr (#597)
* Embed files

* Build CLI

* Remove packr

* Embed view directory

* Embed static files

* Make view engine

* Populate build info
2022-05-09 20:34:23 +10:00

35 lines
874 B
Go

package controllers
import (
"net/http"
"strings"
"github.com/dnote/dnote/pkg/server/app"
"github.com/dnote/dnote/pkg/server/views"
)
// NewStatic creates a new Static controller.
func NewStatic(app *app.App, viewEngine *views.Engine) *Static {
return &Static{
NotFoundView: viewEngine.NewView(app, views.Config{Title: "Not Found", Layout: "base"}, "static/not_found"),
}
}
// Static is a static controller
type Static struct {
NotFoundView *views.View
}
// NotFound is a catch-all handler for requests with no matching handler
func (s *Static) NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
accept := r.Header.Get("Accept")
if strings.Contains(accept, "text/html") {
s.NotFoundView.Render(w, r, nil, http.StatusOK)
} else {
statusText := http.StatusText(http.StatusNotFound)
w.Write([]byte(statusText))
}
}