From a58aa7306c2a00a994d4e2862bc3607dfecc06af Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 May 2021 21:55:48 +0200 Subject: [PATCH] add swagger --- main.go | 20 +++++++++++-- routers/api/v1/user/user.go | 33 +++++++++++++++++++++ swagger.yaml | 57 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 swagger.yaml diff --git a/main.go b/main.go index 57393d7..9cc8ac5 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,11 @@ -// Golang Microservice API +// App-Idea Rest-API Documentation // // Example Swagger spec. // // Schemes: [http, https] -// BasePath: / +// BasePath: /api/v1/ // Version: 0.0.1 -// Contact: Matthias Sommer +// Contact: Alex // // Consumes: // - application/json @@ -28,11 +28,17 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" ) func main() { app := fiber.New() + app.Use(cors.New(cors.Config{ + AllowOrigins: "*", + AllowHeaders: "Origin, Content-Type, Accept", + })) + router.SetupRoutes(app) config.LoadConfig() @@ -40,3 +46,11 @@ func main() { app.Listen(config.GetConfig().Server.Host) } + +/* + +swagger generate spec -o ./swagger.yaml + +sudo docker run -p 80:8080 -e SWAGGER_JSON=/api.yaml -v ~/Documents/coding/go/rest-api/swagger.yaml:/api.yaml swaggerapi/swagger-ui + +*/ diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index f228faf..4b2466c 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -13,7 +13,40 @@ import ( "golang.org/x/crypto/bcrypt" ) +// NewUser func NewUser(c *fiber.Ctx) error { + // swagger:operation POST /users user usersNewUser + // --- + // summary: Create new user + // produces: + // - application/json + // parameters: + // - name: username + // in: query + // description: username of the user (length 3-30) + // type: string + // required: true + // - name: email + // in: query + // description: email of the user (length max 200) + // type: string + // required: true + // - name: password + // in: query + // description: password of the user (length 6-256) + // type: string + // required: true + // responses: + // '200': + // description: user created + // schema: + // type: object + // example: + // user_hastag: XVLBZG + // default: + // description: unexpected error + // schema: + // "$ref": "#/definitions/errorModel" type LoginInput struct { Username string `json:"username"` Email string `json:"email"` diff --git a/swagger.yaml b/swagger.yaml new file mode 100644 index 0000000..a289c56 --- /dev/null +++ b/swagger.yaml @@ -0,0 +1,57 @@ +basePath: /api/v1/ +consumes: +- application/json +info: + contact: + email: alex@roese.dev + name: Alex + description: |- + Example Swagger spec. + + Schemes: [http, https] + title: App-Idea Rest-API Documentation + version: 0.0.1 +paths: + /users: + post: + operationId: usersNewUser + parameters: + - description: username of the user (length 3-30) + in: query + name: username + required: true + type: string + - description: email of the user (length max 200) + in: query + name: email + required: true + type: string + - description: password of the user (length 6-256) + in: query + name: password + required: true + type: string + produces: + - application/json + responses: + "200": + description: user created + schema: + example: + user_hastag: XVLBZG + type: object + default: + description: unexpected error + schema: + $ref: '#/definitions/errorModel' + summary: Create new user + tags: + - user +produces: +- application/json +securityDefinitions: + api_key: + in: header + name: Authorization + type: apiKey +swagger: "2.0"