Skip to content

Commit 703a43e

Browse files
author
Hasan Demir
committed
Add swagger
1 parent 07c72ef commit 703a43e

File tree

11 files changed

+870
-46
lines changed

11 files changed

+870
-46
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ docker build --tag go-sanctum .
1111
Run the go-sanctum docker image
1212
````
1313
docker run -p 3000:3000 --name go-sanctum go-sanctum
14+
````
15+
Open swagger
16+
````
17+
http://localhost:3000/swagger/index.html
1418
````

controllers/v1/user.go

+39-14
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,35 @@ import (
99
"github.com/hsndmr/go-sanctum/ent"
1010
"github.com/hsndmr/go-sanctum/repositories"
1111
)
12+
type CreateRequest struct {
13+
Name string `json:"name" binding:"required" example:"name"`
14+
Email string `json:"email" binding:"required,email" example:"[email protected]"`
15+
Password string `json:"password" binding:"required" example:"secret"`
16+
}
17+
type LoginRequest struct {
18+
Email string `json:"email" binding:"required,email" example:"[email protected]"`
19+
Password string `json:"password" binding:"required" example:"secret"`
20+
}
1221

13-
// RegisterUserRoutes registers the user routes
1422
func RegisterUserRoutes(r *gin.RouterGroup) {
1523
user := &User{}
1624
r.POST("/user", user.Create)
1725
r.POST("/auth/login", user.Login)
1826
r.GET("/user", middleware.Sanctum(), user.GetUser)
1927
}
2028

21-
// validations
22-
type CreateRequest struct {
23-
Name string `json:"name" binding:"required"`
24-
Email string `json:"email" binding:"required,email"`
25-
Password string `json:"password" binding:"required"`
26-
}
27-
28-
type LoginRequest struct {
29-
Email string `json:"email" binding:"required,email"`
30-
Password string `json:"password" binding:"required"`
31-
}
32-
3329
type User struct {}
3430

35-
// Create creates a new user
31+
// Create User
32+
// @Summary This endpoint creates a new user
33+
// @Description This endpoint creates a new user
34+
// @Accept json
35+
// @Produce json
36+
// @Param message body controllerv1.CreateRequest true "User Info"
37+
// @Success 201 {object} swagger.ResponseCreatedUser "success"
38+
// @Failure 400 {object} swagger.ResponseError "bad request"
39+
// @Failure 500 {object} swagger.ResponseError "error"
40+
// @Router /user [post]
3641
func (u *User) Create(c *gin.Context) {
3742
var createRequest CreateRequest
3843
if err := c.ShouldBindJSON(&createRequest); err != nil {
@@ -80,6 +85,16 @@ func (u *User) Create(c *gin.Context) {
8085
})
8186
}
8287

88+
// Login User
89+
// @Summary This endpoint login a user with email and password
90+
// @Description This endpoint login a user with email and password
91+
// @Accept json
92+
// @Produce json
93+
// @Param message body controllerv1.LoginRequest true "User Info"
94+
// @Success 201 {object} swagger.ResponseCreatedUser "success"
95+
// @Failure 400 {object} swagger.ResponseError "bad request"
96+
// @Failure 500 {object} swagger.ResponseError "error"
97+
// @Router /auth/login [post]
8398
func (u *User) Login(c *gin.Context) {
8499
var loginRequest LoginRequest
85100
if err := c.ShouldBindJSON(&loginRequest); err != nil {
@@ -116,6 +131,16 @@ func (u *User) Login(c *gin.Context) {
116131
})
117132
}
118133

