cavepedia/src/web.go

172 lines
3.4 KiB
Go

package main
import (
"html/template"
"net/http"
"strings"
"git.seaturtle.pw/pew/cavepedia/utils"
"github.com/blevesearch/bleve/v2/search"
)
func GetHome(w http.ResponseWriter, r *http.Request) {
// Bypass auth
if utils.GetConfig().Password == "" {
http.Redirect(w, r, "/search", http.StatusTemporaryRedirect)
return
}
// if auth'd --> redirect to /
auth, ok := getJWT(w, r)
if !ok {
return
}
if auth {
http.Redirect(w, r, "/search", http.StatusTemporaryRedirect)
return
}
// return index.html
t, err := template.ParseFS(templatesHTML, "templates/index.html.tmpl")
if !checkWebError(w, err) {
return
}
err = t.Execute(w, struct {
TryAgain bool
}{
TryAgain: false,
})
checkError(err)
}
func GetSearch(w http.ResponseWriter, r *http.Request) {
// Check auth
if utils.GetConfig().Password != "" {
// if not auth'd --> redirect to /
auth, ok := getJWT(w, r)
if !ok {
return
}
if !auth {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
}
// Lookup params
_, exists := r.URL.Query()["exact"]
fuzziness := 2
if exists {
fuzziness = 0
}
errorMsg := getURLParam(r, "error")
placeholder := getURLParam(r, "placeholder")
term := getURLParam(r, "term")
// We don't actually pass the message in, just true if there's an error
if errorMsg != "" {
errorMsg = "Invalid query syntax! Please try again."
}
// Invalid query and/or show placeholder
if errorMsg != "" || term == "" {
t, err := template.ParseFS(templatesHTML, "templates/search.html.tmpl")
if !checkWebError(w, err) {
return
}
err = t.Execute(w, struct {
ErrorMsg string
Placeholder string
}{
ErrorMsg: errorMsg,
Placeholder: placeholder,
})
checkError(err)
return
}
// Do search
results, err := doSearch(term, fuzziness)
if err != nil {
http.Redirect(w, r, "/search?error=true&placeholder="+term, http.StatusTemporaryRedirect)
return
}
t, err := template.ParseFS(templatesHTML, "templates/results.html.tmpl")
if !checkWebError(w, err) {
return
}
err = t.Execute(w, struct {
Results search.DocumentMatchCollection
Term string
}{
Results: results,
Term: term,
})
checkError(err)
}
func PostQuiz(w http.ResponseWriter, r *http.Request) {
if !checkWebError(w, r.ParseForm()) {
return
}
// valid quiz
if strings.EqualFold(r.Form.Get("answer"), utils.GetConfig().Password) {
// create JWT
if !setJWT(w) {
return
}
http.Redirect(w, r, "/search", http.StatusTemporaryRedirect)
return
}
// invalid quiz
t, err := template.ParseFS(templatesHTML, "templates/index.html.tmpl")
if !checkWebError(w, err) {
return
}
err = t.Execute(w, struct {
TryAgain bool
}{
TryAgain: true,
})
checkError(err)
}
func GetPDF(w http.ResponseWriter, r *http.Request) {
category := getURLParam(r, "category")
file := getURLParam(r, "file")
filename := getURLParam(r, "filename")
pageNum := getURLParam(r, "pageNum")
season := getURLParam(r, "season")
term := getURLParam(r, "term")
t, err := template.ParseFS(templatesHTML, "templates/pdf.html.tmpl")
if !checkWebError(w, err) {
return
}
err = t.Execute(w, struct {
Category string
File string
Filename string
PageNum string
Season string
Term string
}{
Category: category,
File: file,
Filename: filename,
PageNum: pageNum,
Season: season,
Term: term,
})
if !checkError(err) {
return
}
}