1
0
Fork 0

Initial scaffold for search

main
Ambrose Chua 2020-11-22 16:59:41 +08:00
parent 7b5aeeb87e
commit 70f92f299e
4 changed files with 29 additions and 7 deletions

12
app.go
View File

@ -53,8 +53,8 @@ type appRequest struct {
}
type appSearch struct {
App Datetime
Req map[string]string
App Datetime
Search []*data.City
}
// index handles all incoming page requests
@ -111,8 +111,12 @@ func (app Datetime) search(w http.ResponseWriter, req *http.Request) {
}
// TODO: do search
search := map[string]string{
"hi": "hello",
query := req.URL.Query()
search, err := FullSearchCities(app.cities, query.Get("zone"))
if err != nil {
l.Error("search failed", zap.Error(err))
app.error(HTTPError{http.StatusInternalServerError, err}, w, req)
return
}
l.Debug("rendering template", zap.Reflect("search", search))

11
search.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"github.com/serverwentdown/datetime.link/data"
)
// FullSearchCities uses a very basic iterative method to search for a city
// with the given string
func FullSearchCities(cities map[string]*data.City, zone string) ([]*data.City, error) {
return nil, nil
}

View File

@ -19,13 +19,20 @@ func (app Datetime) loadTemplate(name string, w http.ResponseWriter, req *http.R
accept := req.Header.Get("Accept")
tmpl, contentType, acceptable := app.chooseTemplate(accept, name)
if !acceptable {
app.simpleError(HTTPError{http.StatusNotAcceptable, nil}, w, req)
if name == "error" {
app.simpleError(HTTPError{http.StatusNotAcceptable, nil}, w, req)
return nil
}
app.error(HTTPError{http.StatusNotAcceptable, nil}, w, req)
return nil
}
if tmpl == nil {
err := fmt.Errorf("%w \"%s\" for \"%s\"", ErrTemplateNotFound, name, accept)
l.Warn("unable to find template", zap.Error(err), zap.String("name", name), zap.String("accept", accept))
app.simpleError(HTTPError{http.StatusNotAcceptable, err}, w, req)
if name == "error" {
app.simpleError(HTTPError{http.StatusNotAcceptable, err}, w, req)
}
app.error(HTTPError{http.StatusNotAcceptable, err}, w, req)
//app.simpleError(HTTPError{http.StatusInternalServerError, ErrNoTemplate}, w, req)
return nil
}

View File

@ -1 +1 @@
{{.Req | jsonMarshal | thisIsSafe }}
{{.Search | jsonMarshal | thisIsSafe }}