Richard Lavoie 10897caf37
[go-server] fix imports with go generation (#18514)
* fix imports with go generation

* Wrong copy over

* Missing new line

* tab vs space

* Fix new line between router and std go libs

* Add both use case, add samples to CI validation

* Update samples
2024-05-01 18:13:03 +08:00

78 lines
1.9 KiB
Go

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* Simple no path and body param spec
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 1.0.0
*/
package petstoreserver
import (
"net/http"
"strings"
"github.com/gorilla/mux"
)
// PathAPIController binds http requests to an api service and writes the service results to the http response
type PathAPIController struct {
service PathAPIServicer
errorHandler ErrorHandler
}
// PathAPIOption for how the controller is set up.
type PathAPIOption func(*PathAPIController)
// WithPathAPIErrorHandler inject ErrorHandler into controller
func WithPathAPIErrorHandler(h ErrorHandler) PathAPIOption {
return func(c *PathAPIController) {
c.errorHandler = h
}
}
// NewPathAPIController creates a default api controller
func NewPathAPIController(s PathAPIServicer, opts ...PathAPIOption) Router {
controller := &PathAPIController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the PathAPIController
func (c *PathAPIController) Routes() Routes {
return Routes{
"Path": Route{
strings.ToUpper("Get"),
"/path/endpoint/{pathParam}",
c.Path,
},
}
}
// Path - summary
func (c *PathAPIController) Path(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
pathParamParam := params["pathParam"]
if pathParamParam == "" {
c.errorHandler(w, r, &RequiredError{"pathParam"}, nil)
return
}
result, err := c.service.Path(r.Context(), pathParamParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}