Rename Responder to Handler
This commit is contained in:
parent
29f2b3738d
commit
7910ed433b
9
fs.go
9
fs.go
@ -13,11 +13,12 @@ func init() {
|
||||
mime.AddExtensionType(".gemini", "text/gemini")
|
||||
}
|
||||
|
||||
// FileServer takes a filesystem and returns a Responder which uses that filesystem.
|
||||
// The returned Responder cleans paths before handling them.
|
||||
// FileServer returns a handler that serves Gemini requests with the contents
|
||||
// of the file system rooted at root.
|
||||
// The returned handler cleans paths before handling them.
|
||||
//
|
||||
// TODO: Use io/fs.FS when available.
|
||||
func FileServer(fsys FS) Responder {
|
||||
func FileServer(fsys FS) Handler {
|
||||
return fsHandler{fsys}
|
||||
}
|
||||
|
||||
@ -25,7 +26,7 @@ type fsHandler struct {
|
||||
FS
|
||||
}
|
||||
|
||||
func (fsh fsHandler) Respond(w *ResponseWriter, r *Request) {
|
||||
func (fsh fsHandler) ServeGemini(w *ResponseWriter, r *Request) {
|
||||
p := path.Clean(r.URL.Path)
|
||||
f, err := fsh.Open(p)
|
||||
if err != nil {
|
||||
|
28
mux.go
28
mux.go
@ -50,7 +50,7 @@ type ServeMux struct {
|
||||
}
|
||||
|
||||
type muxEntry struct {
|
||||
r Responder
|
||||
r Handler
|
||||
pattern string
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ func cleanPath(p string) string {
|
||||
|
||||
// Find a handler on a handler map given a path string.
|
||||
// Most-specific (longest) pattern wins.
|
||||
func (mux *ServeMux) match(path string) Responder {
|
||||
func (mux *ServeMux) match(path string) Handler {
|
||||
// Check for exact match first.
|
||||
v, ok := mux.m[path]
|
||||
if ok {
|
||||
@ -130,9 +130,9 @@ func (mux *ServeMux) shouldRedirectRLocked(path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Respond dispatches the request to the responder whose
|
||||
// ServeGemini dispatches the request to the handler whose
|
||||
// pattern most closely matches the request URL.
|
||||
func (mux *ServeMux) Respond(w *ResponseWriter, r *Request) {
|
||||
func (mux *ServeMux) ServeGemini(w *ResponseWriter, r *Request) {
|
||||
path := cleanPath(r.URL.Path)
|
||||
|
||||
// If the given path is /tree and its handler is not registered,
|
||||
@ -157,19 +157,19 @@ func (mux *ServeMux) Respond(w *ResponseWriter, r *Request) {
|
||||
w.Status(StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.Respond(w, r)
|
||||
resp.ServeGemini(w, r)
|
||||
}
|
||||
|
||||
// Handle registers the responder for the given pattern.
|
||||
// If a responder already exists for pattern, Handle panics.
|
||||
func (mux *ServeMux) Handle(pattern string, responder Responder) {
|
||||
// Handle registers the handler for the given pattern.
|
||||
// If a handler already exists for pattern, Handle panics.
|
||||
func (mux *ServeMux) Handle(pattern string, handler Handler) {
|
||||
mux.mu.Lock()
|
||||
defer mux.mu.Unlock()
|
||||
|
||||
if pattern == "" {
|
||||
panic("gemini: invalid pattern")
|
||||
}
|
||||
if responder == nil {
|
||||
if handler == nil {
|
||||
panic("gemini: nil responder")
|
||||
}
|
||||
if _, exist := mux.m[pattern]; exist {
|
||||
@ -179,7 +179,7 @@ func (mux *ServeMux) Handle(pattern string, responder Responder) {
|
||||
if mux.m == nil {
|
||||
mux.m = make(map[string]muxEntry)
|
||||
}
|
||||
e := muxEntry{responder, pattern}
|
||||
e := muxEntry{handler, pattern}
|
||||
mux.m[pattern] = e
|
||||
if pattern[len(pattern)-1] == '/' {
|
||||
mux.es = appendSorted(mux.es, e)
|
||||
@ -201,10 +201,10 @@ func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
|
||||
return es
|
||||
}
|
||||
|
||||
// HandleFunc registers the responder function for the given pattern.
|
||||
func (mux *ServeMux) HandleFunc(pattern string, responder func(*ResponseWriter, *Request)) {
|
||||
if responder == nil {
|
||||
// HandleFunc registers the handler function for the given pattern.
|
||||
func (mux *ServeMux) HandleFunc(pattern string, handler func(*ResponseWriter, *Request)) {
|
||||
if handler == nil {
|
||||
panic("gemini: nil responder")
|
||||
}
|
||||
mux.Handle(pattern, ResponderFunc(responder))
|
||||
mux.Handle(pattern, HandlerFunc(handler))
|
||||
}
|
||||
|
52
server.go
52
server.go
@ -37,35 +37,36 @@ type Server struct {
|
||||
ErrorLog *log.Logger
|
||||
|
||||
// registered responders
|
||||
responders map[responderKey]Responder
|
||||
responders map[handlerKey]Handler
|
||||
hosts map[string]bool
|
||||
}
|
||||
|
||||
type responderKey struct {
|
||||
type handlerKey struct {
|
||||
scheme string
|
||||
hostname string
|
||||
}
|
||||
|
||||
// Handle registers a responder for the given pattern.
|
||||
// Handle registers the handler for the given pattern.
|
||||
// If a handler already exists for pattern, Handle panics.
|
||||
//
|
||||
// The pattern must be in the form of "hostname" or "scheme://hostname".
|
||||
// If no scheme is specified, a scheme of "gemini://" is implied.
|
||||
// Wildcard patterns are supported (e.g. "*.example.com").
|
||||
// To handle any hostname, use the wildcard pattern "*".
|
||||
func (s *Server) Handle(pattern string, responder Responder) {
|
||||
func (s *Server) Handle(pattern string, handler Handler) {
|
||||
if pattern == "" {
|
||||
panic("gemini: invalid pattern")
|
||||
}
|
||||
if responder == nil {
|
||||
if handler == nil {
|
||||
panic("gemini: nil responder")
|
||||
}
|
||||
if s.responders == nil {
|
||||
s.responders = map[responderKey]Responder{}
|
||||
s.responders = map[handlerKey]Handler{}
|
||||
s.hosts = map[string]bool{}
|
||||
}
|
||||
|
||||
split := strings.SplitN(pattern, "://", 2)
|
||||
var key responderKey
|
||||
var key handlerKey
|
||||
if len(split) == 2 {
|
||||
key.scheme = split[0]
|
||||
key.hostname = split[1]
|
||||
@ -77,13 +78,13 @@ func (s *Server) Handle(pattern string, responder Responder) {
|
||||
if _, ok := s.responders[key]; ok {
|
||||
panic("gemini: multiple registrations for " + pattern)
|
||||
}
|
||||
s.responders[key] = responder
|
||||
s.responders[key] = handler
|
||||
s.hosts[key.hostname] = true
|
||||
}
|
||||
|
||||
// HandleFunc registers a responder function for the given pattern.
|
||||
func (s *Server) HandleFunc(pattern string, responder func(*ResponseWriter, *Request)) {
|
||||
s.Handle(pattern, ResponderFunc(responder))
|
||||
// HandleFunc registers the handler function for the given pattern.
|
||||
func (s *Server) HandleFunc(pattern string, handler func(*ResponseWriter, *Request)) {
|
||||
s.Handle(pattern, HandlerFunc(handler))
|
||||
}
|
||||
|
||||
// ListenAndServe listens for requests at the server's configured address.
|
||||
@ -225,20 +226,20 @@ func (s *Server) respond(conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Respond(w, req)
|
||||
resp.ServeGemini(w, req)
|
||||
}
|
||||
|
||||
func (s *Server) responder(r *Request) Responder {
|
||||
if h, ok := s.responders[responderKey{r.URL.Scheme, r.URL.Hostname()}]; ok {
|
||||
func (s *Server) responder(r *Request) Handler {
|
||||
if h, ok := s.responders[handlerKey{r.URL.Scheme, r.URL.Hostname()}]; ok {
|
||||
return h
|
||||
}
|
||||
wildcard := strings.SplitN(r.URL.Hostname(), ".", 2)
|
||||
if len(wildcard) == 2 {
|
||||
if h, ok := s.responders[responderKey{r.URL.Scheme, "*." + wildcard[1]}]; ok {
|
||||
if h, ok := s.responders[handlerKey{r.URL.Scheme, "*." + wildcard[1]}]; ok {
|
||||
return h
|
||||
}
|
||||
}
|
||||
if h, ok := s.responders[responderKey{r.URL.Scheme, "*"}]; ok {
|
||||
if h, ok := s.responders[handlerKey{r.URL.Scheme, "*"}]; ok {
|
||||
return h
|
||||
}
|
||||
return nil
|
||||
@ -252,15 +253,20 @@ func (s *Server) logf(format string, args ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// A Responder responds to a Gemini request.
|
||||
type Responder interface {
|
||||
// Respond accepts a Request and constructs a Response.
|
||||
Respond(*ResponseWriter, *Request)
|
||||
// A Handler responds to a Gemini request.
|
||||
//
|
||||
// ServeGemini should write the response header and data to the ResponseWriter
|
||||
// and then return.
|
||||
type Handler interface {
|
||||
ServeGemini(*ResponseWriter, *Request)
|
||||
}
|
||||
|
||||
// ResponderFunc is a wrapper around a bare function that implements Responder.
|
||||
type ResponderFunc func(*ResponseWriter, *Request)
|
||||
// The HandlerFunc type is an adapter to allow the use of ordinary functions
|
||||
// as Gemini handlers. If f is a function with the appropriate signature,
|
||||
// HandlerFunc(f) is a Handler that calls f.
|
||||
type HandlerFunc func(*ResponseWriter, *Request)
|
||||
|
||||
func (f ResponderFunc) Respond(w *ResponseWriter, r *Request) {
|
||||
// ServeGemini calls f(w, r).
|
||||
func (f HandlerFunc) ServeGemini(w *ResponseWriter, r *Request) {
|
||||
f(w, r)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user