134+
// Get User
135+
// @Summary This endpoint gets a user with its token
136+
// @Description This endpoint gets a user with its token
137+
// @Accept json
138+
// @Produce json
139+
// @Param Authorization header string true "Authentication Bearer header"
140+
// @Success 200 {object} swagger.ResponseGetUser "success"
141+
// @Failure 401 {object} swagger.ResponseError "bad request"
142+
// @Failure 500 {object} swagger.ResponseError "error"
143+
// @Router /user [get]
119144
func (u *User) GetUser(c *gin.Context) {
120145
user := c.MustGet("user").(*ent.User)
121146
c.JSON(http.StatusOK, gin.H{

docs/docs.go

+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
// Package docs GENERATED BY SWAG; DO NOT EDIT
2+
// This file was generated by swaggo/swag
3+
package docs
4+
5+
import "github.com/swaggo/swag"
6+
7+
const docTemplate = `{
8+
"schemes": {{ marshal .Schemes }},
9+
"swagger": "2.0",
10+
"info": {
11+
"description": "{{escape .Description}}",
12+
"title": "{{.Title}}",
13+
"contact": {},
14+
"version": "{{.Version}}"
15+
},
16+
"host": "{{.Host}}",
17+
"basePath": "{{.BasePath}}",
18+
"paths": {
19+
"/auth/login": {
20+
"post": {
21+
"description": "This endpoint login a user with email and password",
22+
"consumes": [
23+
"application/json"
24+
],
25+
"produces": [
26+
"application/json"
27+
],
28+
"summary": "This endpoint login a user with email and password",
29+
"parameters": [
30+
{
31+
"description": "User Info",
32+
"name": "message",
33+
"in": "body",
34+
"required": true,
35+
"schema": {
36+
"$ref": "#/definitions/controllerv1.LoginRequest"
37+
}
38+
}
39+
],
40+
"responses": {
41+
"201": {
42+
"description": "success",
43+
"schema": {
44+
"$ref": "#/definitions/swagger.ResponseCreatedUser"
45+
}
46+
},
47+
"400": {
48+
"description": "bad request",
49+
"schema": {
50+
"$ref": "#/definitions/swagger.ResponseError"
51+
}
52+
},
53+
"500": {
54+
"description": "error",
55+
"schema": {
56+
"$ref": "#/definitions/swagger.ResponseError"
57+
}
58+
}
59+
}
60+
}
61+
},
62+
"/user": {
63+
"get": {
64+
"description": "This endpoint gets a user with its token",
65+
"consumes": [
66+
"application/json"
67+
],
68+
"produces": [
69+
"application/json"
70+
],
71+
"summary": "This endpoint gets a user with its token",
72+
"parameters": [
73+
{
74+
"type": "string",
75+
"description": "Authentication Bearer header",
76+
"name": "Authorization",
77+
"in": "header",
78+
"required": true
79+
}
80+
],
81+
"responses": {
82+
"200": {
83+
"description": "success",
84+
"schema": {
85+
"$ref": "#/definitions/swagger.ResponseGetUser"
86+
}
87+
},
88+
"401": {
89+
"description": "bad request",
90+
"schema": {
91+
"$ref": "#/definitions/swagger.ResponseError"
92+
}
93+
},
94+
"500": {
95+
"description": "error",
96+
"schema": {
97+
"$ref": "#/definitions/swagger.ResponseError"
98+
}
99+
}
100+
}
101+
},
102+
"post": {
103+
"description": "This endpoint creates a new user",
104+
"consumes": [
105+
"application/json"
106+
],
107+
"produces": [
108+
"application/json"
109+
],
110+
"summary": "This endpoint creates a new user",
111+
"parameters": [
112+
{
113+
"description": "User Info",
114+
"name": "message",
115+
"in": "body",
116+
"required": true,
117+
"schema": {
118+
"$ref": "#/definitions/controllerv1.CreateRequest"
119+
}
120+
}
121+
],
122+
"responses": {
123+
"201": {
124+
"description": "success",
125+
"schema": {
126+
"$ref": "#/definitions/swagger.ResponseCreatedUser"
127+
}
128+
},
129+
"400": {
130+
"description": "bad request",
131+
"schema": {
132+
"$ref": "#/definitions/swagger.ResponseError"
133+
}
134+
},
135+
"500": {
136+
"description": "error",
137+
"schema": {
138+
"$ref": "#/definitions/swagger.ResponseError"
139+
}
140+
}
141+
}
142+
}
143+
}
144+
},
145+
"definitions": {
146+
"controllerv1.CreateRequest": {
147+
"type": "object",
148+
"required": [
149+
"email",
150+
"name",
151+
"password"
152+
],
153+
"properties": {
154+
"email": {
155+
"type": "string",
156+
"example": "[email protected]"
157+
},
158+
"name": {
159+
"type": "string",
160+
"example": "name"
161+
},
162+
"password": {
163+
"type": "string",
164+
"example": "secret"
165+
}
166+
}
167+
},
168+
"controllerv1.LoginRequest": {
169+
"type": "object",
170+
"required": [
171+
"email",
172+
"password"
173+
],
174+
"properties": {
175+
"email": {
176+
"type": "string",
177+
"example": "[email protected]"
178+
},
179+
"password": {
180+
"type": "string",
181+
"example": "secret"
182+
}
183+
}
184+
},
185+
"swagger.ResponseCreatedUser": {
186+
"type": "object",
187+
"properties": {
188+
"data": {
189+
"$ref": "#/definitions/swagger.createdUser"
190+
}
191+
}
192+
},
193+
"swagger.ResponseError": {
194+
"type": "object",
195+
"properties": {
196+
"message": {
197+
"type": "string",
198+
"example": "message"
199+
}
200+
}
201+
},
202+
"swagger.ResponseGetUser": {
203+
"type": "object",
204+
"properties": {
205+
"data": {
206+
"$ref": "#/definitions/swagger.user"
207+
}
208+
}
209+
},
210+
"swagger.createdUser": {
211+
"type": "object",
212+
"properties": {
213+
"token": {
214+
"type": "string",
215+
"example": "1|V96CSH7oe3Pjr174sL4s24rCcnjZhWWw0IBUUjeh"
216+
},
217+
"user": {
218+
"$ref": "#/definitions/swagger.user"
219+
}
220+
}
221+
},
222+
"swagger.user": {
223+
"type": "object",
224+
"properties": {
225+
"email": {
226+
"type": "string",
227+
"example": "[email protected]"
228+
},
229+
"id": {
230+
"type": "string",
231+
"example": "1"
232+
},
233+
"name": {
234+
"type": "string",
235+
"example": "name"
236+
}
237+
}
238+
}
239+
}
240+
}`
241+
242+
// SwaggerInfo holds exported Swagger Info so clients can modify it
243+
var SwaggerInfo = &swag.Spec{
244+
Version: "1.0",
245+
Host: "localhost:3000",
246+
BasePath: "/api/v1",
247+
Schemes: []string{},
248+
Title: "Go Sanctum",
249+
Description: "Go Sanctum offers a simple-to-use authentication system like Laravel Sanctum.",
250+
InfoInstanceName: "swagger",
251+
SwaggerTemplate: docTemplate,
252+
}
253+
254+
func init() {
255+
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
256+
}

0 commit comments

Comments
 (0)