v2.0.0: doc: add swagger from code

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2024-03-17 21:54:48 +01:00
commit ee4e2e3e31
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
8 changed files with 502 additions and 3 deletions

146
cmd/docs/docs.go Normal file
View file

@ -0,0 +1,146 @@
// 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}}",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Cette route de verifier que le token est toujours valide",
"tags": [
"auth"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
},
"post": {
"description": "Cette route permet d'obtenir un token user",
"consumes": [
"application/json"
],
"tags": [
"auth"
],
"parameters": [
{
"description": "request body",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/logins.RequestLogin"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/logins.ResponseLogin"
}
}
}
},
"delete": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Cette route permet de revoker sont token",
"tags": [
"auth"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"logins.RequestLogin": {
"type": "object",
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
"logins.ResponseLogin": {
"type": "object",
"properties": {
"publickey": {
"type": "string"
},
"token": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"ApiKeyAuth": {
"description": "Enter the token with the ` + "`" + `Bearer: ` + "`" + ` prefix, e.g. \"Bearer abcde12345\".",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}`
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "Users",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}
func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}

119
cmd/docs/swagger.json Normal file
View file

@ -0,0 +1,119 @@
{
"swagger": "2.0",
"info": {
"title": "Users",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
},
"paths": {
"/": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Cette route de verifier que le token est toujours valide",
"tags": [
"auth"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
},
"post": {
"description": "Cette route permet d'obtenir un token user",
"consumes": [
"application/json"
],
"tags": [
"auth"
],
"parameters": [
{
"description": "request body",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/logins.RequestLogin"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/logins.ResponseLogin"
}
}
}
},
"delete": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Cette route permet de revoker sont token",
"tags": [
"auth"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"logins.RequestLogin": {
"type": "object",
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
"logins.ResponseLogin": {
"type": "object",
"properties": {
"publickey": {
"type": "string"
},
"token": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"ApiKeyAuth": {
"description": "Enter the token with the `Bearer: ` prefix, e.g. \"Bearer abcde12345\".",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}

75
cmd/docs/swagger.yaml Normal file
View file

@ -0,0 +1,75 @@
definitions:
logins.RequestLogin:
properties:
password:
type: string
username:
type: string
type: object
logins.ResponseLogin:
properties:
publickey:
type: string
token:
type: string
type: object
info:
contact:
email: support@swagger.io
name: API Support
url: http://www.swagger.io/support
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: http://swagger.io/terms/
title: Users
version: "1.0"
paths:
/:
delete:
description: Cette route permet de revoker sont token
responses:
"200":
description: OK
schema:
type: string
security:
- ApiKeyAuth: []
tags:
- auth
get:
description: Cette route de verifier que le token est toujours valide
responses:
"200":
description: OK
schema:
type: string
security:
- ApiKeyAuth: []
tags:
- auth
post:
consumes:
- application/json
description: Cette route permet d'obtenir un token user
parameters:
- description: request body
in: body
name: request
required: true
schema:
$ref: '#/definitions/logins.RequestLogin'
responses:
"200":
description: OK
schema:
$ref: '#/definitions/logins.ResponseLogin'
tags:
- auth
securityDefinitions:
ApiKeyAuth:
description: 'Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".'
in: header
name: Authorization
type: apiKey
swagger: "2.0"

View file

@ -7,6 +7,14 @@ import (
logins "gitlab.g3e.fr/h6n/users/internal"
)
// Login
// @Tags auth
// @Description Cette route permet d'obtenir un token user
// @Accept json
// @Product json
// @Router / [post]
// @Param request body logins.RequestLogin true "request body"
// @Success 200 {object} logins.ResponseLogin
func login(c echo.Context) error {
var request logins.RequestLogin
if err := c.Bind(&request); err != nil {

View file

@ -6,19 +6,35 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
"gitlab.g3e.fr/h6n/users/lib"
"database/sql"
_ "github.com/lib/pq"
_ "gitlab.g3e.fr/h6n/users/cmd/docs"
)
var db *sql.DB = nil
// Logout
// @Tags auth
// @Description Cette route permet de revoker sont token
// @Product json
// @Router / [delete]
// @Success 200 {object} string
// @Security ApiKeyAuth
func logout(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
// Logout
// @Tags auth
// @Description Cette route de verifier que le token est toujours valide
// @Product json
// @Router / [get]
// @Success 200 {object} string
// @Security ApiKeyAuth
func isLoggedIn(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
@ -45,6 +61,9 @@ func skip_auth(c echo.Context) bool {
if c.Request().Method == "GET" && c.Path() == "/health" {
return true
}
if len(c.Path()) > 9 && c.Path()[:9] == "/swagger/" {
return true
}
return false
}
@ -55,6 +74,21 @@ func skip_log(c echo.Context) bool {
return false
}
// @title Users
// @version 1.0
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @description Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".
func main() {
init_database()
if err := lib.InitLoginBiscuit(); err != nil {
@ -76,6 +110,7 @@ func main() {
e.POST("/", login)
e.DELETE("/", logout)
e.GET("/health", health)
e.GET("/swagger/*", echoSwagger.WrapHandler)
// Start server
e.Logger.Fatal(e.Start(":1222"))