From 8a90448854ed477830f4cdafd8f4b1623fae38cb Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 14 Nov 2025 17:56:51 +0000 Subject: [PATCH] tsk-15: Add API documentation (#17) Closes #15 Reviewed-on: https://git.kundeng.us/phoenix/textsender-auth/pulls/17 Co-authored-by: phoenix Co-committed-by: phoenix --- Dockerfile | 1 + README.md | 11 ++ cmd/api/main.go | 17 +++ docs/docs.go | 230 +++++++++++++++++++++++++++++++++++ docs/swagger.json | 206 +++++++++++++++++++++++++++++++ docs/swagger.yaml | 132 ++++++++++++++++++++ go.mod | 14 ++- go.sum | 41 +++++++ internal/handler/login.go | 12 ++ internal/handler/register.go | 12 ++ 10 files changed, 675 insertions(+), 1 deletion(-) create mode 100644 docs/docs.go create mode 100644 docs/swagger.json create mode 100644 docs/swagger.yaml diff --git a/Dockerfile b/Dockerfile index d374bbb..0e4d948 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,6 +29,7 @@ COPY ./internal ./internal COPY ./Makefile . COPY ./.env . COPY ./migrations ./migrations +COPY ./docs ./docs # Build the application RUN CGO_ENABLED=0 GOOS=linux make build diff --git a/README.md b/README.md index 8fa169b..6f5cc98 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,14 @@ make build ``` ./textsender-auth -reset-db ``` + + +Generate API documentation +``` +go install github.com/swaggo/swag/cmd/swag@latest +go get -u github.com/swaggo/http-swagger/v2 + +swag init --generalInfo main.go --dir ./cmd/api,./internal/handler --output docs/ --parseDependency --parseInternal +``` + +The API documentation can be viewed from `http://localhost:9080/swagger/index.html`. diff --git a/cmd/api/main.go b/cmd/api/main.go index e8888c5..07917a9 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -12,7 +12,9 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" + "github.com/swaggo/http-swagger/v2" + _ "git.kundeng.us/phoenix/textsender-auth/docs" "git.kundeng.us/phoenix/textsender-auth/internal/config" database "git.kundeng.us/phoenix/textsender-auth/internal/db" "git.kundeng.us/phoenix/textsender-auth/internal/handler" @@ -21,6 +23,17 @@ import ( "git.kundeng.us/phoenix/textsender-auth/internal/model" ) +// @title textsender-auth +// @version 1.0 +// @description Auth API to send text messages + +// @host localhost:9080 +// @BasePath /api/v1 + +// @securityDefinitions.apikey BearerAuth +// @in header +// @name Authorization +// @description JWT Bearer Token func main() { cfg := config.Load() if cfg == nil { @@ -73,6 +86,10 @@ func main() { router.Method("Post", endpoint.Register, http.HandlerFunc(userHandler.Register)) router.Method("Post", endpoint.Login, http.HandlerFunc(loginHandler.Login)) + router.Method("GET", "/swagger/*", httpSwagger.Handler( + httpSwagger.URL(fmt.Sprintf("http://localhost:%s/swagger/doc.json", config.Port)), + )) + // Start server server := &http.Server{ Addr: ":" + cfg.ServerPort, diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..6a7e528 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,230 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "contact": {}, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/login": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Login and be given an access token (requires JWT)", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Login", + "parameters": [ + { + "description": "Data to obtain a token", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.LoginAccount" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.LoginResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/handler.LoginResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/handler.LoginResponse" + } + } + } + } + }, + "/register": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Create a user that can send texts (requires JWT)", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Register user", + "parameters": [ + { + "description": "Data to add user", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.RegisterUser" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.RegisterResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/handler.RegisterResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/handler.RegisterResponse" + } + } + } + } + } + }, + "definitions": { + "handler.LoginAccount": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "handler.LoginResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/token.Login" + } + }, + "message": { + "type": "string" + } + } + }, + "handler.RegisterResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/handler.RegisterResponseItem" + } + }, + "message": { + "type": "string" + } + } + }, + "handler.RegisterResponseItem": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "phone_number": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "handler.RegisterUser": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "phone_number": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "token.Login": { + "type": "object", + "properties": { + "access_token": { + "type": "string" + }, + "expires_in": { + "type": "integer" + }, + "token_type": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "BearerAuth": { + "description": "JWT Bearer Token", + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "localhost:9080", + BasePath: "/api/v1", + Schemes: []string{}, + Title: "textsender-auth", + Description: "Auth API to send text messages", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..d06ced4 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,206 @@ +{ + "swagger": "2.0", + "info": { + "description": "Auth API to send text messages", + "title": "textsender-auth", + "contact": {}, + "version": "1.0" + }, + "host": "localhost:9080", + "basePath": "/api/v1", + "paths": { + "/login": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Login and be given an access token (requires JWT)", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Login", + "parameters": [ + { + "description": "Data to obtain a token", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.LoginAccount" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.LoginResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/handler.LoginResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/handler.LoginResponse" + } + } + } + } + }, + "/register": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Create a user that can send texts (requires JWT)", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Register user", + "parameters": [ + { + "description": "Data to add user", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.RegisterUser" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.RegisterResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/handler.RegisterResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/handler.RegisterResponse" + } + } + } + } + } + }, + "definitions": { + "handler.LoginAccount": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "handler.LoginResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/token.Login" + } + }, + "message": { + "type": "string" + } + } + }, + "handler.RegisterResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/handler.RegisterResponseItem" + } + }, + "message": { + "type": "string" + } + } + }, + "handler.RegisterResponseItem": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "phone_number": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "handler.RegisterUser": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "phone_number": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "token.Login": { + "type": "object", + "properties": { + "access_token": { + "type": "string" + }, + "expires_in": { + "type": "integer" + }, + "token_type": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "BearerAuth": { + "description": "JWT Bearer Token", + "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..774e7f3 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,132 @@ +basePath: /api/v1 +definitions: + handler.LoginAccount: + properties: + password: + type: string + username: + type: string + type: object + handler.LoginResponse: + properties: + data: + items: + $ref: '#/definitions/token.Login' + type: array + message: + type: string + type: object + handler.RegisterResponse: + properties: + data: + items: + $ref: '#/definitions/handler.RegisterResponseItem' + type: array + message: + type: string + type: object + handler.RegisterResponseItem: + properties: + id: + type: string + phone_number: + type: string + username: + type: string + type: object + handler.RegisterUser: + properties: + password: + type: string + phone_number: + type: string + username: + type: string + type: object + token.Login: + properties: + access_token: + type: string + expires_in: + type: integer + token_type: + type: string + type: object +host: localhost:9080 +info: + contact: {} + description: Auth API to send text messages + title: textsender-auth + version: "1.0" +paths: + /login: + post: + consumes: + - application/json + description: Login and be given an access token (requires JWT) + parameters: + - description: Data to obtain a token + in: body + name: request + required: true + schema: + $ref: '#/definitions/handler.LoginAccount' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.LoginResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/handler.LoginResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/handler.LoginResponse' + security: + - BearerAuth: [] + summary: Login + tags: + - users + /register: + post: + consumes: + - application/json + description: Create a user that can send texts (requires JWT) + parameters: + - description: Data to add user + in: body + name: request + required: true + schema: + $ref: '#/definitions/handler.RegisterUser' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.RegisterResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/handler.RegisterResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/handler.RegisterResponse' + security: + - BearerAuth: [] + summary: Register user + tags: + - users +securityDefinitions: + BearerAuth: + description: JWT Bearer Token + in: header + name: Authorization + type: apiKey +swagger: "2.0" diff --git a/go.mod b/go.mod index 7555b02..3a91d7d 100644 --- a/go.mod +++ b/go.mod @@ -11,18 +11,30 @@ require ( github.com/jackc/pgx/v5 v5.7.5 github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.11.1 + github.com/swaggo/http-swagger/v2 v2.0.2 + github.com/swaggo/swag v1.16.6 golang.org/x/crypto v0.42.0 ) require ( + github.com/KyleBanks/depth v1.2.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-openapi/jsonpointer v0.19.5 // indirect + github.com/go-openapi/jsonreference v0.20.0 // indirect + github.com/go-openapi/spec v0.20.6 // indirect + github.com/go-openapi/swag v0.19.15 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect - github.com/kr/text v0.2.0 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/mailru/easyjson v0.7.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.14.1 // indirect + github.com/swaggo/files/v2 v2.0.0 // indirect + golang.org/x/mod v0.27.0 // indirect golang.org/x/sync v0.17.0 // indirect golang.org/x/text v0.29.0 // indirect + golang.org/x/tools v0.36.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1f2b200..2f1f7ba 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,27 @@ git.kundeng.us/phoenix/textsender-models v0.0.6-main-28b29802b9-556 h1:2NI8cXaxt2rsXrhy/rDhgoOzvjLQwbhwr4nv2g35Ah8= git.kundeng.us/phoenix/textsender-models v0.0.6-main-28b29802b9-556/go.mod h1:lx5MCnOgGgsdpwzrfi9uph5xmkeb6H8AuexUNGss2no= +github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= +github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE= github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= +github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= +github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonreference v0.20.0 h1:MYlu0sBgChmCfJxxUKZ8g1cPWFOB37YSZqewK7OKeyA= +github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo= +github.com/go-openapi/spec v0.20.6 h1:ich1RQ3WDbfoeTqTAb+5EIxNmpKVJZWBNah9RAT0jIQ= +github.com/go-openapi/spec v0.20.6/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA= +github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM= +github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= @@ -22,28 +36,55 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= +github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw= +github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM= +github.com/swaggo/http-swagger/v2 v2.0.2 h1:FKCdLsl+sFCx60KFsyM0rDarwiUSZ8DqbfSyIKC9OBg= +github.com/swaggo/http-swagger/v2 v2.0.2/go.mod h1:r7/GBkAWIfK6E/OLnE8fXnviHiDeAHmgIyooa4xm3AQ= +github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI= +github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg= golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI= golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8= +golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ= +golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc= golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk= golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4= +golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg= +golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/handler/login.go b/internal/handler/login.go index fff6430..c1bc6c8 100644 --- a/internal/handler/login.go +++ b/internal/handler/login.go @@ -29,6 +29,18 @@ func NewLoginHandler(userStore model.UserStore) *LoginHandler { return &LoginHandler{UserStore: userStore} } +// Login godoc +// @Summary Login +// @Description Login and be given an access token (requires JWT) +// @Tags users +// @Accept json +// @Produce json +// @Security BearerAuth +// @Param request body LoginAccount true "Data to obtain a token" +// @Success 200 {object} LoginResponse +// @Failure 400 {object} LoginResponse +// @Failure 500 {object} LoginResponse +// @Router /login [post] func (l *LoginHandler) Login(w http.ResponseWriter, r *http.Request) { var req LoginAccount if err := ExtractFromRequest(r, &req); err != nil { diff --git a/internal/handler/register.go b/internal/handler/register.go index f7e67ea..10c622c 100644 --- a/internal/handler/register.go +++ b/internal/handler/register.go @@ -36,6 +36,18 @@ func NewUserHandler(userStore model.UserStore) *UserHandler { return &UserHandler{UserStore: userStore} } +// Register godoc +// @Summary Register user +// @Description Create a user that can send texts (requires JWT) +// @Tags users +// @Accept json +// @Produce json +// @Security BearerAuth +// @Param request body RegisterUser true "Data to add user" +// @Success 200 {object} RegisterResponse +// @Failure 400 {object} RegisterResponse +// @Failure 500 {object} RegisterResponse +// @Router /register [post] func (u *UserHandler) Register(w http.ResponseWriter, r *http.Request) { var req RegisterUser err := ExtractFromRequest(r, &req)