feat: add flag to generate error page for web server
5 files changed, 55 insertions(+), 18 deletions(-)
M .gitignore → .gitignore
@@ -21,5 +21,6 @@ /.env /.envrc /.pre-commit-config.yaml /frontend/static/base.css +/frontent/error.html /data/ /config.toml
M cmd/searchix-web/main.go → cmd/searchix-web/main.go
@@ -12,6 +12,8 @@ "badc0de.net/pkg/flagutil" "github.com/getsentry/sentry-go" + "go.alanpearce.eu/searchix/frontend" + "go.alanpearce.eu/searchix/internal/components" "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/importer" "go.alanpearce.eu/searchix/internal/index"@@ -26,10 +28,11 @@ "print-default-config", false, "print default configuration and exit", ) - dev = flag.Bool("dev", false, "enable live reloading and nicer logging") - replace = flag.Bool("replace", false, "replace existing index and exit") - version = flag.Bool("version", false, "print version information") - cpuprofile = flag.String("cpuprofile", "", "enable CPU profiling and save to `file`") + generateErrorPage = flag.Bool("generate-error-page", false, "generate error page and exit") + dev = flag.Bool("dev", false, "enable live reloading and nicer logging") + replace = flag.Bool("replace", false, "replace existing index and exit") + version = flag.Bool("version", false, "print version information") + cpuprofile = flag.String("cpuprofile", "", "enable CPU profiling and save to `file`") ) func main() {@@ -48,6 +51,22 @@ panic("can't write to standard output?!") } os.Exit(0) } + if *generateErrorPage { + err := components.ErrorTemplate(components.TemplateData{ + Source: nil, + Sources: []*config.Source{}, + Query: "", + ExtraHeadHTML: "", + Code: 0, + Message: `{{placeholder "http.error.status_code"}} {{placeholder "http.error.status_text"}}`, + Assets: frontend.Assets, + }).Render(os.Stdout) + if err != nil { + panic("failed to render error template: " + err.Error()) + } + os.Exit(0) + } + if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil {
M internal/components/error.go → internal/components/error.go
@@ -15,3 +15,10 @@ func ErrorPage(tdata TemplateData) g.Node { return Page(tdata, Error(tdata)) } + +func ErrorTemplate(tdata TemplateData) g.Node { + return Page(tdata, P( + Class("notice error"), + g.Raw(tdata.Message), + )) +}
M internal/components/page.go → internal/components/page.go
@@ -21,11 +21,13 @@ Meta(Name("viewport"), Content("width=device-width, initial-scale=1")), TitleEl(g.Text("Searchix"), g.If(config.DevMode, g.Text(" (Dev)"))), g.Map(tdata.Assets.Stylesheets, css), g.Raw(tdata.ExtraHeadHTML), - Link( - Rel("search"), - Type("application/opensearchdescription+xml"), - TitleAttr("Searchix "+sourceNameAndType(nil)), - Href(joinPath("opensearch.xml")), + g.If(len(tdata.Sources) > 0, + Link( + Rel("search"), + Type("application/opensearchdescription+xml"), + TitleAttr("Searchix "+sourceNameAndType(nil)), + Href(joinPath("opensearch.xml")), + ), ), g.Map(tdata.Sources, func(source *config.Source) g.Node { return Link(@@ -40,16 +42,18 @@ Body( Header( Nav( H1(A(Href("/"), g.Text("Searchix"))), - A( - c.Classes{ - "current": tdata.Source == nil, - }, - g.If( - tdata.Source == nil, - Href("/"), - Href(joinPathQuery("/", tdata.Query)), + g.If(len(tdata.Sources) > 0, + A( + c.Classes{ + "current": tdata.Source == nil, + }, + g.If( + tdata.Source == nil, + Href("/"), + Href(joinPathQuery("/", tdata.Query)), + ), + g.Text("All"), ), - g.Text("All"), ), g.Map(tdata.Sources, func(source *config.Source) g.Node { if tdata.Source != nil && tdata.Source.Name == source.Name {
M nix/package.nix → nix/package.nix
@@ -46,6 +46,12 @@ "-X" "go.alanpearce.eu/searchix/internal/config.Version=v${version}" ]; + postInstall = '' + install -d -m755 $out/lib/searchix/static + cp -r $src/frontend/static $out/lib/searchix + $out/bin/searchix-web --generate-error-page > $out/lib/searchix/error.html + ''; + modules = ../gomod2nix.toml; doCheck = false;