Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD CUSTOM METHODS #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ func (m *Mux) HandleFunc(pattern string, handlerFunc func(http.ResponseWriter, *
m.Handle(pattern, http.HandlerFunc(handlerFunc))
}

// Get registers a route handler function for a path pattern for a GET Request
func (m *Mux) Get(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, Methods().HandleFunc(http.MethodGet, handlerFunc))
}

// Post registers a route handler function for a path pattern for a POST Request
func (m *Mux) Post(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, Methods().HandleFunc(http.MethodPost, handlerFunc))
}

// Put registers a route handler function for a path pattern with a GET Request
func (m *Mux) Put(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, Methods().HandleFunc(http.MethodPut, handlerFunc))
}

// Delete registers a route handler function for a path pattern with a DELETE Request
func (m *Mux) Delete(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, Methods().HandleFunc(http.MethodDelete, handlerFunc))
}

// Purge registers a route handler function for a path pattern with a PURGE Request
func (m *Mux) Purge(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, Methods().HandleFunc(http.MethodPurge, handlerFunc))
}

// Purge registers a route handler function for a path pattern with a OPTIONS Request
func (m *Mux) Options(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
m.Handle(pattern, Methods().HandleFunc(http.MethodOptions, handlerFunc))
}

// ServeHTTP exposes and serves the registered routes.
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, h := range m.requestHandlers {
Expand Down Expand Up @@ -212,6 +242,12 @@ type SubMux interface {
Handle(pattern string, handler http.Handler)
HandleFunc(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
AbsPath() string
Get(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
Post(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
Put(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
Delete(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
Purge(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
Options(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
}

// Of returns a new Mux which its Handle and HandleFunc will register the path based on given "prefix", i.e:
Expand Down
97 changes: 96 additions & 1 deletion params_writer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package muxie

import (
"bufio"
"errors"
"net"
"net/http"
)

Expand Down Expand Up @@ -57,11 +60,39 @@ type ResponseWriter interface {
ParamsSetter
Get(string) string
GetAll() []ParamEntry
// Status returns the status code of the response or 0 if the response has
// not been written
Status() int
// Written returns whether or not the ResponseWriter has been written.
Written() bool
// Size returns the size of the response body.
Size() int
// Before allows for a function to be called before the ResponseWriter has been written to. This is
// useful for setting headers or any other operations that must happen before a response has been written.
Before(func(ResponseWriter))
}

type beforeFunc func(ResponseWriter)

type paramsWriter struct {
http.ResponseWriter
params []ParamEntry
params []ParamEntry
status int
size int
beforeFuncs []beforeFunc
}

// NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
func NewResponseWriter(pw http.ResponseWriter) ResponseWriter {
npw := &paramsWriter{
ResponseWriter: pw,
}

if _, ok := pw.(http.CloseNotifier); ok {
return &responseWriterCloseNotifer{npw}
}

return npw
}

var _ ResponseWriter = (*paramsWriter)(nil)
Expand All @@ -84,6 +115,52 @@ func (pw *paramsWriter) Set(key, value string) {
})
}

func (pw *paramsWriter) WriteHeader(s int) {
pw.status = s
pw.ResponseWriter.WriteHeader(s)
}

func (pw *paramsWriter) Write(b []byte) (int, error) {
if !pw.Written() {
// The status will be StatusOK if WriteHeader has not been called yet
pw.WriteHeader(http.StatusOK)
}
size, err := pw.ResponseWriter.Write(b)
pw.size += size
return size, err
}

func (pw *paramsWriter) Flush() {
flusher, ok := pw.ResponseWriter.(http.Flusher)
if ok {
if !pw.Written() {
// The status will be StatusOK if WriteHeader has not been called yet
pw.WriteHeader(http.StatusOK)
}
flusher.Flush()
}
}

func (pw *paramsWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := pw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}

func (pw *paramsWriter) Status() int {
return pw.status
}

func (pw *paramsWriter) Size() int {
return pw.size
}

func (pw *paramsWriter) Written() bool {
return pw.status != 0
}

// Get returns the value of the associated parameter based on its key/name.
func (pw *paramsWriter) Get(key string) string {
n := len(pw.params)
Expand All @@ -96,6 +173,16 @@ func (pw *paramsWriter) Get(key string) string {
return ""
}

func (pw *paramsWriter) Before(before func(ResponseWriter)) {
pw.beforeFuncs = append(pw.beforeFuncs, before)
}

func (pw *paramsWriter) callBefore() {
for i := len(pw.beforeFuncs) - 1; i >= 0; i-- {
pw.beforeFuncs[i](pw)
}
}

// GetAll returns all the path parameters keys-values.
func (pw *paramsWriter) GetAll() []ParamEntry {
return pw.params
Expand All @@ -105,3 +192,11 @@ func (pw *paramsWriter) reset(w http.ResponseWriter) {
pw.ResponseWriter = w
pw.params = pw.params[0:0]
}

type responseWriterCloseNotifer struct {
*paramsWriter
}

func (pw *responseWriterCloseNotifer) CloseNotify() <-chan bool {
return pw.ResponseWriter.(http.CloseNotifier).CloseNotify()
}