From 2e348b27f78e33dc092c2c8226eaed1ac1af4a42 Mon Sep 17 00:00:00 2001 From: Ben Wells Date: Thu, 14 Dec 2017 16:04:48 +0000 Subject: [PATCH] [Go-server] Fix formatting of go-server generated code (#7162) * Add missing go-server petstore batch script * Make go-server confirm to gofmt --- bin/windows/go-petstore-server.bat | 2 +- bin/windows/go-petstore.bat | 2 +- .../resources/go-server/controller.mustache | 13 +++---- .../main/resources/go-server/logger.mustache | 2 +- .../main/resources/go-server/main.mustache | 7 ++-- .../main/resources/go-server/routers.mustache | 10 +++--- .../go-api-server/.swagger-codegen/VERSION | 2 +- .../petstore/go-api-server/go/README.md | 3 +- .../petstore/go-api-server/go/logger.go | 2 +- .../server/petstore/go-api-server/go/pet.go | 34 +++++++++---------- .../petstore/go-api-server/go/routers.go | 4 +-- .../server/petstore/go-api-server/go/store.go | 18 +++++----- .../server/petstore/go-api-server/go/user.go | 34 +++++++++---------- samples/server/petstore/go-api-server/main.go | 7 ++-- 14 files changed, 66 insertions(+), 74 deletions(-) diff --git a/bin/windows/go-petstore-server.bat b/bin/windows/go-petstore-server.bat index 03c077e34a5..459aeac0fb1 100644 --- a/bin/windows/go-petstore-server.bat +++ b/bin/windows/go-petstore-server.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties -set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l go-server -o samples\server\petstore\go-api-server +set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l go-server -o samples\server\petstore\go-api-server -DpackageName=petstoreserver -Dservice java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/go-petstore.bat b/bin/windows/go-petstore.bat index e6007cca3a1..11b2ec22d92 100755 --- a/bin/windows/go-petstore.bat +++ b/bin/windows/go-petstore.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties -set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l go -o samples\client\petstore\go +set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l go -o samples\client\petstore\go -DpackageName=petstore java %JAVA_OPTS% -jar %executable% %ags% diff --git a/modules/swagger-codegen/src/main/resources/go-server/controller.mustache b/modules/swagger-codegen/src/main/resources/go-server/controller.mustache index b8efd5f50e7..4e91e4e1f3d 100644 --- a/modules/swagger-codegen/src/main/resources/go-server/controller.mustache +++ b/modules/swagger-codegen/src/main/resources/go-server/controller.mustache @@ -6,14 +6,9 @@ import ( ) type {{classname}} struct { +}{{#operation}} -} - -{{#operation}} func {{nickname}}(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} - -{{/operation}} -{{/operations}} \ No newline at end of file + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) +}{{/operation}}{{/operations}} diff --git a/modules/swagger-codegen/src/main/resources/go-server/logger.mustache b/modules/swagger-codegen/src/main/resources/go-server/logger.mustache index 6ccb2dafe81..8c636b477ba 100644 --- a/modules/swagger-codegen/src/main/resources/go-server/logger.mustache +++ b/modules/swagger-codegen/src/main/resources/go-server/logger.mustache @@ -20,4 +20,4 @@ func Logger(inner http.Handler, name string) http.Handler { time.Since(start), ) }) -} \ No newline at end of file +} diff --git a/modules/swagger-codegen/src/main/resources/go-server/main.mustache b/modules/swagger-codegen/src/main/resources/go-server/main.mustache index 17b6072ebfb..749125b7e3e 100644 --- a/modules/swagger-codegen/src/main/resources/go-server/main.mustache +++ b/modules/swagger-codegen/src/main/resources/go-server/main.mustache @@ -1,6 +1,9 @@ package main import ( + "log" + "net/http" + // WARNING! // Change this to a fully-qualified import path // once you place this file into your project. @@ -9,14 +12,12 @@ import ( // sw "github.com/myname/myrepo/{{apiPath}}" // sw "./{{apiPath}}" - "log" - "net/http" ) func main() { log.Printf("Server started") router := sw.NewRouter() - + log.Fatal(http.ListenAndServe(":{{serverPort}}", router)) } diff --git a/modules/swagger-codegen/src/main/resources/go-server/routers.mustache b/modules/swagger-codegen/src/main/resources/go-server/routers.mustache index 638ecba8209..d63544f1d8e 100644 --- a/modules/swagger-codegen/src/main/resources/go-server/routers.mustache +++ b/modules/swagger-codegen/src/main/resources/go-server/routers.mustache @@ -1,8 +1,9 @@ package {{packageName}} import ( - "net/http" "fmt" + "net/http" + "github.com/gorilla/mux" ) @@ -42,13 +43,12 @@ var routes = Routes{ "GET", "{{{basePathWithoutHost}}}/", Index, - }, -{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}} + },{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}} + Route{ "{{operationId}}", "{{httpMethod}}", "{{{basePathWithoutHost}}}{{{path}}}", {{operationId}}, - }, -{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} + },{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} } diff --git a/samples/server/petstore/go-api-server/.swagger-codegen/VERSION b/samples/server/petstore/go-api-server/.swagger-codegen/VERSION index 7fea99011a6..f9f7450d135 100644 --- a/samples/server/petstore/go-api-server/.swagger-codegen/VERSION +++ b/samples/server/petstore/go-api-server/.swagger-codegen/VERSION @@ -1 +1 @@ -2.2.3-SNAPSHOT \ No newline at end of file +2.3.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/go-api-server/go/README.md b/samples/server/petstore/go-api-server/go/README.md index 9ad254ac015..507fdaef454 100644 --- a/samples/server/petstore/go-api-server/go/README.md +++ b/samples/server/petstore/go-api-server/go/README.md @@ -13,7 +13,8 @@ To see how to make this your own, look here: [README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md) - API version: 1.0.0 -- Build date: 2017-03-12T22:24:15.399-05:00 +- Build date: 2017-12-12T20:26:23.341Z + ### Running the server To run the server, follow these simple steps: diff --git a/samples/server/petstore/go-api-server/go/logger.go b/samples/server/petstore/go-api-server/go/logger.go index 1ff51c3f16d..d61efef2423 100644 --- a/samples/server/petstore/go-api-server/go/logger.go +++ b/samples/server/petstore/go-api-server/go/logger.go @@ -20,4 +20,4 @@ func Logger(inner http.Handler, name string) http.Handler { time.Since(start), ) }) -} \ No newline at end of file +} diff --git a/samples/server/petstore/go-api-server/go/pet.go b/samples/server/petstore/go-api-server/go/pet.go index 21252983217..a5c9a9375fd 100644 --- a/samples/server/petstore/go-api-server/go/pet.go +++ b/samples/server/petstore/go-api-server/go/pet.go @@ -5,46 +5,44 @@ import ( ) type Pet struct { - } func AddPet(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func DeletePet(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func FindPetsByStatus(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func FindPetsByTags(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func GetPetById(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func UpdatePet(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func UpdatePetWithForm(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func UploadFile(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } - diff --git a/samples/server/petstore/go-api-server/go/routers.go b/samples/server/petstore/go-api-server/go/routers.go index 22e3235fe69..f0e3a29e527 100644 --- a/samples/server/petstore/go-api-server/go/routers.go +++ b/samples/server/petstore/go-api-server/go/routers.go @@ -1,8 +1,9 @@ package petstoreserver import ( - "net/http" "fmt" + "net/http" + "github.com/gorilla/mux" ) @@ -183,5 +184,4 @@ var routes = Routes{ "/v2/user/{username}", UpdateUser, }, - } diff --git a/samples/server/petstore/go-api-server/go/store.go b/samples/server/petstore/go-api-server/go/store.go index 9af921d3a54..e48c9b5a2e2 100644 --- a/samples/server/petstore/go-api-server/go/store.go +++ b/samples/server/petstore/go-api-server/go/store.go @@ -5,26 +5,24 @@ import ( ) type Store struct { - } func DeleteOrder(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func GetInventory(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func GetOrderById(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func PlaceOrder(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } - diff --git a/samples/server/petstore/go-api-server/go/user.go b/samples/server/petstore/go-api-server/go/user.go index 5c7d0cd2f1b..097a25e4dd8 100644 --- a/samples/server/petstore/go-api-server/go/user.go +++ b/samples/server/petstore/go-api-server/go/user.go @@ -5,46 +5,44 @@ import ( ) type User struct { - } func CreateUser(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func DeleteUser(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func GetUserByName(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func LoginUser(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func LogoutUser(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } func UpdateUser(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) } - diff --git a/samples/server/petstore/go-api-server/main.go b/samples/server/petstore/go-api-server/main.go index 915963a6095..ed6c13a1eff 100644 --- a/samples/server/petstore/go-api-server/main.go +++ b/samples/server/petstore/go-api-server/main.go @@ -1,6 +1,9 @@ package main import ( + "log" + "net/http" + // WARNING! // Change this to a fully-qualified import path // once you place this file into your project. @@ -9,14 +12,12 @@ import ( // sw "github.com/myname/myrepo/go" // sw "./go" - "log" - "net/http" ) func main() { log.Printf("Server started") router := sw.NewRouter() - + log.Fatal(http.ListenAndServe(":8080", router)) }