go-server.controller-api.mustache Maven / Gradle / Ivy
{{>partial_header}}
package {{packageName}}
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// A {{classname}}Controller binds http requests to an api service and writes the service results to the http response
type {{classname}}Controller struct {
service {{classname}}Servicer
}
// New{{classname}}Controller creates a default api controller
func New{{classname}}Controller(s {{classname}}Servicer) Router {
return &{{classname}}Controller{ service: s }
}
// Routes returns all of the api route for the {{classname}}Controller
func (c *{{classname}}Controller) Routes() Routes {
return Routes{ {{#operations}}{{#operation}}
{
"{{operationId}}",
strings.ToUpper("{{httpMethod}}"),
"{{{basePathWithoutHost}}}{{{path}}}",
c.{{operationId}},
},{{/operation}}{{/operations}}
}
}{{#operations}}{{#operation}}
// {{nickname}} - {{{summary}}}
func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Request) { {{#hasFormParams}}
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/hasFormParams}}{{#hasPathParams}}
params := mux.Vars(r){{/hasPathParams}}{{#hasQueryParams}}
query := r.URL.Query(){{/hasQueryParams}}{{#allParams}}{{#isPathParam}}{{#isLong}}
{{paramName}}, err := parseInt64Parameter(params["{{baseName}}"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}{{/isLong}}{{#isInteger}}
{{paramName}}, err := parseInt32Parameter(params["{{baseName}}"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isInteger}}{{^isLong}}{{^isInteger}}
{{paramName}} := params["{{baseName}}"]{{/isInteger}}{{/isLong}}{{/isPathParam}}{{#isQueryParam}}{{#isLong}}
{{paramName}}, err := parseInt64Parameter(query.Get("{{baseName}}"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isLong}}{{#isInteger}}
{{paramName}}, err := parseInt32Parameter(query.Get("{{baseName}}"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isInteger}}{{^isLong}}{{^isInteger}}
{{paramName}} := {{#isArray}}strings.Split({{/isArray}}query.Get("{{baseName}}"){{#isArray}}, ","){{/isArray}}{{/isInteger}}{{/isLong}}{{/isQueryParam}}{{#isFormParam}}{{#isFile}}
{{#isArray}}{{paramName}}, err := ReadFormFilesToTempFiles(r, "{{baseName}}"){{/isArray}}{{^isArray}}{{paramName}}, err := ReadFormFileToTempFile(r, "{{baseName}}"){{/isArray}}
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isFile}}{{#isLong}}
{{paramName}}, err := parseInt64Parameter( r.FormValue("{{baseName}}"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isLong}}{{#isInteger}}
{{paramName}}, err := parseInt32Parameter( r.FormValue("{{baseName}}"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isInteger}}{{^isFile}}{{^isLong}}
{{paramName}} := r.FormValue("{{baseName}}"){{/isLong}}{{/isFile}}{{/isFormParam}}{{#isHeaderParam}}
{{paramName}} := r.Header.Get("{{baseName}}"){{/isHeaderParam}}{{#isBodyParam}}
{{paramName}} := &{{dataType}}{}
if err := json.NewDecoder(r.Body).Decode(&{{paramName}}); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
{{/isBodyParam}}{{/allParams}}
result, err := c.service.{{nickname}}(r.Context(){{#allParams}}, {{#isBodyParam}}*{{/isBodyParam}}{{paramName}}{{/allParams}})
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}{{/operation}}{{/operations}}