Skip to content
Merged
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
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import:
version: ^2.1.0
- package: github.com/Masterminds/semver
version: 1.2.2
- package: github.com/metal3d/go-slugify
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
Expand Down
17 changes: 17 additions & 0 deletions hyperdrive.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package hyperdrive

import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/gorilla/mux"
slugify "github.com/metal3d/go-slugify"
)

// API is a logical collection of one or more endpoints, connecting requests
Expand Down Expand Up @@ -47,9 +50,23 @@ func (api *API) AddEndpoint(e Endpointer) {
api.Router.Handle(e.GetPath(), api.DefaultMiddlewareChain(NewMethodHandler(e)))
}

// GetMediaType returns a media type string, sans any content-type extension (e.g. json),
// based on the name of the API, the Endpoint, and the Endpoint's version. The Media Type
// produced will be used for Content Negotiation, via the Accept header, as well as routing
// to the appropriate endpoint, when the media type appears in the request headers (e.g.
// Accept and Content-Type). It will also be used, after content negotation in the
// Content-Type response header.
func (api *API) GetMediaType(e Endpointer) string {
return fmt.Sprintf("application/vnd.%s.%s.%s", slug(api.Name), slug(e.GetName()), e.GetVersion())
}

// Start starts the configured http server, listening on the configured Port
// (default: 5000). Set the PORT environment variable to change this.
func (api *API) Start() {
log.Printf("Hyperdrive API starting on PORT %d in ENVIRONMENT %s", api.conf.Port, api.conf.Env)
log.Fatal(api.Server.ListenAndServe())
}

func slug(s string) string {
return strings.ToLower(slugify.Marshal(s))
}
8 changes: 6 additions & 2 deletions hyperdrive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type HyperdriveTestSuite struct {
}

func (suite *HyperdriveTestSuite) SetupTest() {
suite.TestAPI = NewAPI("Test API", "Test API Desc")
suite.TestAPI = NewAPI("API", "Test API Desc")
suite.TestEndpoint = NewEndpoint("Test", "Test Endpoint", "/test", "1.0.1")
suite.TestHandler = NewMethodHandler(suite.TestEndpoint)
suite.TestRoot = NewRootResource(suite.TestAPI)
suite.TestEndpointRepresentation = Representation{"name": "Test", "desc": "Test Endpoint", "path": "/test", "methods": []string{"OPTIONS"}}
suite.TestRootRepresentation = Representation{"resource": "api", "name": "Test API", "endpoints": []Representation{suite.TestEndpointRepresentation}}
suite.TestRootRepresentation = Representation{"resource": "api", "name": "API", "endpoints": []Representation{suite.TestEndpointRepresentation}}
}

func (suite *HyperdriveTestSuite) TestNewAPI() {
Expand All @@ -34,6 +34,10 @@ func (suite *HyperdriveTestSuite) TestAPIServer() {
suite.IsType(&http.Server{}, suite.TestAPI.Server, "expects an instance of *http.Server")
}

func (suite *HyperdriveTestSuite) TestGetMediaType() {
suite.Equal("application/vnd.api.test.v1.0.1", suite.TestAPI.GetMediaType(suite.TestEndpoint), "returns a media type string")
}

func TestHyperdriveTestSuite(t *testing.T) {
suite.Run(t, new(HyperdriveTestSuite))
}