aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authoradnano <[email protected]>2020-09-27 20:11:45 -0400
committeradnano <[email protected]>2020-09-27 20:11:45 -0400
commita4a8d49ca73b8d8a9732f719c329f47fefeea107 (patch)
tree58fe70fa50f57882a6a980c685e45e95342216ad /server.go
parentReject invalid status codes (diff)
downloadgo-gemini-a4a8d49ca73b8d8a9732f719c329f47fefeea107.tar.xz
go-gemini-a4a8d49ca73b8d8a9732f719c329f47fefeea107.zip
Add helper handler functions
Diffstat (limited to 'server.go')
-rw-r--r--server.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/server.go b/server.go
index 96747e1..7eb3aaf 100644
--- a/server.go
+++ b/server.go
@@ -175,6 +175,35 @@ type Handler interface {
Serve(*ResponseWriter, *Request)
}
+// NotFoundHandler returns a simple handler that responds to each request with
+// the status code NotFound.
+func NotFound() Handler {
+ return HandlerFunc(func(rw *ResponseWriter, req *Request) {
+ rw.WriteHeader(StatusNotFound, "Not found")
+ })
+}
+
+// Redirect returns a simple handler that responds to each request with
+// a redirect to the given URL.
+// If permanent is true, the handler will respond with a permanent redirect.
+func Redirect(url string, permanent bool) Handler {
+ return HandlerFunc(func(rw *ResponseWriter, req *Request) {
+ if permanent {
+ rw.WriteHeader(StatusRedirectPermanent, url)
+ } else {
+ rw.WriteHeader(StatusRedirect, url)
+ }
+ })
+}
+
+// Input returns a simple handler that responds to each request with
+// a request for input.
+func Input(prompt string) Handler {
+ return HandlerFunc(func(rw *ResponseWriter, req *Request) {
+ rw.WriteHeader(StatusInput, prompt)
+ })
+}
+
// ServeMux is a Gemini request multiplexer.
// It matches the URL of each incoming request against a list of registered
// patterns and calls the handler for the pattern that most closesly matches