Skip to content

Commit 0ae190d

Browse files
committed
Added Promotheus Middleware
1 parent e03eb99 commit 0ae190d

File tree

6 files changed

+341
-20
lines changed

6 files changed

+341
-20
lines changed

README.md

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# golang-rest-api-template
2+
23
[![license](https://img.shields.io/badge/license-MIT-green)](https://raw.githubusercontent.com/MitulShah1/golang-rest-api-template/main/LICENSE)
34
[![build](https://github.com/MitulShah1/golang-rest-api-template//actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/MitulShah1/golang-rest-api-template/actions/workflows/go.yml)
45
[![codecov](https://codecov.io/github/MitulShah1/golang-rest-api-template/graph/badge.svg?token=88JSRODXSS)](https://codecov.io/github/MitulShah1/golang-rest-api-template)
56

67
## Overview
8+
79
This is a template for building production-ready and easily extendible REST API using Go. It follows best practices and includes a standardized project structure with all necessary components for building scalable microservices.
810

911
## Features
12+
1013
- Structured logging
1114
- Middleware support (authentication, etc.)
1215
- Configuration management
@@ -19,18 +22,21 @@ This is a template for building production-ready and easily extendible REST API
1922

2023
The main ones are:
2124

22-
* [gorilla/mux](http://www.gorillatoolkit.org/pkg/mux) for routing
23-
* [go-playground/validator](https://github.com/go-playground/validator) for request validation
24-
* [go-sql-driver/mysql](https://github.com/go-sql-driver/mysql) for MySQL database access
25-
* [jmoiron/sqlx](https://github.com/jmoiron/sqlx) for enhanced database access
26-
* [Masterminds/squirrel](https://github.com/Masterminds/squirrel) for SQL builder
27-
* [golang-migrate/migrate](https://github.com/golang-migrate/migrate) for database migrations
28-
* [swaggo/swag](https://github.com/swaggo/swag) for API documentation generation
29-
* [strechr/testify](https://github.com/stretchr/testify) for writing easier test assertions
30-
* [mockery/](https://vektra.github.io/mockery/) for generating mock interfaces
31-
* [uber/zap](go.uber.org/zap) for structured logging
25+
- [gorilla/mux](http://www.gorillatoolkit.org/pkg/mux) for routing
26+
- [go-playground/validator](https://github.com/go-playground/validator) for request validation
27+
- [go-sql-driver/mysql](https://github.com/go-sql-driver/mysql) for MySQL database access
28+
- [jmoiron/sqlx](https://github.com/jmoiron/sqlx) for enhanced database access
29+
- [Masterminds/squirrel](https://github.com/Masterminds/squirrel) for SQL builder
30+
- [golang-migrate/migrate](https://github.com/golang-migrate/migrate) for database migrations
31+
- [swaggo/swag](https://github.com/swaggo/swag) for API documentation generation
32+
- [strechr/testify](https://github.com/stretchr/testify) for writing easier test assertions
33+
- [mockery](https://vektra.github.io/mockery/) for generating mock interfaces
34+
- [uber/zap](go.uber.org/zap) for structured logging
35+
- [prometheus/client_golang](https://github.com/prometheus/client_golang) for metrics
36+
3237
## Project Structure
33-
```
38+
39+
```sh
3440
golang-microservice-template/
3541
│── cmd/
3642
│ ├── server/ # Main entry point for the service
@@ -50,8 +56,9 @@ golang-microservice-template/
5056
│ ├── logger/
5157
│ │ ├── logger.go
5258
│ ├── middleware/
53-
│ │ ├── basic_auth.go
54-
│ │ ├── cors.go
59+
│ │ ├── basic_auth.go # Basic authentication middleware
60+
│ │ ├── cors.go # CORS middleware
61+
│ ├── ├── promotheus.go # Prometheus metrics
5562
│── test/
5663
│ ├── e2e/ # End-to-end tests
5764
│── Dockerfile # Docker build configuration
@@ -65,90 +72,115 @@ golang-microservice-template/
6572
## Getting Started
6673

6774
### Prerequisites
75+
6876
- Go 1.21 or higher
6977
- Docker and Docker Compose
7078
- Make
7179

7280
### All Make Commands
81+
7382
To Check All Commands:
83+
7484
```bash
7585
make help
7686
```
87+
7788
![Make Help Commands](make_help.png)
7889

7990
### Running the Application
80-
1. Clone the repository
91+
92+
1; Clone the repository
93+
8194
```bash
8295
git clone https://github.com/MitulShah1/golang-rest-api-template.git
8396
```
8497

85-
2. Create .env file from .env.example add details
98+
2; Create .env file from .env.example add details
99+
86100
```bash
87101
make env
88102
```
89103

90-
3. Start the application using Docker Compose
104+
3; Start the application using Docker Compose
105+
91106
```bash
92107
make docker_up
93108
```
94109

95110
### Development
111+
96112
Build the application:
113+
97114
```bash
98115
make build
99116
```
100117

101118
Run tests:
119+
102120
```bash
103121
make test
104122
```
105123

106124
Generate API documentation:
125+
107126
```bash
108127
make generate_docs
109128
```
110129

111130
### DB Migrations
131+
112132
Create Migration:
133+
113134
```bash
114135
make create_migration
115136
```
116137

117138
Run Migrations:
139+
118140
```bash
119141
make migration_up
120142
```
121143

122144
Down Migrations:
145+
123146
```bash
124147
make migration_down
125148
```
126149

127150
## Configuration
151+
128152
Configuration is managed through `.env`. Environment variables can override these settings.
129153

130154
## API Documentation
155+
131156
API documentation is generated using Swagger. The documentation is available at `http://localhost:8080/swagger/index.html`.
132157

158+
## Prometheus Metrics
159+
160+
Prometheus metrics are exposed at `http://localhost:8080/metrics`.
161+
133162
## Testing
163+
134164
- Unit tests are alongside the code
135165
- Integration tests are in the `test/` directory
136166
- Run all tests with `make test`
137167

138168
## Deployment
169+
139170
The project includes:
171+
140172
- Dockerfile for containerization
141173
- docker-compose.yml for local development
142174
- Jenkinsfile for CI/CD pipeline
143175

144176
## Contributing
177+
145178
1. Fork the repository
146179
2. Create your feature branch
147180
3. Commit your changes
148181
4. Push to the branch
149182
5. Create a new Pull Request
150183

151184
## License
152-
This project is licensed under the MIT License - see the LICENSE file for details
153-
154185

186+
This project is licensed under the MIT License - see the LICENSE file for details

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/gorilla/mux v1.8.1
1111
github.com/jmoiron/sqlx v1.4.0
1212
github.com/joho/godotenv v1.5.1
13+
github.com/prometheus/client_golang v1.21.1
1314
github.com/stretchr/testify v1.10.0
1415
github.com/swaggo/http-swagger/v2 v2.0.2
1516
github.com/swaggo/swag v1.16.4
@@ -19,6 +20,8 @@ require (
1920
require (
2021
filippo.io/edwards25519 v1.1.0 // indirect
2122
github.com/KyleBanks/depth v1.2.1 // indirect
23+
github.com/beorn7/perks v1.0.1 // indirect
24+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2225
github.com/davecgh/go-spew v1.1.1 // indirect
2326
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
2427
github.com/go-openapi/jsonpointer v0.19.5 // indirect
@@ -28,11 +31,16 @@ require (
2831
github.com/go-playground/locales v0.14.1 // indirect
2932
github.com/go-playground/universal-translator v0.18.1 // indirect
3033
github.com/josharian/intern v1.0.0 // indirect
34+
github.com/klauspost/compress v1.17.11 // indirect
3135
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
3236
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
3337
github.com/leodido/go-urn v1.4.0 // indirect
3438
github.com/mailru/easyjson v0.7.6 // indirect
39+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
3540
github.com/pmezard/go-difflib v1.0.0 // indirect
41+
github.com/prometheus/client_model v0.6.1 // indirect
42+
github.com/prometheus/common v0.62.0 // indirect
43+
github.com/prometheus/procfs v0.15.1 // indirect
3644
github.com/stretchr/objx v0.5.2 // indirect
3745
github.com/swaggo/files/v2 v2.0.0 // indirect
3846
go.uber.org/multierr v1.10.0 // indirect
@@ -41,6 +49,7 @@ require (
4149
golang.org/x/sys v0.29.0 // indirect
4250
golang.org/x/text v0.21.0 // indirect
4351
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
52+
google.golang.org/protobuf v1.36.1 // indirect
4453
gopkg.in/yaml.v2 v2.4.0 // indirect
4554
gopkg.in/yaml.v3 v3.0.1 // indirect
4655
)

go.sum

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
66
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
77
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
88
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
9+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
10+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
11+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
12+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
913
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1014
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1115
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -33,6 +37,8 @@ github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1
3337
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
3438
github.com/go-sql-driver/mysql v1.9.1 h1:FrjNGn/BsJQjVRuSa8CBrM5BWA9BWoXXat3KrtSb/iI=
3539
github.com/go-sql-driver/mysql v1.9.1/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
40+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
41+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3642
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
3743
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
3844
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
@@ -42,11 +48,17 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA
4248
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
4349
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
4450
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
51+
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
52+
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
4553
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
54+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
55+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
4656
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4757
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4858
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4959
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
60+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
61+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
5062
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
5163
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
5264
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
@@ -61,10 +73,21 @@ github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA
6173
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
6274
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
6375
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
64-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
76+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
77+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
6578
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
6679
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6780
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
81+
github.com/prometheus/client_golang v1.21.1 h1:DOvXXTqVzvkIewV/CDPFdejpMCGeMcbGCQ8YOmu+Ibk=
82+
github.com/prometheus/client_golang v1.21.1/go.mod h1:U9NM32ykUErtVBxdvD3zfi+EuFkkaBvMb09mIfe0Zgg=
83+
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
84+
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
85+
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
86+
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
87+
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
88+
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
89+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
90+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
6891
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6992
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
7093
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
@@ -99,10 +122,13 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
99122
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
100123
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
101124
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
125+
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
126+
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
102127
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
103128
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
104-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
105129
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
130+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
131+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
106132
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
107133
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
108134
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

internal/handlers/server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/MitulShah1/golang-rest-api-template/package/database"
1414
"github.com/MitulShah1/golang-rest-api-template/package/logger"
1515
"github.com/MitulShah1/golang-rest-api-template/package/middleware"
16+
"github.com/prometheus/client_golang/prometheus/promhttp"
1617

1718
_ "github.com/MitulShah1/golang-rest-api-template/docs"
1819
catApi "github.com/MitulShah1/golang-rest-api-template/internal/handlers/category"
@@ -36,6 +37,16 @@ func NewServer(address string, logger *logger.Logger, db *database.Database) (*S
3637
// Serve Swagger UI
3738
router.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
3839

40+
// Promotheus metrics
41+
promotheuseMiddleware := middleware.NewPrometheusMiddleware(middleware.Config{
42+
Namespace: "golang_rest_api_template",
43+
Subsystem: "http",
44+
})
45+
46+
router.Use(promotheuseMiddleware.Middleware)
47+
48+
router.Handle("/metrics", promhttp.Handler())
49+
3950
r := router.PathPrefix("/api").Subrouter()
4051

4152
// health check API

0 commit comments

Comments
 (0)