diff options
Diffstat (limited to 'pkg/server/server.go')
| -rw-r--r-- | pkg/server/server.go | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/pkg/server/server.go b/pkg/server/server.go index a7a5395..68750a2 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -6,6 +6,7 @@ import ( "io" "log/slog" "net/http" + "slices" "strings" "sync" "time" @@ -21,11 +22,20 @@ type Server interface { } func info(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(` - <h1>Atlas Server</h1> - <p>This is the experimental atlas server! - Try POSTing a query to <pre>/search</pre></p> - `)) + w.Write([]byte(`<h1>Atlas Server</h1> +<p>This is the experimental atlas server! +Try POSTing a query to <pre>/search</pre></p> +<hr> +<p>You can sort the results using the query param <pre>sortBy</pre> +<ul> +<li>path</li> +<li>title</li> +<li>date</li> +<li>filetime</li> +<li>meta</li> +</ul> +You can change the order using <pre>sortOrder</pre> with asc or desc +</p>`)) } func NewMux(db *data.Query) *http.ServeMux { @@ -37,7 +47,7 @@ func NewMux(db *data.Query) *http.ServeMux { } mux.HandleFunc("/", info) - mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("POST /search", func(w http.ResponseWriter, r *http.Request) { b := &strings.Builder{} if _, err := io.Copy(b, r.Body); err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -70,6 +80,16 @@ func NewMux(db *data.Query) *http.ServeMux { } } + queryParams := r.URL.Query() + if queryParams.Has("sortBy") { + sortBy := queryParams.Get("sortBy") + sortOrder := queryParams.Get("sortOrder") + docCmp, ok := index.NewDocCmp(sortBy, sortOrder == "desc" || sortOrder == "descending") + if ok { + slices.SortFunc(docs, docCmp) + } + } + if !maxFileTime.IsZero() { w.Header().Add("Last-Modified", maxFileTime.UTC().Format(http.TimeFormat)) } |
