Skip to content

Commit

Permalink
new doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Zorynix committed Mar 17, 2024
1 parent c85bc5d commit 57fb3a1
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
56 changes: 56 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,43 @@ const docTemplate = `{
}
}
},
"/login": {
"post": {
"description": "handles login requests by checking username and password",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "User login",
"parameters": [
{
"description": "Login Credentials",
"name": "LoginRequest",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/routes.LoginRequest"
}
}
],
"responses": {
"200": {
"description": "Returns login token",
"schema": {
"$ref": "#/definitions/routes.LoginResponse"
}
},
"400": {
"description": "Invalid request or Unauthorized"
},
"401": {
"description": "Invalid request or Unauthorized"
}
}
}
},
"/movie-add": {
"post": {
"description": "Adds a new movie with the given details including title, description, release date, and rating",
Expand Down Expand Up @@ -425,6 +462,25 @@ const docTemplate = `{
"type": "string"
}
}
},
"routes.LoginRequest": {
"type": "object",
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
"routes.LoginResponse": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
}
}
}`
Expand Down
56 changes: 56 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,43 @@
}
}
},
"/login": {
"post": {
"description": "handles login requests by checking username and password",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "User login",
"parameters": [
{
"description": "Login Credentials",
"name": "LoginRequest",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/routes.LoginRequest"
}
}
],
"responses": {
"200": {
"description": "Returns login token",
"schema": {
"$ref": "#/definitions/routes.LoginResponse"
}
},
"400": {
"description": "Invalid request or Unauthorized"
},
"401": {
"description": "Invalid request or Unauthorized"
}
}
}
},
"/movie-add": {
"post": {
"description": "Adds a new movie with the given details including title, description, release date, and rating",
Expand Down Expand Up @@ -414,6 +451,25 @@
"type": "string"
}
}
},
"routes.LoginRequest": {
"type": "object",
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
"routes.LoginResponse": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
}
}
}
36 changes: 36 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ definitions:
title:
type: string
type: object
routes.LoginRequest:
properties:
password:
type: string
username:
type: string
type: object
routes.LoginResponse:
properties:
token:
type: string
type: object
info:
contact: {}
paths:
Expand Down Expand Up @@ -152,6 +164,30 @@ paths:
summary: Lists all actors
tags:
- actor
/login:
post:
consumes:
- application/json
description: handles login requests by checking username and password
parameters:
- description: Login Credentials
in: body
name: LoginRequest
required: true
schema:
$ref: '#/definitions/routes.LoginRequest'
produces:
- application/json
responses:
"200":
description: Returns login token
schema:
$ref: '#/definitions/routes.LoginResponse'
"400":
description: Invalid request or Unauthorized
"401":
description: Invalid request or Unauthorized
summary: User login
/movie-add:
post:
consumes:
Expand Down
7 changes: 7 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"vk.com/m/auth"
)

// AuthMiddleware is a middleware for JWT authentication
// @Summary JWT Authentication Middleware
// @Description It validates the JWT token and ensures the role is allowed to access the endpoint
// @Param Authorization header string true "Bearer [JWT token]"
// @Success 200 "Access granted"
// @Failure 401 "Unauthorized or Invalid token"
// @Failure 403 "Forbidden - Role not allowed"
func AuthMiddleware(next http.Handler, allowedRoles ...string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeaderParts := strings.Split(r.Header.Get("Authorization"), " ")
Expand Down
9 changes: 9 additions & 0 deletions routes/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type LoginResponse struct {
Token string `json:"token"`
}

// LoginHandler handles user login requests
// @Summary User login
// @Description handles login requests by checking username and password
// @Accept json
// @Produce json
// @Param LoginRequest body LoginRequest true "Login Credentials"
// @Success 200 {object} LoginResponse "Returns login token"
// @Failure 400,401 "Invalid request or Unauthorized"
// @Router /login [post]
func (router *Router) LoginHandler(w http.ResponseWriter, r *http.Request) {
var req LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Expand Down

0 comments on commit 57fb3a1

Please sign in to comment.