From afa239379f7dc057dff4f1d69f82b9e70700c644 Mon Sep 17 00:00:00 2001 From: Ricardo Lopes Date: Wed, 23 Aug 2023 17:43:12 +0100 Subject: [PATCH] feat: adds documentation (#20) - Creates Swagger docs (closes #19) - Adds example for patch /short/:id - Minor fixes on documentation - Enables github pages with docs --- Makefile | 5 + README.md | 43 +++- api/preview.go | 19 +- api/shortener.go | 60 +++++- docs/favicon.ico | Bin 0 -> 1150 bytes docs/index.html | 30 +++ docs/swagger.json | 487 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 321 ++++++++++++++++++++++++++++++ main.go | 6 + 9 files changed, 964 insertions(+), 7 deletions(-) create mode 100644 docs/favicon.ico create mode 100644 docs/index.html create mode 100644 docs/swagger.json create mode 100644 docs/swagger.yaml diff --git a/Makefile b/Makefile index be8d67b..2ad3ad0 100644 --- a/Makefile +++ b/Makefile @@ -76,3 +76,8 @@ migration/force: .PHONY: image-push image-push: @build-tools/tag.sh + +.PHONY: docs +docs: + @swag init --parseDependency --parseInternal --parseDepth 1 + @rm docs/docs.go diff --git a/README.md b/README.md index 7d20987..19ea7dd 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ you can use either `cockroach` or `postgres` as value for the `DB_ADAPTOR` If you want to use `cockroach`, you can create a free account [here](https://cockroachlabs.cloud/) +Bellow are a few examples of how to use the API. You can find the documentation [here](https://doutorfinancas.github.io/pun-sho/) + ### Create a short link ```bash read -r -d '' BODY <vYhz#zuJz@P!dKp~(AL>x#lFaYI-0=T%&`#-7fG|>@u0AY|` z7>0>qqhaR5)RIF_t2L{{rSCrw;F2Ro9Hbuvh*1kt^WbDVSpB8-esFy-{a^tgLCCCU zm*>Kig4Mz~NDM+|pv!}_!o)!AkI#<5`7m)(XpkA#cO+3{1}XLtQVU8)gw)`Yho!-R Zr2hkR8VLUe;*X$Q$4J;U4L~(8eE{q4?Xv&? literal 0 HcmV?d00001 diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..d24a219 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,30 @@ + + + + + + Pun-Sho API + + + +
+ + + diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..8abf1d4 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,487 @@ +{ + "swagger": "2.0", + "info": { + "description": "Create your shortlinks with QRCodes and more!", + "title": "Pun Sho API", + "contact": {}, + "version": "0.2" + }, + "basePath": "/api/v1", + "paths": { + "/preview": { + "post": { + "description": "Creates a QR Code preview for a given url", + "produces": [ + "application/json" + ], + "tags": [ + "Preview" + ], + "summary": "Creates a QR Code preview for a given url", + "parameters": [ + { + "type": "string", + "description": "Authorization token", + "name": "token", + "in": "header" + }, + { + "description": "Request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.GeneratePreview" + } + } + ], + "responses": { + "201": { + "description": "response", + "schema": { + "$ref": "#/definitions/response.GeneratePreviewResponse" + } + }, + "400": { + "description": "error", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + } + } + } + }, + "/short": { + "get": { + "description": "Lists all the shortlinks available", + "produces": [ + "application/json" + ], + "tags": [ + "Short" + ], + "summary": "Lists your shorlinks", + "parameters": [ + { + "type": "string", + "description": "Authorization token", + "name": "token", + "in": "header" + } + ], + "responses": { + "200": { + "description": "response", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.Shorty" + } + } + }, + "400": { + "description": "error", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + } + } + }, + "post": { + "description": "Creates a shortlink for a given url, optionally setting a ttl and a redirection limit", + "produces": [ + "application/json" + ], + "tags": [ + "Short" + ], + "summary": "Creates a shortlink for a given url", + "parameters": [ + { + "type": "string", + "description": "Authorization token", + "name": "token", + "in": "header" + }, + { + "description": "Request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.CreateShorty" + } + } + ], + "responses": { + "201": { + "description": "response", + "schema": { + "$ref": "#/definitions/entity.Shorty" + } + }, + "400": { + "description": "error", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + } + } + } + }, + "/short/{id}": { + "get": { + "description": "retrieves full information for the give shortlink", + "tags": [ + "Short" + ], + "summary": "get your shortlink information", + "parameters": [ + { + "type": "string", + "description": "Authorization token", + "name": "token", + "in": "header" + }, + { + "type": "string", + "description": "ShortLink ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "response", + "schema": { + "$ref": "#/definitions/entity.Shorty" + } + }, + "400": { + "description": "error", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + }, + "404": { + "description": "not found", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + } + } + }, + "delete": { + "description": "Deletes a shortlink", + "produces": [ + "application/json" + ], + "tags": [ + "Short" + ], + "summary": "Deletes a shortlink", + "parameters": [ + { + "type": "string", + "description": "Authorization token", + "name": "token", + "in": "header" + }, + { + "type": "string", + "description": "ShortLink ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.UpdateShorty" + } + } + ], + "responses": { + "204": { + "description": "No Content", + "schema": { + "type": "string" + } + }, + "400": { + "description": "error", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + }, + "404": { + "description": "not found", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + } + } + }, + "patch": { + "description": "Edits a shortlink, allowing to set TTL, cancel the link or change the redirection limit or associated link", + "produces": [ + "application/json" + ], + "tags": [ + "Short" + ], + "summary": "Edits a shortlink", + "parameters": [ + { + "type": "string", + "description": "Authorization token", + "name": "token", + "in": "header" + }, + { + "type": "string", + "description": "ShortLink ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.UpdateShorty" + } + } + ], + "responses": { + "200": { + "description": "response", + "schema": { + "$ref": "#/definitions/entity.Shorty" + } + }, + "400": { + "description": "error", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + }, + "404": { + "description": "not found", + "schema": { + "$ref": "#/definitions/response.FailureResponse" + } + } + } + } + } + }, + "definitions": { + "entity.Meta": { + "type": "object", + "properties": { + "meta_collection": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.MetaValues" + } + } + } + }, + "entity.MetaValues": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "values": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "entity.Shorty": { + "type": "object", + "properties": { + "TTL": { + "type": "string" + }, + "accesses": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.ShortyAccess" + } + }, + "created_at": { + "type": "string" + }, + "deleted_at": { + "type": "string" + }, + "id": { + "type": "string" + }, + "link": { + "type": "string" + }, + "qr_code": { + "type": "string" + }, + "redirection_limit": { + "type": "integer" + }, + "redirects": { + "type": "integer" + }, + "short_link": { + "type": "string" + }, + "visits": { + "type": "integer" + } + } + }, + "entity.ShortyAccess": { + "type": "object", + "properties": { + "browser": { + "type": "string" + }, + "created_at": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "id": { + "type": "string" + }, + "ip": { + "type": "string" + }, + "meta": { + "$ref": "#/definitions/entity.Meta" + }, + "os": { + "type": "string" + }, + "status": { + "type": "string" + }, + "user_agent": { + "type": "string" + } + } + }, + "request.CreateShorty": { + "type": "object", + "properties": { + "TTL": { + "type": "string" + }, + "link": { + "type": "string" + }, + "qr_code": { + "$ref": "#/definitions/request.QRCode" + }, + "redirection_limit": { + "type": "integer" + } + } + }, + "request.GeneratePreview": { + "type": "object", + "properties": { + "link": { + "type": "string" + }, + "qr_code": { + "$ref": "#/definitions/request.QRCode" + } + } + }, + "request.QRCode": { + "type": "object", + "properties": { + "background_color": { + "type": "string" + }, + "border_width": { + "type": "integer" + }, + "create": { + "type": "boolean" + }, + "foreground_color": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "shape": { + "type": "string" + }, + "width": { + "type": "integer" + } + } + }, + "request.UpdateShorty": { + "type": "object", + "properties": { + "TTL": { + "type": "string" + }, + "cancel": { + "type": "boolean" + }, + "link": { + "type": "string" + }, + "redirection_limit": { + "type": "integer" + } + } + }, + "response.FailureResponse": { + "type": "object", + "properties": { + "message": { + "type": "array", + "items": { + "type": "string" + } + }, + "status": { + "type": "string" + } + } + }, + "response.GeneratePreviewResponse": { + "type": "object", + "properties": { + "message": { + "type": "array", + "items": { + "type": "string" + } + }, + "qr_code": { + "type": "string" + }, + "status": { + "type": "string" + } + } + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..1699cfd --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,321 @@ +basePath: /api/v1 +definitions: + entity.Meta: + properties: + meta_collection: + items: + $ref: '#/definitions/entity.MetaValues' + type: array + type: object + entity.MetaValues: + properties: + name: + type: string + values: + items: + type: string + type: array + type: object + entity.Shorty: + properties: + TTL: + type: string + accesses: + items: + $ref: '#/definitions/entity.ShortyAccess' + type: array + created_at: + type: string + deleted_at: + type: string + id: + type: string + link: + type: string + qr_code: + type: string + redirection_limit: + type: integer + redirects: + type: integer + short_link: + type: string + visits: + type: integer + type: object + entity.ShortyAccess: + properties: + browser: + type: string + created_at: + type: string + extra: + type: string + id: + type: string + ip: + type: string + meta: + $ref: '#/definitions/entity.Meta' + os: + type: string + status: + type: string + user_agent: + type: string + type: object + request.CreateShorty: + properties: + TTL: + type: string + link: + type: string + qr_code: + $ref: '#/definitions/request.QRCode' + redirection_limit: + type: integer + type: object + request.GeneratePreview: + properties: + link: + type: string + qr_code: + $ref: '#/definitions/request.QRCode' + type: object + request.QRCode: + properties: + background_color: + type: string + border_width: + type: integer + create: + type: boolean + foreground_color: + type: string + logo: + type: string + shape: + type: string + width: + type: integer + type: object + request.UpdateShorty: + properties: + TTL: + type: string + cancel: + type: boolean + link: + type: string + redirection_limit: + type: integer + type: object + response.FailureResponse: + properties: + message: + items: + type: string + type: array + status: + type: string + type: object + response.GeneratePreviewResponse: + properties: + message: + items: + type: string + type: array + qr_code: + type: string + status: + type: string + type: object +info: + contact: {} + description: Create your shortlinks with QRCodes and more! + title: Pun Sho API + version: "0.2" +paths: + /preview: + post: + description: Creates a QR Code preview for a given url + parameters: + - description: Authorization token + in: header + name: token + type: string + - description: Request + in: body + name: request + required: true + schema: + $ref: '#/definitions/request.GeneratePreview' + produces: + - application/json + responses: + "201": + description: response + schema: + $ref: '#/definitions/response.GeneratePreviewResponse' + "400": + description: error + schema: + $ref: '#/definitions/response.FailureResponse' + summary: Creates a QR Code preview for a given url + tags: + - Preview + /short: + get: + description: Lists all the shortlinks available + parameters: + - description: Authorization token + in: header + name: token + type: string + produces: + - application/json + responses: + "200": + description: response + schema: + items: + $ref: '#/definitions/entity.Shorty' + type: array + "400": + description: error + schema: + $ref: '#/definitions/response.FailureResponse' + summary: Lists your shorlinks + tags: + - Short + post: + description: Creates a shortlink for a given url, optionally setting a ttl and + a redirection limit + parameters: + - description: Authorization token + in: header + name: token + type: string + - description: Request + in: body + name: request + required: true + schema: + $ref: '#/definitions/request.CreateShorty' + produces: + - application/json + responses: + "201": + description: response + schema: + $ref: '#/definitions/entity.Shorty' + "400": + description: error + schema: + $ref: '#/definitions/response.FailureResponse' + summary: Creates a shortlink for a given url + tags: + - Short + /short/{id}: + delete: + description: Deletes a shortlink + parameters: + - description: Authorization token + in: header + name: token + type: string + - description: ShortLink ID + in: path + name: id + required: true + type: string + - description: Request + in: body + name: request + required: true + schema: + $ref: '#/definitions/request.UpdateShorty' + produces: + - application/json + responses: + "204": + description: No Content + schema: + type: string + "400": + description: error + schema: + $ref: '#/definitions/response.FailureResponse' + "404": + description: not found + schema: + $ref: '#/definitions/response.FailureResponse' + summary: Deletes a shortlink + tags: + - Short + get: + description: retrieves full information for the give shortlink + parameters: + - description: Authorization token + in: header + name: token + type: string + - description: ShortLink ID + in: path + name: id + required: true + type: string + responses: + "200": + description: response + schema: + $ref: '#/definitions/entity.Shorty' + "400": + description: error + schema: + $ref: '#/definitions/response.FailureResponse' + "404": + description: not found + schema: + $ref: '#/definitions/response.FailureResponse' + summary: get your shortlink information + tags: + - Short + patch: + description: Edits a shortlink, allowing to set TTL, cancel the link or change + the redirection limit or associated link + parameters: + - description: Authorization token + in: header + name: token + type: string + - description: ShortLink ID + in: path + name: id + required: true + type: string + - description: Request + in: body + name: request + required: true + schema: + $ref: '#/definitions/request.UpdateShorty' + produces: + - application/json + responses: + "200": + description: response + schema: + $ref: '#/definitions/entity.Shorty' + "400": + description: error + schema: + $ref: '#/definitions/response.FailureResponse' + "404": + description: not found + schema: + $ref: '#/definitions/response.FailureResponse' + summary: Edits a shortlink + tags: + - Short +swagger: "2.0" diff --git a/main.go b/main.go index 448bba0..9540ff7 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,12 @@ import ( const Timestamp = "timestamp" +// @title Pun Sho API +// @version 0.2 +// @description Create your shortlinks with QRCodes and more! + +// @BasePath /api/v1 + func main() { loggerConfig := zap.NewProductionConfig() loggerConfig.EncoderConfig.TimeKey = Timestamp