-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bruno-chavez/feature/complete-refactor
Complete refactor of module
- Loading branch information
Showing
25 changed files
with
237 additions
and
519 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PORT=8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
SESSION_STORE_KEY=[00 126 119 122 10 123 137 34 124 31 12 192 126 361 250 215 219 555 113 233] | ||
SESSION_STORE_ADDRESS=:6379 | ||
SESSION_STORE_SIZE=10 | ||
SESSION_STORE_PASSWORD= | ||
DB=postgres://postgres:postgres@localhost:5432/db-name | ||
FRONT-END-ADDRESS=http://localhost:8081 | ||
PORT=8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- feature/* | ||
|
||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.15 | ||
|
||
- name: Lint | ||
run: | | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.39.0 | ||
./bin/golangci-lint run | ||
- name: Test | ||
run: | | ||
wget -o -q https://github.com/mfridman/tparse/releases/download/v0.8.3/tparse_0.8.3_Linux_x86_64.tar.gz -O tparse.tar.gz | ||
tar xvzf tparse.tar.gz | ||
go test ./... -json -cover | ./tparse | ||
- name: Build Go binary | ||
run: go build | ||
|
||
- name: Build Docker Image | ||
run: docker build -t $(echo $GITHUB_SHA | head -c7) . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
.env | ||
.idea |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
FROM golang:1.15 as builder | ||
|
||
ENV GO111MODULE=on | ||
|
||
WORKDIR /service | ||
|
||
COPY go.mod . | ||
COPY go.sum . | ||
|
||
RUN go mod download | ||
|
||
COPY . . | ||
|
||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build | ||
|
||
|
||
FROM scratch | ||
|
||
ENV PORT=8080 | ||
|
||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
|
||
# Rename go-microservice-template with your module name | ||
COPY --from=builder /service/go-microservice-template . | ||
|
||
EXPOSE ${PORT} | ||
ENTRYPOINT ["/search"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,39 @@ | ||
[![GoDoc](https://godoc.org/github.com/bruno-chavez/go-web-template?status.svg)](https://godoc.org/github.com/bruno-chavez/go-web-template) | ||
[![Build Status](https://travis-ci.org/bruno-chavez/go-web-template.svg?branch=master)](https://travis-ci.org/bruno-chavez/go-web-template) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/bruno-chavez/go-web-template)](https://goreportcard.com/report/github.com/bruno-chavez/go-web-template) | ||
|
||
# Description | ||
|
||
`go-web-template` main purpose is to be a starting point | ||
for web development in Go, | ||
usually just as a API for a front-end application. | ||
`go-microservice-template` main purpose is to be a starting point | ||
for a REST API in Go. Reducing boilerplate writing and speeding up development of new microservices | ||
|
||
# How to use | ||
|
||
1. Rename package, project, go mod file and delete .git directory | ||
|
||
2. Download dependencies | ||
|
||
3. Have Redis installed and a server running. | ||
|
||
4. Have PostgresSQL installed and a database created. | ||
|
||
5. Rename the `.env.example` file to `.env` and customize the parameters accordingly | ||
|
||
Note: Don't store the `SESSION_STORE_KEY` in your source code and ensure your key is sufficiently random and large. | ||
|
||
# Features | ||
|
||
+ Ready to use custom authentication routes, for registering, | ||
login in and login out users. | ||
|
||
+ Ready to use cookie based sessions with | ||
[gorilla/sessions](https://github.com/gorilla/sessions) and | ||
Redis as a session store with | ||
[redistore](https://github.com/boj/redistore). | ||
|
||
+ Ready to use Relational Database connection to store user | ||
and domain data with PostgreSQL, | ||
[lib/pq](https://github.com/lib/pq) as the driver and | ||
[sqlx](https://github.com/jmoiron/sqlx) | ||
to help with raw queries. | ||
|
||
+ Handles CORS requests with | ||
[rs/cors](https://github.com/rs/cors). | ||
|
||
+ Safely hashes and salts user passwords with the official | ||
[bcrypt](https://godoc.org/golang.org/x/crypto/bcrypt) | ||
implementation. | ||
|
||
+ Fast and easy to use router with | ||
[julienschmidt/httprouter](https://github.com/julienschmidt/httprouter). | ||
|
||
+ Migration file for a `user` table. | ||
Currently no automatic way of running migrations is provided. | ||
|
||
+ Loads environment variables from an `.env` file | ||
with the help of [godotenv](https://github.com/joho/godotenv). | ||
|
||
1. Clone repository. | ||
2. Delete `go.mod`, `go.sum`, `LICENSE` and `.git`. | ||
3. Run `go mod init`. | ||
4. Rename the binary name on `Dockerfile` with the name of you module. Do the same with the files inside `handlers/`, `server/` and `main.go`. | ||
5. Rename the `.env.example` file to `.env` and customize the parameters accordingly. | ||
|
||
# Features | ||
+ Fast and easy to use router with [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter). | ||
+ Graceful shutdown out of the box. | ||
+ Environment variables loading from an `.env` file with the help of [godotenv](https://github.com/joho/godotenv). | ||
+ Dependency management with Go Modules. | ||
+ Optional CORS handling with [rs/cors](https://github.com/rs/cors) see `server/server.go` for how to enable it. | ||
|
||
+ Continuous Integration with Travis-CI. | ||
# Continuous Integration | ||
`go-microservice-template` currently, it uses Github Actions for linting, testing and building the microservice. Current workflow: | ||
|
||
# To Do | ||
+ Lints with [golangci-lint](https://github.com/golangci/golangci-lint). | ||
+ Run unit tests and prints the results with [tparse](https://github.com/mfridman/tparse). | ||
+ Builds binary. | ||
+ Builds Docker Image. | ||
|
||
+ Unit testing. | ||
See [ci.yml](https://github.com/bruno-chavez/go-web-template/blob/master/.github/workflows/ci.yml) for more info. | ||
|
||
# Contribute | ||
# Notes | ||
+ Previously the module was called `go-web-template`, it lacked any kind of meaningful updates and was too opinionated for its purpose. I decided to complete refactor it and came up with the current iteration of `go-microservice-template`. | ||
|
||
Found a bug or an error? Post it in the | ||
[issue tracker](https://github.com/bruno-chavez/go-web-template/issues). | ||
# Contribute | ||
Found a bug or an error? Post it in the[issue tracker](https://github.com/bruno-chavez/go-microservice-template/issues). | ||
|
||
Want to add an awesome new feature? | ||
[Fork](https://github.com/bruno-chavez/go-web-template/fork) | ||
this repository, add your feature on a new branch, | ||
then send a pull request. | ||
Want to add an awesome new feature? [Fork](https://github.com/bruno-chavez/go-microservice-template/fork) this repository, add your feature on a new branch, then send a pull request. | ||
|
||
# License | ||
The MIT License (MIT) | ||
Copyright (c) 2019 Bruno Chavez | ||
Copyright (c) 2021 Bruno Chavez |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,8 @@ | ||
module go-web-template | ||
module go-microservice-template | ||
|
||
go 1.13 | ||
go 1.15 | ||
|
||
require ( | ||
github.com/garyburd/redigo v1.6.0 // indirect | ||
github.com/gorilla/sessions v1.2.0 | ||
github.com/jmoiron/sqlx v1.2.0 | ||
github.com/joho/godotenv v1.3.0 | ||
github.com/julienschmidt/httprouter v1.3.0 | ||
github.com/lib/pq v1.2.0 | ||
github.com/lopezator/migrator v0.2.0 | ||
github.com/rs/cors v1.7.0 | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 | ||
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,4 @@ | ||
github.com/garyburd/redigo v1.6.0 h1:0VruCpn7yAIIu7pWVClQC8wxCJEcG3nyzpMSHKi1PQc= | ||
github.com/garyburd/redigo v1.6.0/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= | ||
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= | ||
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= | ||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= | ||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= | ||
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ= | ||
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= | ||
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= | ||
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= | ||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= | ||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= | ||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= | ||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= | ||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= | ||
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= | ||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= | ||
github.com/lopezator/migrator v0.2.0 h1:5t2GE77ojbyl9fZ4lHxkfFjwNZvTCzWFMDSorQq5O/c= | ||
github.com/lopezator/migrator v0.2.0/go.mod h1:bpVAVPkWSvTw8ya2Pk7E/KiNAyDWNImgivQY79o8/8I= | ||
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= | ||
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= | ||
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= | ||
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b h1:U/Uqd1232+wrnHOvWNaxrNqn/kFnr4yu4blgPtQt0N8= | ||
gopkg.in/boj/redistore.v1 v1.0.0-20160128113310-fc113767cd6b/go.mod h1:fgfIZMlsafAHpspcks2Bul+MWUNw/2dyQmjC2faKjtg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
// Package handlers contains the handlers for each endpoint of the app | ||
// Package handlers contains all the routes for the API | ||
package handlers | ||
|
||
import ( | ||
"github.com/jmoiron/sqlx" | ||
"gopkg.in/boj/redistore.v1" | ||
"log" | ||
) | ||
// Type Handler contains all the routes as methods. | ||
// This makes it easy to spread client, secrets, etc between your routes. | ||
// In case you need to add one of those said common parts, you just need to add them to your struct definition. | ||
type Handler struct{} | ||
|
||
// Handler is used to expose dependencies to the handlers | ||
type Handler struct { | ||
Db *sqlx.DB | ||
SessionStore *redistore.RediStore | ||
Logger *log.Logger | ||
func NewHandler() *Handler { | ||
return &Handler{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/julienschmidt/httprouter" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// Route Info is used for health checking as well as outputting current app version | ||
func (h Handler) Info() httprouter.Handle { | ||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
|
||
err := writeResponse(w, 200, "version", "0.0.1") | ||
if err != nil { | ||
log.Println(fmt.Errorf("error writing response: %w", err)) | ||
} | ||
} | ||
} |
Oops, something went wrong.