From 3017a930db0f308dc8642a1282340513b4690861 Mon Sep 17 00:00:00 2001 From: Dimas Restu H Date: Mon, 9 May 2022 16:35:44 +0700 Subject: [PATCH] wip integrated swagger docs --- .goreleaser.yml | 1 + cmd/main/main.go | 6 + docs/docs.go | 200 ++++++++++++++++++++++++++++++++++ docs/swagger.json | 179 ++++++++++++++++++++++++++++++ docs/swagger.yaml | 114 +++++++++++++++++++ internal/auth/auth.go | 8 ++ internal/index/index.go | 12 +- internal/whatsapp/whatsapp.go | 31 ++++++ 8 files changed, 545 insertions(+), 6 deletions(-) create mode 100644 docs/docs.go create mode 100644 docs/swagger.json create mode 100644 docs/swagger.yaml diff --git a/.goreleaser.yml b/.goreleaser.yml index 0e43807..789a154 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,6 +1,7 @@ before: hooks: - make vendor + - make gen-docs builds: - main: ./cmd/main/main.go env: diff --git a/cmd/main/main.go b/cmd/main/main.go index 9ef3e9b..e4d9621 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -11,6 +11,12 @@ package main // @schemes http // @host 127.0.0.1:3000 +// @securityDefinitions.basic BasicAuth + +// @securityDefinitions.apikey BearerAuth +// @in header +// @name Authorization + import ( "context" "net/http" diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..bd12b37 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,200 @@ +// Package docs GENERATED BY SWAG; DO NOT EDIT +// This file was generated by swaggo/swag +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "contact": { + "name": "Dimas Restu Hidayanto", + "url": "https://github.com/dimaskiddo", + "email": "drh.dimasrestu@gmail.com" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/api/v1/whatsapp": { + "get": { + "description": "Get The Server Status", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Root" + ], + "summary": "Show The Status of The Server", + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/auth": { + "get": { + "security": [ + { + "BasicAuth": [] + } + ], + "description": "Get Authentication Token", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Root" + ], + "summary": "Generate Authentication Token", + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/login": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Get QR Code for WhatsApp Multi-Device Login", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json", + "text/html" + ], + "tags": [ + "WhatsApp" + ], + "summary": "Generate QR Code for WhatsApp Multi-Device Login", + "parameters": [ + { + "enum": [ + "html", + "json" + ], + "type": "string", + "default": "html", + "description": "Change Output Format in HTML or JSON", + "name": "output", + "in": "query" + } + ], + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/logout": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Make Device Logout from WhatsApp Multi-Device", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "WhatsApp" + ], + "summary": "Logout Device from WhatsApp Multi-Device", + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/send/text": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Make Device Logout from WhatsApp Multi-Device", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "WhatsApp" + ], + "summary": "Logout Device from WhatsApp Multi-Device", + "parameters": [ + { + "type": "string", + "description": "Destination Phone Number", + "name": "msisdn", + "in": "query", + "required": true + }, + { + "type": "string", + "description": "Text Message Content", + "name": "message", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "" + } + } + } + } + }, + "securityDefinitions": { + "BasicAuth": { + "type": "basic" + }, + "BearerAuth": { + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.x", + Host: "127.0.0.1:3000", + BasePath: "", + Schemes: []string{"http"}, + Title: "Go WhatsApp Multi-Device REST API", + Description: "This is WhatsApp Multi-Device Implementation in Go REST API", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..bce5654 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,179 @@ +{ + "schemes": [ + "http" + ], + "swagger": "2.0", + "info": { + "description": "This is WhatsApp Multi-Device Implementation in Go REST API", + "title": "Go WhatsApp Multi-Device REST API", + "contact": { + "name": "Dimas Restu Hidayanto", + "url": "https://github.com/dimaskiddo", + "email": "drh.dimasrestu@gmail.com" + }, + "version": "1.x" + }, + "host": "127.0.0.1:3000", + "paths": { + "/api/v1/whatsapp": { + "get": { + "description": "Get The Server Status", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Root" + ], + "summary": "Show The Status of The Server", + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/auth": { + "get": { + "security": [ + { + "BasicAuth": [] + } + ], + "description": "Get Authentication Token", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Root" + ], + "summary": "Generate Authentication Token", + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/login": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Get QR Code for WhatsApp Multi-Device Login", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json", + "text/html" + ], + "tags": [ + "WhatsApp" + ], + "summary": "Generate QR Code for WhatsApp Multi-Device Login", + "parameters": [ + { + "enum": [ + "html", + "json" + ], + "type": "string", + "default": "html", + "description": "Change Output Format in HTML or JSON", + "name": "output", + "in": "query" + } + ], + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/logout": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Make Device Logout from WhatsApp Multi-Device", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "WhatsApp" + ], + "summary": "Logout Device from WhatsApp Multi-Device", + "responses": { + "200": { + "description": "" + } + } + } + }, + "/api/v1/whatsapp/send/text": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Make Device Logout from WhatsApp Multi-Device", + "consumes": [ + "*/*" + ], + "produces": [ + "application/json" + ], + "tags": [ + "WhatsApp" + ], + "summary": "Logout Device from WhatsApp Multi-Device", + "parameters": [ + { + "type": "string", + "description": "Destination Phone Number", + "name": "msisdn", + "in": "query", + "required": true + }, + { + "type": "string", + "description": "Text Message Content", + "name": "message", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "" + } + } + } + } + }, + "securityDefinitions": { + "BasicAuth": { + "type": "basic" + }, + "BearerAuth": { + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..ee83633 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,114 @@ +host: 127.0.0.1:3000 +info: + contact: + email: drh.dimasrestu@gmail.com + name: Dimas Restu Hidayanto + url: https://github.com/dimaskiddo + description: This is WhatsApp Multi-Device Implementation in Go REST API + title: Go WhatsApp Multi-Device REST API + version: 1.x +paths: + /api/v1/whatsapp: + get: + consumes: + - '*/*' + description: Get The Server Status + produces: + - application/json + responses: + "200": + description: "" + summary: Show The Status of The Server + tags: + - Root + /api/v1/whatsapp/auth: + get: + consumes: + - '*/*' + description: Get Authentication Token + produces: + - application/json + responses: + "200": + description: "" + security: + - BasicAuth: [] + summary: Generate Authentication Token + tags: + - Root + /api/v1/whatsapp/login: + post: + consumes: + - '*/*' + description: Get QR Code for WhatsApp Multi-Device Login + parameters: + - default: html + description: Change Output Format in HTML or JSON + enum: + - html + - json + in: query + name: output + type: string + produces: + - application/json + - text/html + responses: + "200": + description: "" + security: + - BearerAuth: [] + summary: Generate QR Code for WhatsApp Multi-Device Login + tags: + - WhatsApp + /api/v1/whatsapp/logout: + post: + consumes: + - '*/*' + description: Make Device Logout from WhatsApp Multi-Device + produces: + - application/json + responses: + "200": + description: "" + security: + - BearerAuth: [] + summary: Logout Device from WhatsApp Multi-Device + tags: + - WhatsApp + /api/v1/whatsapp/send/text: + post: + consumes: + - '*/*' + description: Make Device Logout from WhatsApp Multi-Device + parameters: + - description: Destination Phone Number + in: query + name: msisdn + required: true + type: string + - description: Text Message Content + in: query + name: message + required: true + type: string + produces: + - application/json + responses: + "200": + description: "" + security: + - BearerAuth: [] + summary: Logout Device from WhatsApp Multi-Device + tags: + - WhatsApp +schemes: +- http +securityDefinitions: + BasicAuth: + type: basic + BearerAuth: + in: header + name: Authorization + type: apiKey +swagger: "2.0" diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 688e40c..1db829b 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -14,6 +14,14 @@ import ( ) // Auth +// @Summary Generate Authentication Token +// @Description Get Authentication Token +// @Tags Root +// @Accept */* +// @Produce json +// @Success 200 +// @Security BasicAuth +// @Router /api/v1/whatsapp/auth [get] func Auth(c echo.Context) error { var reqAuthBasicInfo typAuth.RequestAuthBasicInfo var resAuthJWTData typAuth.ResponseAuthJWTData diff --git a/internal/index/index.go b/internal/index/index.go index 62d2a5d..36e2523 100644 --- a/internal/index/index.go +++ b/internal/index/index.go @@ -7,13 +7,13 @@ import ( ) // Index -// @Summary Show The Status of The Server +// @Summary Show The Status of The Server // @Description Get The Server Status -// @Tags Root -// @Accept */* -// @Produce json -// @Success 200 -// @Router /api/v1/whatsapp [get] +// @Tags Root +// @Accept */* +// @Produce json +// @Success 200 +// @Router /api/v1/whatsapp [get] func Index(c echo.Context) error { return router.ResponseSuccess(c, "Go WhatsApp Multi-Device REST is running") } diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index 75ee25c..8ee6cdb 100644 --- a/internal/whatsapp/whatsapp.go +++ b/internal/whatsapp/whatsapp.go @@ -40,6 +40,17 @@ func convertFileToBytes(file multipart.File) ([]byte, error) { return buffer.Bytes(), nil } +// Login +// @Summary Generate QR Code for WhatsApp Multi-Device Login +// @Description Get QR Code for WhatsApp Multi-Device Login +// @Tags WhatsApp +// @Accept */* +// @Produce json +// @Produce html +// @Param output query string false "Change Output Format in HTML or JSON" Enums(html, json) default(html) +// @Success 200 +// @Security BearerAuth +// @Router /api/v1/whatsapp/login [post] func Login(c echo.Context) error { var err error jid := jwtPayload(c).JID @@ -93,6 +104,15 @@ func Login(c echo.Context) error { return router.ResponseSuccessWithData(c, "Successfully Generated QR Code", resLogin) } +// Logout +// @Summary Logout Device from WhatsApp Multi-Device +// @Description Make Device Logout from WhatsApp Multi-Device +// @Tags WhatsApp +// @Accept */* +// @Produce json +// @Success 200 +// @Security BearerAuth +// @Router /api/v1/whatsapp/logout [post] func Logout(c echo.Context) error { var err error jid := jwtPayload(c).JID @@ -105,6 +125,17 @@ func Logout(c echo.Context) error { return router.ResponseSuccess(c, "Successfully Logged Out") } +// SendText +// @Summary Logout Device from WhatsApp Multi-Device +// @Description Make Device Logout from WhatsApp Multi-Device +// @Tags WhatsApp +// @Accept */* +// @Produce json +// @Param msisdn query string true "Destination Phone Number" +// @Param message query string true "Text Message Content" +// @Success 200 +// @Security BearerAuth +// @Router /api/v1/whatsapp/send/text [post] func SendText(c echo.Context) error { var err error jid := jwtPayload(c).JID