mirror of https://github.com/jlelse/GoBlog
Simple blogging system written in Go
https://goblog.app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
package main |
|
|
|
import ( |
|
"context" |
|
"encoding/base64" |
|
"net/http" |
|
"net/url" |
|
"path" |
|
"strings" |
|
) |
|
|
|
const defaultSearchPath = "/search" |
|
const searchPlaceholder = "{search}" |
|
|
|
func (a *goBlog) serveSearch(w http.ResponseWriter, r *http.Request) { |
|
servePath := r.Context().Value(pathKey).(string) |
|
err := r.ParseForm() |
|
if err != nil { |
|
a.serveError(w, r, err.Error(), http.StatusBadRequest) |
|
return |
|
} |
|
if q := r.Form.Get("q"); q != "" { |
|
// Clean query |
|
q = cleanHTMLText(q) |
|
// Redirect to results |
|
http.Redirect(w, r, path.Join(servePath, searchEncode(q)), http.StatusFound) |
|
return |
|
} |
|
a.render(w, r, a.renderSearch, &renderData{ |
|
Canonical: a.getFullAddress(servePath), |
|
}) |
|
} |
|
|
|
func (a *goBlog) serveSearchResult(w http.ResponseWriter, r *http.Request) { |
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{ |
|
path: r.Context().Value(pathKey).(string) + "/" + searchPlaceholder, |
|
}))) |
|
} |
|
|
|
func searchEncode(search string) string { |
|
return url.PathEscape(strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(search)), "/", "_")) |
|
} |
|
|
|
func searchDecode(encoded string) string { |
|
encoded, err := url.PathUnescape(encoded) |
|
if err != nil { |
|
return "" |
|
} |
|
encoded = strings.ReplaceAll(encoded, "_", "/") |
|
db, err := base64.StdEncoding.DecodeString(encoded) |
|
if err != nil { |
|
return "" |
|
} |
|
return string(db) |
|
}
|
|
|