added doormen ownership
This commit is contained in:
1 parent
f0628e4682
commit
f7a8d416c1
27 files changed
+489
-126
No files matched your search
+54
-8
@@ -1,13 +1,17 @@
|
||||
package simpleapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
)
|
||||
|
||||
func JSONHendler(handler http.HandlerFunc) http.HandlerFunc {
|
||||
type MiddleWare func(handler http.HandlerFunc) http.HandlerFunc
|
||||
|
||||
func JSONHandler(handler http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, request *http.Request) {
|
||||
w.Header().Add("Content-Type", mime.TypeByExtension(".json"))
|
||||
handler(w, request)
|
||||
@@ -21,25 +25,67 @@ type Endpoint struct {
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
func (ep *Endpoint) MarshalJSON() ([]byte, error) {
|
||||
m := map[string]string{
|
||||
"pattern": path.Join(ep.Pattern),
|
||||
"description": ep.Description,
|
||||
"method": ep.Method,
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
type SimpleApi struct {
|
||||
apiEndpoints []*Endpoint
|
||||
router *mux.Router
|
||||
prefix string
|
||||
apiEndpoints []*Endpoint
|
||||
router *mux.Router
|
||||
prefix string
|
||||
middlewares []MiddleWare
|
||||
documentationRoute string
|
||||
}
|
||||
|
||||
func New(prefix string, apiEndpoints ...*Endpoint) *SimpleApi {
|
||||
api := &SimpleApi{apiEndpoints, nil, prefix}
|
||||
api.initRouter()
|
||||
api := &SimpleApi{apiEndpoints, nil, prefix, make([]MiddleWare, 0), ""}
|
||||
api.InitRouter()
|
||||
return api
|
||||
}
|
||||
|
||||
func (api *SimpleApi) initRouter() {
|
||||
func (api *SimpleApi) DocumentationHandler(w http.ResponseWriter, req *http.Request) {
|
||||
enc := json.NewEncoder(w)
|
||||
payload := map[string]interface{}{"endpoints": api.apiEndpoints}
|
||||
if e := enc.Encode(payload); e != nil {
|
||||
fmt.Fprintln(w, e)
|
||||
}
|
||||
}
|
||||
|
||||
func (api *SimpleApi) EnableDocumentation(route string) {
|
||||
if route == "" {
|
||||
return
|
||||
}
|
||||
api.documentationRoute = route
|
||||
api.router.Path(path.Join(api.prefix, route)).
|
||||
HandlerFunc(JSONHandler(api.DocumentationHandler))
|
||||
}
|
||||
|
||||
func (api *SimpleApi) AddMiddlewares(middlewares ...MiddleWare) {
|
||||
api.middlewares = append(api.middlewares, middlewares...)
|
||||
}
|
||||
|
||||
func (api *SimpleApi) applyMiddlewares(handler http.HandlerFunc) http.HandlerFunc {
|
||||
ret := handler
|
||||
for _, middleware := range api.middlewares {
|
||||
ret = middleware(ret)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (api *SimpleApi) InitRouter() {
|
||||
api.router = mux.NewRouter()
|
||||
for _, apiEndpoint := range api.apiEndpoints {
|
||||
handler := JSONHandler(api.applyMiddlewares(apiEndpoint.HandlerFunc))
|
||||
api.router.Path(path.Join(api.prefix, apiEndpoint.Pattern)).
|
||||
Methods(apiEndpoint.Method).
|
||||
HandlerFunc(JSONHendler(apiEndpoint.HandlerFunc))
|
||||
HandlerFunc(handler)
|
||||
}
|
||||
api.EnableDocumentation(api.documentationRoute)
|
||||
}
|
||||
|
||||
func (api *SimpleApi) Set404Handler(handler http.HandlerFunc) {
|
||||
|
||||
@@ -135,3 +135,77 @@ func TestServePrefix(t *testing.T) {
|
||||
t.Error(rd)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentationHandler(t *testing.T) {
|
||||
api := New("/api", GetMockApiEndpoint("/foo/bar", "GET"))
|
||||
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
api.DocumentationHandler(w, req)
|
||||
if code := w.Code; code != 200 {
|
||||
t.Error("bad status", code)
|
||||
}
|
||||
body := `{"endpoints":[{"description":"","method":"GET","pattern":"/foo/bar"}]}` + "\n"
|
||||
if body != w.Body.String() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnableDocumentation(t *testing.T) {
|
||||
api := New("/api", GetMockApiEndpoint("/foo/bar", "GET"))
|
||||
api.EnableDocumentation("/doc")
|
||||
ts := httptest.NewServer(api)
|
||||
defer ts.Close()
|
||||
res, err := http.Get(ts.URL + "/api/doc")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
t.Error("should have status 200 but got", res.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnableDocumentationAndIniting(t *testing.T) {
|
||||
api := New("/api", GetMockApiEndpoint("/foo/bar", "GET"))
|
||||
api.EnableDocumentation("/doc")
|
||||
api.InitRouter()
|
||||
ts := httptest.NewServer(api)
|
||||
defer ts.Close()
|
||||
res, err := http.Get(ts.URL + "/api/doc")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
t.Error("should have status 200 but got", res.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMiddleWares(t *testing.T) {
|
||||
api := New("/api", GetMockApiEndpoint("/foo", "GET"))
|
||||
middleWare1 := func(f http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path += "/1"
|
||||
f(w, r)
|
||||
}
|
||||
}
|
||||
middleWare2 := func(f http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path += "/2"
|
||||
f(w, r)
|
||||
}
|
||||
}
|
||||
api.AddMiddlewares(middleWare1, middleWare2)
|
||||
api.InitRouter()
|
||||
ts := httptest.NewServer(api)
|
||||
defer ts.Close()
|
||||
if rd, err := GET(ts, "/api/foo"); err != nil {
|
||||
t.Error(err)
|
||||
} else if rd.Path != "/api/foo/2/1" {
|
||||
t.Error(rd.Path)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user