[go-server] Enhance Go API server with interfaces router binding and services (#4038)

* Enhance go api server with interfaces router binding and services

Enhance the default go api server generation to define interfaces for an API's routes and services. Handle an endpoint's http binding in the generated router and the skeleton for the service logic in an API service.

* Include interface documentation in Go Server generation.
This commit is contained in:
Jesse Michael
2019-10-19 08:32:52 -07:00
committed by William Cheng
parent dd64241f8f
commit 0abb910dbc
15 changed files with 1083 additions and 257 deletions

View File

@@ -10,13 +10,16 @@
package petstoreserver
import (
"fmt"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"os"
"strconv"
"github.com/gorilla/mux"
)
// A Route defines the parameters for an api endpoint
type Route struct {
Name string
Method string
@@ -24,174 +27,71 @@ type Route struct {
HandlerFunc http.HandlerFunc
}
// Routes are a collection of defined api endpoints
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
// Router defines the required methods for retrieving api routes
type Router interface {
Routes() Routes
}
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
// NewRouter creates a new router for any number of api routers
func NewRouter(routers ...Router) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, api := range routers {
for _, route := range api.Routes() {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
}
return router
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
func EncodeJSONResponse(i interface{}, status *int, w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if status != nil {
w.WriteHeader(*status)
} else {
w.WriteHeader(http.StatusOK)
}
return json.NewEncoder(w).Encode(i)
}
var routes = Routes{
{
"Index",
"GET",
"/v2/",
Index,
},
// ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file
func ReadFormFileToTempFile(r *http.Request, key string) (*os.File, error) {
r.ParseForm()
formFile, _, err := r.FormFile(key)
if err != nil {
return nil, err
}
{
"AddPet",
strings.ToUpper("Post"),
"/v2/pet",
AddPet,
},
defer formFile.Close()
file, err := ioutil.TempFile("tmp", key)
if err != nil {
return nil, err
}
{
"DeletePet",
strings.ToUpper("Delete"),
"/v2/pet/{petId}",
DeletePet,
},
defer file.Close()
fileBytes, err := ioutil.ReadAll(formFile)
if err != nil {
return nil, err
}
{
"FindPetsByStatus",
strings.ToUpper("Get"),
"/v2/pet/findByStatus",
FindPetsByStatus,
},
{
"FindPetsByTags",
strings.ToUpper("Get"),
"/v2/pet/findByTags",
FindPetsByTags,
},
{
"GetPetById",
strings.ToUpper("Get"),
"/v2/pet/{petId}",
GetPetById,
},
{
"UpdatePet",
strings.ToUpper("Put"),
"/v2/pet",
UpdatePet,
},
{
"UpdatePetWithForm",
strings.ToUpper("Post"),
"/v2/pet/{petId}",
UpdatePetWithForm,
},
{
"UploadFile",
strings.ToUpper("Post"),
"/v2/pet/{petId}/uploadImage",
UploadFile,
},
{
"DeleteOrder",
strings.ToUpper("Delete"),
"/v2/store/order/{orderId}",
DeleteOrder,
},
{
"GetInventory",
strings.ToUpper("Get"),
"/v2/store/inventory",
GetInventory,
},
{
"GetOrderById",
strings.ToUpper("Get"),
"/v2/store/order/{orderId}",
GetOrderById,
},
{
"PlaceOrder",
strings.ToUpper("Post"),
"/v2/store/order",
PlaceOrder,
},
{
"CreateUser",
strings.ToUpper("Post"),
"/v2/user",
CreateUser,
},
{
"CreateUsersWithArrayInput",
strings.ToUpper("Post"),
"/v2/user/createWithArray",
CreateUsersWithArrayInput,
},
{
"CreateUsersWithListInput",
strings.ToUpper("Post"),
"/v2/user/createWithList",
CreateUsersWithListInput,
},
{
"DeleteUser",
strings.ToUpper("Delete"),
"/v2/user/{username}",
DeleteUser,
},
{
"GetUserByName",
strings.ToUpper("Get"),
"/v2/user/{username}",
GetUserByName,
},
{
"LoginUser",
strings.ToUpper("Get"),
"/v2/user/login",
LoginUser,
},
{
"LogoutUser",
strings.ToUpper("Get"),
"/v2/user/logout",
LogoutUser,
},
{
"UpdateUser",
strings.ToUpper("Put"),
"/v2/user/{username}",
UpdateUser,
},
file.Write(fileBytes)
return file, nil
}
// parseIntParameter parses a sting parameter to an int64
func parseIntParameter(param string) (int64, error) {
return strconv.ParseInt(param, 10, 64)
}