Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f6b6a5c
modifying ci/cd
mx-gp Feb 20, 2025
65a99bd
moving into .github
mx-gp Feb 20, 2025
610a9e7
Merge pull request #3 from mx-gp/gobuild_test
mx-gp Feb 20, 2025
0b0f20f
test cases added
mx-gp Feb 23, 2025
a3d0efd
Merge pull request #4 from mx-gp/gobuild_test
mx-gp Feb 23, 2025
c33df47
run test cases for user and also go build is working properly or not
mx-gp Feb 24, 2025
5ea8b13
Merge branch 'develop' of https://github.com/mx-gp/user_crud into use…
mx-gp Feb 24, 2025
753a732
added db connection
mx-gp Feb 24, 2025
f4be626
added postgresql status
mx-gp Feb 24, 2025
c83ed40
workflow file not using latest version
mx-gp Feb 24, 2025
2bbafed
modification for db connect
mx-gp Feb 24, 2025
2b89870
read db creds from .env/env vars
mx-gp Feb 24, 2025
880ead3
workflow modified to use creds from config
mx-gp Feb 24, 2025
2b551d3
Test Actions
mx-gp Feb 24, 2025
e0c6ce9
valid yml
mx-gp Feb 24, 2025
208fb07
triggers
mx-gp Feb 24, 2025
2119fa6
run with default credentials
mx-gp Feb 24, 2025
84f0a1c
remove unnecessary db create
mx-gp Feb 24, 2025
869853c
setUpto use creds properly
mx-gp Feb 24, 2025
e7c394d
Create table if not exists
mx-gp Feb 24, 2025
fdbbf07
setTestDB refactor
mx-gp Feb 24, 2025
a202423
.env modified
mx-gp Feb 26, 2025
2d8cdc9
fixed test case db connection issues
mx-gp Feb 26, 2025
9e83134
gitignore
mx-gp Feb 26, 2025
34ee1fa
test case fixes
mx-gp Feb 26, 2025
9f1b77e
improvements in yml
mx-gp Feb 26, 2025
22eb2cb
Merge pull request #5 from mx-gp/user/testcases
mx-gp Feb 26, 2025
05f8490
adding test cases badge for develop branch
mx-gp Feb 26, 2025
8361ef3
modifying main.go and moving to seperate folder
mx-gp Mar 1, 2025
2ddb806
moving routes.go
mx-gp Mar 1, 2025
a02d448
refactor and moving: user_test.go
mx-gp Mar 1, 2025
63db409
Repository added for seperatin db queries logic
mx-gp Mar 1, 2025
2847307
adding dockerfile and docker-compose for setting up dependencies
mx-gp Mar 1, 2025
a3686d3
refactor user controller
mx-gp Mar 1, 2025
9552093
adding auth middleware which uses jwt
mx-gp Mar 1, 2025
80441d1
adding middleware for ratelimit implementation
mx-gp Mar 1, 2025
ad7556c
refactor: user models
mx-gp Mar 1, 2025
7f38b1d
updating workflow.yml according to latest changes in logic
mx-gp Mar 1, 2025
9ba1c9b
updating go mod files
mx-gp Mar 1, 2025
27706da
create .env in workflow
mx-gp Mar 1, 2025
e17eded
test path
mx-gp Mar 1, 2025
7a58ef9
testcases path fix
mx-gp Mar 1, 2025
0f18a18
keyword fix for yml
mx-gp Mar 1, 2025
34a3f2a
removing unnecessary comments
mx-gp Mar 1, 2025
77ba59f
Merge pull request #7 from mx-gp/microservice
mx-gp Mar 1, 2025
bf3e82f
upgrade to 1.24
mx-gp Nov 8, 2025
628cbb2
Merge branch 'develop' of https://github.com/mx-gp/user_crud into mic…
mx-gp Nov 8, 2025
4c6ba5d
Merge pull request #8 from mx-gp/microservice
mx-gp Nov 8, 2025
511661c
Merge branch 'master' of https://github.com/mx-gp/user_crud into develop
mx-gp Nov 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DB_USER=mx
DB_NAME=crud_db
DB_HOST=localhost
DB_PORT=5432
61 changes: 61 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: User Service CI

on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop

jobs:
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: userdb
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Install Dependencies
run: go mod tidy

- name: Create .env file for Testing
run: |
echo "JWT_SECRET=testsecret" > .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_USER=postgres" >> .env
echo "DB_PASSWORD=postgres" >> .env
echo "DB_NAME=userdb" >> .env

- name: Run Tests
env:
JWT_SECRET: testsecret
DB_HOST: localhost
DB_PORT: 5432
DB_USER: postgres
DB_PASSWORD: postgres
DB_NAME: userdb
run: go test ./tests/ -v
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
.galus.toml
go.mod
go.sum
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.21

WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN go mod tidy

