Skip to content
Merged
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
25 changes: 24 additions & 1 deletion hyperdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func NewAPI(name string, desc string) API {
// HTTP method.
func (api *API) AddEndpoint(e Endpointer) {
api.Root.AddEndpoint(e)
api.Router.Handle(e.GetPath(), api.DefaultMiddlewareChain(NewMethodHandler(e)))
api.Router.Handle(e.GetPath(), api.DefaultMiddlewareChain(NewMethodHandler(e))).
Headers("Accept", api.GetContentTypeJSON(e)).
Headers("Accet", api.GetContentTypeXML(e))
}

// GetMediaType returns a media type string, sans any content-type extension (e.g. json),
Expand All @@ -58,6 +60,27 @@ func (api *API) GetMediaType(e Endpointer) string {
return fmt.Sprintf("application/vnd.%s.%s.%s", slug(api.Name), slug(e.GetName()), e.GetVersion())
}

// GetContentTypeJSON returns the json Content-Type an endpoint can accept and
// respond with. The Content-Type will include the versioned vendor Media
// Type returned by API.GetMediaType() with a json extension.
func (api *API) GetContentTypeJSON(e Endpointer) string {
return fmt.Sprintf("%s.json", api.GetMediaType(e))
}

// GetContentTypeXML returns the xml Content-Type an endpoint can accept and
// respond with. The Content-Type will include the versioned vendor Media
// Type returned by API.GetMediaType() with an xml extension.
func (api *API) GetContentTypeXML(e Endpointer) string {
return fmt.Sprintf("%s.xml", api.GetMediaType(e))
}

// GetContentTypes returns a slice of Content-Types the endpoint can accept and
// respond with. The Content-Types will include both the versioned vendor Media
// Type returned by API.GetMediaType() for both json and xml.
func (api *API) GetContentTypes(e Endpointer) []string {
return []string{api.GetContentTypeJSON(e), api.GetContentTypeXML(e)}
}

// 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() {
Expand Down