Go server clean up (#328)

This commit is contained in:
Ben Wells 2018-06-17 18:09:48 +01:00 committed by William Cheng
parent 97bab928bd
commit c51986d657
15 changed files with 79 additions and 58 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
){{#operation}} ){{#operation}}
// {{nickname}} - {{{summary}}}
func {{nickname}}(w http.ResponseWriter, r *http.Request) { func {{nickname}}(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -2,23 +2,23 @@
package {{packageName}} package {{packageName}}
import ( import (
"log" "log"
"net/http" "net/http"
"time" "time"
) )
func Logger(inner http.Handler, name string) http.Handler { func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
inner.ServeHTTP(w, r) inner.ServeHTTP(w, r)
log.Printf( log.Printf(
"%s %s %s %s", "%s %s %s %s",
r.Method, r.Method,
r.RequestURI, r.RequestURI,
name, name,
time.Since(start), time.Since(start),
) )
}) })
} }

View File

@ -15,7 +15,7 @@ const (
{{/enumVars}} {{/enumVars}}
{{/allowableValues}} {{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}} ){{/isEnum}}{{^isEnum}}{{#description}}
// {{{description}}}{{/description}} // {{classname}} - {{{description}}}{{/description}}
type {{classname}} struct { type {{classname}} struct {
{{#vars}}{{#description}} {{#vars}}{{#description}}
// {{{description}}}{{/description}} // {{{description}}}{{/description}}

View File

@ -40,14 +40,14 @@ func Index(w http.ResponseWriter, r *http.Request) {
} }
var routes = Routes{ var routes = Routes{
Route{ {
"Index", "Index",
"GET", "GET",
"{{{basePathWithoutHost}}}/", "{{{basePathWithoutHost}}}/",
Index, Index,
},{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}} },{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
Route{ {
"{{operationId}}", "{{operationId}}",
strings.ToUpper("{{httpMethod}}"), strings.ToUpper("{{httpMethod}}"),
"{{{basePathWithoutHost}}}{{{path}}}", "{{{basePathWithoutHost}}}{{{path}}}",

View File

@ -13,41 +13,49 @@ import (
"net/http" "net/http"
) )
// AddPet - Add a new pet to the store
func AddPet(w http.ResponseWriter, r *http.Request) { func AddPet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// DeletePet - Deletes a pet
func DeletePet(w http.ResponseWriter, r *http.Request) { func DeletePet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// FindPetsByStatus - Finds Pets by status
func FindPetsByStatus(w http.ResponseWriter, r *http.Request) { func FindPetsByStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// FindPetsByTags - Finds Pets by tags
func FindPetsByTags(w http.ResponseWriter, r *http.Request) { func FindPetsByTags(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// GetPetById - Find pet by ID
func GetPetById(w http.ResponseWriter, r *http.Request) { func GetPetById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// UpdatePet - Update an existing pet
func UpdatePet(w http.ResponseWriter, r *http.Request) { func UpdatePet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// UpdatePetWithForm - Updates a pet in the store with form data
func UpdatePetWithForm(w http.ResponseWriter, r *http.Request) { func UpdatePetWithForm(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// UploadFile - uploads an image
func UploadFile(w http.ResponseWriter, r *http.Request) { func UploadFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -13,21 +13,25 @@ import (
"net/http" "net/http"
) )
// DeleteOrder - Delete purchase order by ID
func DeleteOrder(w http.ResponseWriter, r *http.Request) { func DeleteOrder(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// GetInventory - Returns pet inventories by status
func GetInventory(w http.ResponseWriter, r *http.Request) { func GetInventory(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// GetOrderById - Find purchase order by ID
func GetOrderById(w http.ResponseWriter, r *http.Request) { func GetOrderById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// PlaceOrder - Place an order for a pet
func PlaceOrder(w http.ResponseWriter, r *http.Request) { func PlaceOrder(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -13,41 +13,49 @@ import (
"net/http" "net/http"
) )
// CreateUser - Create user
func CreateUser(w http.ResponseWriter, r *http.Request) { func CreateUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// CreateUsersWithArrayInput - Creates list of users with given input array
func CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) { func CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// CreateUsersWithListInput - Creates list of users with given input array
func CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) { func CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// DeleteUser - Delete user
func DeleteUser(w http.ResponseWriter, r *http.Request) { func DeleteUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// GetUserByName - Get user by user name
func GetUserByName(w http.ResponseWriter, r *http.Request) { func GetUserByName(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// LoginUser - Logs user into the system
func LoginUser(w http.ResponseWriter, r *http.Request) { func LoginUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// LogoutUser - Logs out current logged in user session
func LogoutUser(w http.ResponseWriter, r *http.Request) { func LogoutUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// UpdateUser - Updated user
func UpdateUser(w http.ResponseWriter, r *http.Request) { func UpdateUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -10,23 +10,23 @@
package petstoreserver package petstoreserver
import ( import (
"log" "log"
"net/http" "net/http"
"time" "time"
) )
func Logger(inner http.Handler, name string) http.Handler { func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
inner.ServeHTTP(w, r) inner.ServeHTTP(w, r)
log.Printf( log.Printf(
"%s %s %s %s", "%s %s %s %s",
r.Method, r.Method,
r.RequestURI, r.RequestURI,
name, name,
time.Since(start), time.Since(start),
) )
}) })
} }

View File

@ -9,7 +9,7 @@
package petstoreserver package petstoreserver
// Describes the result of uploading an image resource // ApiResponse - Describes the result of uploading an image resource
type ApiResponse struct { type ApiResponse struct {
Code int32 `json:"code,omitempty"` Code int32 `json:"code,omitempty"`

View File

@ -9,7 +9,7 @@
package petstoreserver package petstoreserver
// A category for a pet // Category - A category for a pet
type Category struct { type Category struct {
Id int64 `json:"id,omitempty"` Id int64 `json:"id,omitempty"`

View File

@ -13,7 +13,7 @@ import (
"time" "time"
) )
// An order for a pets from the pet store // Order - An order for a pets from the pet store
type Order struct { type Order struct {
Id int64 `json:"id,omitempty"` Id int64 `json:"id,omitempty"`

View File

@ -9,7 +9,7 @@
package petstoreserver package petstoreserver
// A pet for sale in the pet store // Pet - A pet for sale in the pet store
type Pet struct { type Pet struct {
Id int64 `json:"id,omitempty"` Id int64 `json:"id,omitempty"`

View File

@ -9,7 +9,7 @@
package petstoreserver package petstoreserver
// A tag for a pet // Tag - A tag for a pet
type Tag struct { type Tag struct {
Id int64 `json:"id,omitempty"` Id int64 `json:"id,omitempty"`

View File

@ -9,7 +9,7 @@
package petstoreserver package petstoreserver
// A User who is purchasing from the pet store // User - A User who is purchasing from the pet store
type User struct { type User struct {
Id int64 `json:"id,omitempty"` Id int64 `json:"id,omitempty"`

View File

@ -48,147 +48,147 @@ func Index(w http.ResponseWriter, r *http.Request) {
} }
var routes = Routes{ var routes = Routes{
Route{ {
"Index", "Index",
"GET", "GET",
"/v2/", "/v2/",
Index, Index,
}, },
Route{ {
"AddPet", "AddPet",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/pet", "/v2/pet",
AddPet, AddPet,
}, },
Route{ {
"DeletePet", "DeletePet",
strings.ToUpper("Delete"), strings.ToUpper("Delete"),
"/v2/pet/{petId}", "/v2/pet/{petId}",
DeletePet, DeletePet,
}, },
Route{ {
"FindPetsByStatus", "FindPetsByStatus",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/pet/findByStatus", "/v2/pet/findByStatus",
FindPetsByStatus, FindPetsByStatus,
}, },
Route{ {
"FindPetsByTags", "FindPetsByTags",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/pet/findByTags", "/v2/pet/findByTags",
FindPetsByTags, FindPetsByTags,
}, },
Route{ {
"GetPetById", "GetPetById",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/pet/{petId}", "/v2/pet/{petId}",
GetPetById, GetPetById,
}, },
Route{ {
"UpdatePet", "UpdatePet",
strings.ToUpper("Put"), strings.ToUpper("Put"),
"/v2/pet", "/v2/pet",
UpdatePet, UpdatePet,
}, },
Route{ {
"UpdatePetWithForm", "UpdatePetWithForm",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/pet/{petId}", "/v2/pet/{petId}",
UpdatePetWithForm, UpdatePetWithForm,
}, },
Route{ {
"UploadFile", "UploadFile",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/pet/{petId}/uploadImage", "/v2/pet/{petId}/uploadImage",
UploadFile, UploadFile,
}, },
Route{ {
"DeleteOrder", "DeleteOrder",
strings.ToUpper("Delete"), strings.ToUpper("Delete"),
"/v2/store/order/{orderId}", "/v2/store/order/{orderId}",
DeleteOrder, DeleteOrder,
}, },
Route{ {
"GetInventory", "GetInventory",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/store/inventory", "/v2/store/inventory",
GetInventory, GetInventory,
}, },
Route{ {
"GetOrderById", "GetOrderById",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/store/order/{orderId}", "/v2/store/order/{orderId}",
GetOrderById, GetOrderById,
}, },
Route{ {
"PlaceOrder", "PlaceOrder",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/store/order", "/v2/store/order",
PlaceOrder, PlaceOrder,
}, },
Route{ {
"CreateUser", "CreateUser",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/user", "/v2/user",
CreateUser, CreateUser,
}, },
Route{ {
"CreateUsersWithArrayInput", "CreateUsersWithArrayInput",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/user/createWithArray", "/v2/user/createWithArray",
CreateUsersWithArrayInput, CreateUsersWithArrayInput,
}, },
Route{ {
"CreateUsersWithListInput", "CreateUsersWithListInput",
strings.ToUpper("Post"), strings.ToUpper("Post"),
"/v2/user/createWithList", "/v2/user/createWithList",
CreateUsersWithListInput, CreateUsersWithListInput,
}, },
Route{ {
"DeleteUser", "DeleteUser",
strings.ToUpper("Delete"), strings.ToUpper("Delete"),
"/v2/user/{username}", "/v2/user/{username}",
DeleteUser, DeleteUser,
}, },
Route{ {
"GetUserByName", "GetUserByName",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/user/{username}", "/v2/user/{username}",
GetUserByName, GetUserByName,
}, },
Route{ {
"LoginUser", "LoginUser",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/user/login", "/v2/user/login",
LoginUser, LoginUser,
}, },
Route{ {
"LogoutUser", "LogoutUser",
strings.ToUpper("Get"), strings.ToUpper("Get"),
"/v2/user/logout", "/v2/user/logout",
LogoutUser, LogoutUser,
}, },
Route{ {
"UpdateUser", "UpdateUser",
strings.ToUpper("Put"), strings.ToUpper("Put"),
"/v2/user/{username}", "/v2/user/{username}",