COPY . .

RUN go build -o main ./cmd/main.go

CMD ["./main"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
[![Go Build and Test with Database](https://github.com/mx-gp/user_crud/actions/workflows/workflow.yml/badge.svg?branch=develop)](https://github.com/mx-gp/user_crud/actions/workflows/workflow.yml)

[![develop](https://github.com/mx-gp/user_crud/actions/workflows/workflow.yml/badge.svg)](https://github.com/mx-gp/user_crud/actions/workflows/workflow.yml)
5 changes: 3 additions & 2 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main
package cmd

import (
"user_crud/config"
"user_crud/routes"

"github.com/gin-gonic/gin"
)
Expand All @@ -13,7 +14,7 @@ func main() {
config.ConnectDB()

// Register Routes
UserRoutes(r)
routes.UserRoutes(r)

// Start Server
r.Run(":8080")
Expand Down
11 changes: 6 additions & 5 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"strconv"
"user_crud/models"
"user_crud/repository"
"user_crud/utils"

"github.com/gin-gonic/gin"
Expand All @@ -17,7 +18,7 @@ func CreateUser(c *gin.Context) {
return
}

err := models.CreateUser(user)
err := repository.CreateUser(user)
if err != nil {
utils.SendErrorResponse(c, http.StatusInternalServerError, "Failed to create user")
return
Expand All @@ -28,7 +29,7 @@ func CreateUser(c *gin.Context) {

// Get All Users
func GetAllUsers(c *gin.Context) {
users, err := models.GetAllUsers()
users, err := repository.GetAllUsers()
if err != nil {
utils.SendErrorResponse(c, http.StatusInternalServerError, "Failed to fetch users")
return
Expand All @@ -39,7 +40,7 @@ func GetAllUsers(c *gin.Context) {
// Get User by ID
func GetUserByID(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
user, err := models.GetUserByID(id)
user, err := repository.GetUserByID(id)
if err != nil {
utils.SendErrorResponse(c, http.StatusNotFound, "User not found")
return
Expand All @@ -55,7 +56,7 @@ func UpdateUser(c *gin.Context) {
return
}

err := models.UpdateUser(user)
err := repository.UpdateUser(user)
if err != nil {
utils.SendErrorResponse(c, http.StatusInternalServerError, "Failed to update user")
return
Expand All @@ -68,7 +69,7 @@ func UpdateUser(c *gin.Context) {
func DeleteUser(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))

err := models.DeleteUser(id)
err := repository.DeleteUser(id)
if err != nil {
utils.SendErrorResponse(c, http.StatusInternalServerError, "Failed to delete user")
return
Expand Down
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.8"

services:
user-service:
build: .
ports:
- "8080:8080"
depends_on:
- db
env_file:
- .env

db:
image: postgres:15
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: userdb
ports:
- "5432:5432"
30 changes: 0 additions & 30 deletions go-build.yml

This file was deleted.

16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
module user_crud

go 1.23.4
go 1.24.4

toolchain go1.24.5

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.10.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
golang.org/x/time v0.10.0
)

require (
github.com/BurntSushi/toml v1.5.0 // indirect
github.com/aliftech/galus v0.0.0-20250726090607-302d48f29cd0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/spf13/cobra v1.9.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/aliftech/galus v0.0.0-20250726090607-302d48f29cd0 h1:C8DDNoVqUMxoB+fdQVuIgRd3u8WC47VSG/Bovv3XbCU=
github.com/aliftech/galus v0.0.0-20250726090607-302d48f29cd0/go.mod h1:nElRcG5JIPLyaZOYuKKa3xCp5JErL3kqY3bdixdm74c=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
Expand All @@ -6,9 +10,16 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
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/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand All @@ -28,6 +39,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -40,6 +53,9 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -51,6 +67,11 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
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/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -74,12 +95,17 @@ golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.10.0 h1:3usCWA8tQn0L8+hFJQNgzpWbd89begxN66o1Ojdn5L4=
golang.org/x/time v0.10.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
Expand Down
21 changes: 21 additions & 0 deletions middleware/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package middleware

import (
"net/http"
"user_crud/utils"

"github.com/gin-gonic/gin"
)

// AuthMiddleware - Protect routes with JWT authentication
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
if token == "" || !utils.ValidateJWT(token) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
c.Abort()
return
}
c.Next()
}
}
18 changes: 18 additions & 0 deletions middleware/ratelimit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import (
"github.com/gin-gonic/gin"
"golang.org/x/time/rate"
)

var limiter = rate.NewLimiter(1, 5) // 1 request per second, burst up to 5

func RateLimit() gin.HandlerFunc {
return func(c *gin.Context) {
if !limiter.Allow() {
c.AbortWithStatusJSON(429, gin.H{"error": "Too many requests"})
return
}
c.Next()
}
}
Loading
Loading