mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-10-30 11:46:16 +00:00
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package web
|
|
|
|
import (
|
|
"github.com/bouncepaw/mycorrhiza/shroom"
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
)
|
|
|
|
func initSearch() {
|
|
http.HandleFunc("/title-search/", handlerTitleSearch)
|
|
http.HandleFunc("/title-search-json/", handlerTitleSearchJSON) // we get a little shroomy
|
|
|
|
}
|
|
|
|
func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) {
|
|
util.PrepareRq(rq)
|
|
_ = rq.ParseForm()
|
|
var (
|
|
query = rq.FormValue("q")
|
|
u = user.FromRequest(rq)
|
|
)
|
|
if shown := u.ShowLockMaybe(w, rq); shown {
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = io.WriteString(
|
|
w,
|
|
views.BaseHTML(
|
|
"Search: "+query,
|
|
views.TitleSearchHTML(query, shroom.YieldHyphaNamesContainingString),
|
|
u,
|
|
),
|
|
)
|
|
}
|
|
|
|
func handlerTitleSearchJSON(w http.ResponseWriter, rq *http.Request) {
|
|
util.PrepareRq(rq)
|
|
_ = rq.ParseForm()
|
|
var (
|
|
query = rq.FormValue("q")
|
|
u = user.FromRequest(rq)
|
|
)
|
|
if shown := u.ShowLockMaybe(w, rq); shown {
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = io.WriteString(
|
|
w,
|
|
views.TitleSearchJSON(query, shroom.YieldHyphaNamesContainingString),
|
|
)
|
|
}
|