Skip to content

zaferkadi/golang-template

Repository files navigation

Step 1

Install golang-migrate to start creating migration files

brew install golang-migrate

Create the first sql migration file

migrate create -ext sql -dir db/migration -seq init_schema

Add your sql schemas into up.sql and down.sql files inside db/migration folder

Fire up a postgres container

make postgres

Create a database

make createdb

Run the migration files

make migrateup

Step 2 sqlc and generating go functions

brew install sqlc
sqlc init

Make sure you configure sqlc.yaml file

Create a query file that represents a query to a table in query folder e.g: query/authors.sql

-- name: CreateAuthor :execresult
INSERT INTO authors
    (
    name, bio
    )
VALUES
    (
        $1, $2
)
RETURNING *;

Now lets generate our sql-go files

make sqlc

step 3 writing tests

go get github.com/lib/pq

another package testing for logs

go get github.com/stretchr/testify

step 4 github actions

Refer to ci.yml to check how we setup a testing pipeline, with a postgres service, and curling the necessary golang-migration bin.

step 5 load config using Viper

install viper

go get github.com/spf13/viper

create an app.env file and util/config.go to unmarshal the env variables

step 6 create http server

use gin server create APIs

step 7 mock db for testing http api using stubs gomock

install gomock

go install github.com/golang/mock/[email protected]

Make sure mockgen is accessible in cli

vi ~/.zshrc

add the following then save and exit.

export PATH=$PATH:~/go/bin
source ~/.zshrc

We can mark go interfaces we'd like to mock by

//go:generate mockgen -package=mockdb -destination=../mock/mock_store.go . Store

We run the go generate on the current project

go generate -v ./...

step 8 install jwt

go get github.com/dgrijalva/jwt-go

install paseto

github.com/o1egl/paseto

About

Golang template with sqlc

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages