Skip to content

Commit

Permalink
V2 - enviroment, JWT, Redis as DB & storage, middlewares, enhanced un…
Browse files Browse the repository at this point in the history
…it tests, structure and code, SSL support & generating certs, go modules and travis update
  • Loading branch information
Massad committed May 10, 2020
1 parent 396ed9c commit 30f006a
Show file tree
Hide file tree
Showing 18 changed files with 1,047 additions and 302 deletions.
11 changes: 11 additions & 0 deletions .env_rename_me
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ENV=LOCAL
PORT=9000
SSL=TRUE
DB_USER="postgres"
DB_PASS="postgres"
DB_NAME="golang_gin_db"
ACCESS_SECRET="ashasdjhjhjadhasdaa123"
REFERSH_SECRET="hjsajdhkjhf41jhagggdga"
REDIS_SECRET="hjfhjhasdfkyuy2"
REDIS_HOST=127.0.0.1:6379
REDIS_PASSWORD=
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

# OS generated files #
######################
.DS_Store
.DS_Store?

# Logs #
######################
*.log

# SENSITVE DATA #
.env
cert/

# BUILD #
./gin-boilerplate
26 changes: 16 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: go
go:
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
- master

services:
Expand All @@ -11,15 +13,17 @@ services:
dist: trusty

addons:
postgresql: "9.6"
postgresql: "12"
apt:
sources:
- ubuntu-toolchain-r-test
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- gcc-4.8
- clang
- postgresql-9.6-postgis-2.3
- g++-4.8
- gcc-4.8
- clang
- postgresql-12
- postgresql-client-12
- postgresql-server-dev-12

before_install:
- "psql -c 'create database golang_gin_db;' -U postgres"
Expand All @@ -28,8 +32,10 @@ before_install:
- sleep 3

install:
- go get -t -v ./...
- go get github.com/bmizerany/assert
- go mod init
- go list -m all
- mv .env_rename_me .env
- mkdir cert/ && sh generate-certificate.sh

script:
- go test -v ./tests/*
- go test -v ./tests/*
104 changes: 93 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
[![Build Status](https://travis-ci.org/Massad/gin-boilerplate.svg?branch=master)](https://travis-ci.org/Massad/gin-boilerplate)
[![Join the chat at https://gitter.im/Massad/gin-boilerplate](https://badges.gitter.im/Massad/gin-boilerplate.svg)](https://gitter.im/Massad/gin-boilerplate?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Welcome to **Golang Gin boilerplate**!
Welcome to **Golang Gin boilerplate** v2

The fastest way to deploy a restful api's with [Gin Framework](https://gin-gonic.github.io/gin/) with a structured project that defaults to **PostgreSQL** database and **Redis** as the session storage.
The fastest way to deploy a restful api's with [Gin Framework](https://gin-gonic.github.io/gin/) with a structured project that defaults to **PostgreSQL** database and **JWT** authentication middleware stored in **Redis**

## Configured with

* [go-gorp](https://github.com/go-gorp/gorp): Go Relational Persistence
* [RedisStore](https://github.com/gin-gonic/contrib/tree/master/sessions): Gin middleware for session management with multi-backend support (currently cookie, Redis).
* Built-in **CORS Middleware**
* Feature **PostgreSQL 9.6** JSON queries
* Unit test
- [go-gorp](https://github.com/go-gorp/gorp): Go Relational Persistence
- [jwt-go](github.com/dgrijalva/jwt-go): JSON Web Tokens (JWT) as middleware
- [go-redis](https://github.com/go-redis/redis): Redis support for Go
- Go Modules
- Built-in **CORS Middleware**
- Built-in **RequestID Middleware**
- Feature **PostgreSQL 12** with JSON/JSONB queries & trigger functions
- SSL Support
- Enviroment support
- Unit test
- And few other important utilties to kickstart any project

### Installation

Expand All @@ -26,23 +32,52 @@ $ cd $GOPATH/src/github.com/Massad/gin-boilerplate
```

```
$ go get -t -v ./...
$ go mod init
```

> Sometimes you need to get this package manually
```
$ go get github.com/bmizerany/assert
$ go list -m all
```

You will find the **database.sql** in `db/database.sql`

And you can import the postgres database using this command:

```
$ psql -U postgres -h localhost < ./db/database.sql
```

Tip:

You will find that we added 2 trigger functions to the dabatase:

- public.created_at_column()
- public.update_at_column()

Those are added to the `updated_at` and `created_at` columns to update the latest timestamp automatically in both **user** and **article** tables. You can explore the tables and public schema for more info.

## Running Your Application

Rename .env_rename_me to .env and place your credentials

```
$ mv .env_rename_me .env
```

Generate SSL certificates (Optional)

> If you don't SSL now, change `SSL=TRUE` to `SSL=FALSE` in the `.env` file
```
$ mkdir cert/
```

```
$ sh generate-certificate.sh
```

> Make sure to change the values in .env for your databases
```
$ go run *.go
```
Expand All @@ -63,19 +98,66 @@ $ ./gin-boilerplate
$ go test -v ./tests/*
```


## Import Postman Collection (API's)

You can import from this [link](https://www.getpostman.com/collections/ac0680f90961bafd5de7). If you don't have **Postman**, check this link [https://www.getpostman.com](https://www.getpostman.com/)

Includes the following:

- User
- Login
- Register
- Logout
- Article
- Create
- Update
- Get Article
- Get Articles
- Delete
- Auth
- Refresh Token

Tip: Add the generated `access_token` from the success login in the **global variable** for later use in other requests in the "**Tests**" tab in **Login** reqeust:

```
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
var jsonData = JSON.parse(responseBody);
pm.globals.set("token", jsonData.token.access_token);
pm.globals.set("refresh_token", jsonData.token.refresh_token);
});
```

And in each request that needs to be authenticated add the following **Authorization**:

Authorization -> Bearer Token with value of {{token}}

In this way, whenever you hit login from Postman it will automatically take the `access_token` and fills it in the golbal variable for later use. And of course, it will update it whenever you hit login again.

## Version 1

No longer supported

You will find the last update on v1 in [v1-session-cookies-auth](https://github.com/Massad/gin-boilerplate/tree/v1-session-cookies-auth) branch or [v1.0.5 release](https://github.com/Massad/gin-boilerplate/releases/tag/1.05) that supported the authentication using the **session** and **cookies** stored in **Redis** if needed.

- [RedisStore](https://github.com/gin-gonic/contrib/tree/master/sessions): Gin middleware for session management with multi-backend support (currently cookie, Redis).

## Contribution

You are welcome to contribute to keep it up to date and always improving!

If you have any question or need help, drop a message at [https://gitter.im/Massad/gin-boilerplate](https://gitter.im/Massad/gin-boilerplate)

## Credit

The implemented JWT inspired from this article: [Using JWT for Authentication in a Golang Application](https://www.nexmo.com/blog/2020/03/13/using-jwt-for-authentication-in-a-golang-application-dr) worth reading it, thanks [Victor Steven](https://www.nexmo.com/blog/author/victor-steven)

---

## License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining
Expand Down
Loading

0 comments on commit 30f006a

Please sign in to comment.