all repos — searchix @ 078020ea68fd850a3d1dcfcdfbcb2ba61312161b

Search engine for NixOS, nix-darwin, home-manager and NUR users

internal/server/source.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package server

import (
	"encoding/xml"
	"fmt"
	"net/http"

	"github.com/Southclaws/fault/ftag"

	"alin.ovh/searchix/internal/components"
	"alin.ovh/searchix/internal/config"
	"alin.ovh/searchix/internal/index"
	"alin.ovh/searchix/internal/opensearch"
)

type SourceHandler struct {
	global *GlobalHandler
	source config.Source
	mux    *http.ServeMux
}

func (h *SourceHandler) getTitle(doc index.Indexable) string {
	title := ""
	if doc != nil {
		title = doc.GetName() + " ‐ "
	}

	title += h.source.Name + " ‐ "

	return title
}

func (h *SourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	h.mux.ServeHTTP(w, r)
}

func (h *SourceHandler) Search(w http.ResponseWriter, r *http.Request) {
	h.global.Search(h.source, w, r)
}

func (h *SourceHandler) Detail(w http.ResponseWriter, r *http.Request) {
	doc, err := h.global.index.GetDocument(r.Context(), h.source, r.PathValue("id"))
	if err != nil {
		if ftag.Get(err) == ftag.NotFound {
			h.global.log.Warn("document not found", "source", h.source.Key, "id", r.PathValue("id"))
			h.global.errorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound)

			return
		}

		h.global.log.Error(
			"failed to get document",
			"source",
			h.source.Key,
			"id",
			r.PathValue("id"),
			"error",
			err,
		)
		h.global.errorHandler(
			w,
			r,
			http.StatusText(http.StatusInternalServerError),
			http.StatusInternalServerError,
		)

		return
	}

	if doc == nil {
		h.global.errorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound)

		return
	}

	tdata := components.TemplateData{
		ExtraHeadHTML: h.global.cfg.Web.ExtraHeadHTML,
		Source:        h.source,
		Sources:       sources,
		Assets:        h.global.assets,
		Title:         h.getTitle(doc),
	}
	var baseErr error
	if r.Header.Get("Fetch") == "true" {
		w.Header().Add("Content-Type", "text/html; charset=utf-8")
		w.Header().Add("Title", h.getTitle(doc))
		baseErr = components.Detail(doc).Render(w)
	} else {
		baseErr = components.DetailPage(tdata, doc).Render(w)
	}
	if baseErr != nil {
		h.global.log.Error("template error", "template", h.source.Importer.Singular(), "error", baseErr)
		h.global.errorHandler(w, r, baseErr.Error(), http.StatusInternalServerError)
	}
}

func (h *SourceHandler) OpenSearchXML(w http.ResponseWriter, _ *http.Request) {
	w.Header().Add("Cache-Control", "max-age=604800")
	w.Header().Set("Content-Type", "application/opensearchdescription+xml")
	osd := &opensearch.Description{
		ShortName:   fmt.Sprintf("Searchix %s", h.source),
		LongName:    fmt.Sprintf("Search %s with Searchix", h.source),
		Description: fmt.Sprintf("Search %s", h.source),
		SearchForm: h.global.cfg.Web.BaseURL.JoinPath(
			h.source.Importer.String(),
			h.source.Key,
			"search",
		),
		Image: opensearch.Image{
			Height:  32,
			Width:   32,
			Type:    "image/x-icon",
			Content: h.global.cfg.Web.BaseURL.JoinPath("favicon.ico").String(),
		},
		URL: opensearch.URL{
			Method: "get",
			Type:   "text/html",
			Template: h.global.cfg.Web.BaseURL.JoinPath(
				h.source.Importer.String(),
				h.source.Key,
				"search",
			).AddRawQuery("query", "{searchTerms}"),
		},
	}
	enc := xml.NewEncoder(w)
	enc.Indent("", "    ")
	err := enc.Encode(osd)
	if err != nil {
		// no h.global.errorHandler; HTML does not make sense here
		http.Error(
			w,
			fmt.Sprintf("OpenSearch XML encoding error: %v", err),
			http.StatusInternalServerError,
		)
	}
}