diff options
-rw-r--r-- | core.go | 43 | ||||
-rw-r--r-- | http.go | 8 | ||||
-rw-r--r-- | tests/items.go | 24 | ||||
-rw-r--r-- | tests/one.go | 14 | ||||
-rw-r--r-- | tests/test.go | 21 |
5 files changed, 52 insertions, 58 deletions
@@ -21,30 +21,10 @@ type Resource interface { Delete(values ...url.Values) (int, interface{}) } -type Route struct { - resource Resource - path string -} - -func (route *Route) pathMatch(path string) bool { - return route.path == path -} +type Api struct{} -type Api struct { - routes []Route -} - -func (api *Api) matchResource(path string) Resource { - for _, route := range api.routes { - if route.pathMatch(path) { - return route.resource - } - } - return nil -} - -func (api *Api) Abort(statusCode int) (int, interface{}) { - return statusCode, map[string]string{"error": "Aborted."} +func (api *Api) Abort(rw http.ResponseWriter, statusCode int) { + rw.WriteHeader(statusCode) } type HandleFunc func(http.ResponseWriter, *http.Request) @@ -52,16 +32,14 @@ type HandleFunc func(http.ResponseWriter, *http.Request) func (api *Api) requestHandler(resource Resource) HandleFunc { return func(rw http.ResponseWriter, request *http.Request) { - var code int var data interface{} - var content []byte + var code int method := request.Method - - if request.ParseForm() == nil { - code, data = api.Abort(500) + if request.ParseForm() != nil { + api.Abort(rw, 400) + return } - values := request.Form switch method { @@ -74,12 +52,14 @@ func (api *Api) requestHandler(resource Resource) HandleFunc { case DELETE: code, data = resource.Delete(values) default: - code, data = 405, map[string]string{"error": "Not implemented!"} + api.Abort(rw, 405) + return } content, err := json.Marshal(data) if err != nil { - content, _ = json.Marshal(map[string]string{"error": "Bad response."}) + api.Abort(rw, 500) + return } rw.WriteHeader(code) @@ -94,4 +74,5 @@ func (api *Api) AddResource(resource Resource, path string) { func (api *Api) Start(port int) { portString := fmt.Sprintf(":%d", port) http.ListenAndServe(portString, nil) + fmt.Println("Hi.") } @@ -12,17 +12,17 @@ type ( ) func (GetNotSupported) Get(values ...url.Values) (int, interface{}) { - return 405, map[string]string{"error": "Not implemented"} + return 405, "" } func (PostNotSupported) Post(values ...url.Values) (int, interface{}) { - return 405, map[string]string{"error": "Not implemented"} + return 405, "" } func (PutNotSupported) Put(values ...url.Values) (int, interface{}) { - return 405, map[string]string{"error": "Not implemented"} + return 405, "" } func (DeleteNotSupported) Delete(values ...url.Values) (int, interface{}) { - return 405, map[string]string{"error": "Not implemented"} + return 405, "" } diff --git a/tests/items.go b/tests/items.go index a6f53f6..ee86c3e 100644 --- a/tests/items.go +++ b/tests/items.go @@ -1,29 +1,29 @@ package main import ( - "net/url" - "sleepy" + "net/url" + "sleepy" ) type Item struct { - sleepy.PostNotSupported - sleepy.PutNotSupported - sleepy.DeleteNotSupported + sleepy.PostNotSupported + sleepy.PutNotSupported + sleepy.DeleteNotSupported } func (item Item) Get(values ...url.Values) (int, interface{}) { - items := []string{"item1", "item2"} - data := map[string][]string{"items": items} + items := []string{"item1", "item2"} + data := map[string][]string{"items": items} - return 200, data + return 200, data } func main() { - item := new(Item) + item := new(Item) - var api = new(sleepy.Api) - api.AddResource(item, "/items") - api.Start(3000) + var api = new(sleepy.Api) + api.AddResource(item, "/items") + api.Start(3000) } diff --git a/tests/one.go b/tests/one.go new file mode 100644 index 0000000..15fd27b --- /dev/null +++ b/tests/one.go @@ -0,0 +1,14 @@ +package main + +import ( + "net/http" +) + +func response(rw http.ResponseWriter, request *http.Request) { + rw.Write([]byte("Hello world.")) +} + +func main() { + http.HandleFunc("/", response) + http.ListenAndServe(":3000", nil) +} diff --git a/tests/test.go b/tests/test.go index 4610f3d..d225072 100644 --- a/tests/test.go +++ b/tests/test.go @@ -1,25 +1,24 @@ package main import ( - "sleepy" - "net/url" + "net/url" + "sleepy" ) type Bar struct { - sleepy.PostNotSupported - sleepy.PutNotSupported - sleepy.DeleteNotSupported + sleepy.PostNotSupported + sleepy.PutNotSupported + sleepy.DeleteNotSupported } func (b Bar) Get(values ...url.Values) (int, interface{}) { - return 200, map[string]string{"hello": "goodbye"} + return 200, map[string]string{"hello": "goodbye"} } func main() { - bar := new(Bar) + bar := new(Bar) - var api = new(sleepy.Api) - api.AddResource(bar, "/bar") - api.Start(3000) + var api = new(sleepy.Api) + api.AddResource(bar, "/bar") + api.Start(3000) } - |