aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml7
-rw-r--r--core.go23
-rw-r--r--core_test.go (renamed from tests/sleepy_test.go)24
-rwxr-xr-xscript/build4
-rwxr-xr-xscript/test3
5 files changed, 42 insertions, 19 deletions
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..068fe6a
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,7 @@
+language: go
+go:
+ - 1.1.2
+ - 1.2
+install:
+ - script/build
+script: script/test
diff --git a/core.go b/core.go
index 6023014..7caaca2 100644
--- a/core.go
+++ b/core.go
@@ -46,7 +46,8 @@ type DeleteSupported interface {
// You can instantiate multiple APIs on separate ports. Each API
// will manage its own set of resources.
type API struct {
- mux *http.ServeMux
+ muxPointer *http.ServeMux
+ muxInitialized bool
}
// NewAPI allocates and returns a new API.
@@ -105,23 +106,31 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc {
}
}
+// singleton mux
+func (api *API) Mux() *http.ServeMux {
+ if api.muxInitialized {
+ return api.muxPointer
+ } else {
+ api.muxPointer = http.NewServeMux()
+ api.muxInitialized = true
+ return api.muxPointer
+ }
+}
+
// AddResource adds a new resource to an API. The API will route
// requests that match one of the given paths to the matching HTTP
// method on the resource.
func (api *API) AddResource(resource interface{}, paths ...string) {
- if api.mux == nil {
- api.mux = http.NewServeMux()
- }
for _, path := range paths {
- api.mux.HandleFunc(path, api.requestHandler(resource))
+ api.Mux().HandleFunc(path, api.requestHandler(resource))
}
}
// Start causes the API to begin serving requests on the given port.
func (api *API) Start(port int) error {
- if api.mux == nil {
+ if !api.muxInitialized {
return errors.New("You must add at least one resource to this API.")
}
portString := fmt.Sprintf(":%d", port)
- return http.ListenAndServe(portString, api.mux)
+ return http.ListenAndServe(portString, api.Mux())
}
diff --git a/tests/sleepy_test.go b/core_test.go
index 655d0a6..196c52f 100644
--- a/tests/sleepy_test.go
+++ b/core_test.go
@@ -1,11 +1,11 @@
-package main
+package sleepy
import (
"github.com/dougblack/sleepy"
+ "io/ioutil"
+ "net/http"
"net/url"
- "net/http"
- "testing"
- "io/ioutil"
+ "testing"
)
type Item struct{}
@@ -23,12 +23,12 @@ func TestBasicGet(t *testing.T) {
var api = sleepy.NewAPI()
api.AddResource(item, "/items", "/bar", "/baz")
go api.Start(3000)
- resp, err := http.Get("http://localhost:3000/items")
- if err != nil {
- t.Error(err)
- }
- body, _ := ioutil.ReadAll(resp.Body)
- if string(body) != `{"items":["item1","item2"]}` {
- t.Error("Not equal.")
- }
+ resp, err := http.Get("http://localhost:3000/items")
+ if err != nil {
+ t.Error(err)
+ }
+ body, _ := ioutil.ReadAll(resp.Body)
+ if string(body) != `{"items":["item1","item2"]}` {
+ t.Error("Not equal.")
+ }
}
diff --git a/script/build b/script/build
new file mode 100755
index 0000000..980a1a2
--- /dev/null
+++ b/script/build
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+go get -d -v ./...
+go build -v ./...
diff --git a/script/test b/script/test
new file mode 100755
index 0000000..ff2d632
--- /dev/null
+++ b/script/test
@@ -0,0 +1,3 @@
+script/build
+go fmt ./...
+go test -v ./...
bgstack15