Initial commit

This commit is contained in:
brajkovic 2023-06-16 07:42:50 +00:00
commit bb4f465321
14 changed files with 403 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea/**
template
.env

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# go-web-app-template
Basic template for creating go applications
Includes:
* dotenv
* pq
* sqlx
* chi
* google.uuid
To start using make sure to setup either environment variables listed
below, or create .env file and copy variables below
```
PSQL_HOST=localhost
PSQL_PORT=5432
PSQL_USER=template
PSQL_PASSWORD=templatePassword
PSQL_DB=template
```
Also, database is required for template to start, so you can start it with `docker compose up -d`

39
db.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"os"
)
func envMustExist(env string) string {
if value, exists := os.LookupEnv(env); !exists {
panic(fmt.Sprintf("env variable '%s' not defined", env))
} else {
return value
}
}
func connectToDb() (*sqlx.DB, error) {
host := envMustExist("PSQL_HOST")
port := envMustExist("PSQL_PORT")
user := envMustExist("PSQL_USER")
password := envMustExist("PSQL_PASSWORD")
dbname := envMustExist("PSQL_DB")
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sqlx.Open("postgres", psqlInfo)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}

0
db/dev/v1_0.sql Normal file
View File

1
db/prod/v1_0.sql Normal file
View File

@ -0,0 +1 @@
package prod

28
docker-compose-deploy.yml Normal file
View File

@ -0,0 +1,28 @@
version: '3.1'
services:
backend:
image: registry.bbr-dev.info/template/backend:latest
restart: on-failure
depends_on:
- database
ports:
- "5281:5281"
networks:
- template
env_file:
- .env.docker
database:
image: postgres:14
ports:
- "5432:5432"
environment:
- POSTGRES_USER=template
- POSTGRES_PASSWORD=templatePassword
- POSTGRES_DB=template
networks:
- template
networks:
template:

11
docker-compose.yml Normal file
View File

@ -0,0 +1,11 @@
version: '3.1'
services:
database:
image: postgres:14
ports:
- "5432:5432"
environment:
- POSTGRES_USER=template
- POSTGRES_PASSWORD=templatePassword
- POSTGRES_DB=template

19
dockerfile Normal file
View File

@ -0,0 +1,19 @@
### STAGE 1.a: Prepare certs ###
FROM alpine:latest as certs
RUN apk --update add ca-certificates
### STAGE 1.b: Build ###
FROM golang as go-build
WORKDIR /root
COPY go.mod go.sum ./
# install packages
RUN go mod download
COPY . .
ENV CGO_ENABLED=0
RUN go build -tags timetzdata template
### Stage 2: Run ###
FROM scratch
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=go-build /root/template /usr/bin/template
ENTRYPOINT ["template"]

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module template
go 1.19
require (
github.com/go-chi/chi v1.5.4
github.com/jmoiron/sqlx v1.3.5
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/google/uuid v1.3.0 // indirect
)

15
go.sum Normal file
View File

@ -0,0 +1,15 @@
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
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-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=

49
main.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"embed"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/joho/godotenv"
"log"
"net/http"
"template/migration"
)
//go:embed db/dev/*.sql
var devMigrations embed.FS
func init() {
godotenv.Load()
log.SetPrefix("")
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func main() {
client, err := connectToDb()
if err != nil {
log.Fatalf("couldn't connect to db: %v", err)
}
if err := migration.InitializeMigrations(client, devMigrations); err != nil {
log.Fatalf("couldn't execute migrations: %v", err)
}
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", helloWorld)
log.Fatal(http.ListenAndServe(":5281", r))
}
func helloWorld(w http.ResponseWriter, r *http.Request) {
response := struct {
Title string `json:"title"`
Message string `json:"message"`
}{Title: "hello world", Message: "this is an example of response"}
render(w, r, http.StatusOK, response)
}

33
makefile Normal file
View File

@ -0,0 +1,33 @@
# scripts for building app
# requires go 1.19+ and git installed
VERSION := 0.0.1
serve:
go run ./...
setup:
go get
docker-dev:
docker image build -t registry.bbr-dev.info/template/backend:$(VERSION)-dev .
docker tag registry.bbr-dev.info/template/backend:$(VERSION)-dev registry.bbr-dev.info/template/backend:latest-dev
docker image push registry.bbr-dev.info/template/backend:$(VERSION)-dev
docker image push registry.bbr-dev.info/template/backend:latest-dev
docker-prod:
docker image build -t registry.bbr-dev.info/template/backend:$(VERSION) .
docker tag registry.bbr-dev.info/template/backend:$(VERSION) registry.bbr-dev.info/template/backend:latest
docker image push registry.bbr-dev.info/template/backend:$(VERSION)
docker image push registry.bbr-dev.info/template/backend:latest
release:
git tag $(VERSION)
git push origin $(VERSION)
test:
go test ./...
clean:
rm -rf template

151
migration/migration.go Normal file
View File

@ -0,0 +1,151 @@
package migration
import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/jmoiron/sqlx"
"io/fs"
"log"
"sort"
"strings"
"time"
)
const migrationsTable = `
CREATE TABLE IF NOT EXISTS migration (
filename varchar,
hash varchar,
created timestamp,
should_validate boolean
);`
const fetchMigrationsTable = `SELECT * FROM "migration";`
const createMigration = `INSERT INTO "migration" (filename, hash, created, should_validate) VALUES ($1, $2, current_timestamp, TRUE);`
type Migration struct {
Filename string `db:"filename"`
Hash string `db:"hash"`
Created time.Time `db:"created"`
ShouldValidate bool `db:"should_validate"`
}
func InitializeMigrations(db *sqlx.DB, migrationFiles fs.FS) error {
// create table if it doesn't exist already
if _, err := db.Exec(migrationsTable); err != nil {
return err
}
// fetch current migrations
var entries []Migration
if err := db.Select(&entries, fetchMigrationsTable); err != nil {
return err
}
migrations := map[string]Migration{}
for _, entry := range entries {
migrations[entry.Filename] = entry
}
return validateMigrations(db, migrations, migrationFiles)
}
type version struct {
major int
minor int
}
func validateMigrations(db *sqlx.DB, migrations map[string]Migration, migrationFiles fs.FS) error {
scripts := map[string]string{}
var scriptNames []string
// fetch scripts
err := fs.WalkDir(migrationFiles, ".", func(path string, d fs.DirEntry, outerErr error) error {
if !d.IsDir() {
if bytearr, err := fs.ReadFile(migrationFiles, path); err == nil {
scripts[d.Name()] = strings.TrimSpace(string(bytearr))
scriptNames = append(scriptNames, d.Name())
}
}
return nil
})
if err != nil {
return err
}
// sort scripts by version
sort.Slice(scriptNames, func(i, j int) bool {
return isLessThan(parseVersion(scriptNames[i]), parseVersion(scriptNames[j]))
})
for _, name := range scriptNames {
if _, exists := migrations[name]; exists {
if err := validateMigration(name, migrations[name], scripts[name]); err != nil {
return err
}
} else {
if err := executeMigration(db, name, scripts[name]); err != nil {
return err
}
}
}
return nil
}
func executeMigration(db *sqlx.DB, name string, script string) error {
log.Printf("[INFO] script='%s' | migrations - executing", name)
tx := db.MustBeginTx(context.Background(), nil)
var err error = nil
if _, e := tx.Exec(script); e != nil {
err = e
}
if _, e := tx.Exec(createMigration, name, hash(script)); e != nil {
err = e
}
if err != nil {
log.Printf("[ERROR] script='%s' | migrations - failed executing", name)
tx.Rollback()
} else {
log.Printf("[INFO] script='%s' | migrations - succesfully executed", name)
tx.Commit()
}
return err
}
func validateMigration(name string, migration Migration, script string) error {
if !migration.ShouldValidate {
return nil
}
calculatedHash := hash(script)
if calculatedHash != migration.Hash {
err := fmt.Sprintf("migrations - mismatch in hash for %s (expected '%s', calculated '%s')", name, migration.Hash, calculatedHash)
log.Printf("[ERROR] script='%s' err='%s' | migrations - failed executing", script, err)
return fmt.Errorf("migrations - mismatch in hashes for %s", name)
}
return nil
}
func hash(script string) string {
hash := sha256.New()
hash.Write([]byte(script))
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
}
func isLessThan(v1 version, v2 version) bool {
if v1.major > v2.major {
return false
}
if v1.major == v2.major && v1.minor > v2.minor {
return false
}
return true
}
func parseVersion(filename string) version {
ver := version{}
if _, err := fmt.Sscanf(filename, "v%d_%d", &ver.major, &ver.minor); err != nil {
panic(err)
}
return ver
}

21
render.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"encoding/json"
"log"
"net/http"
)
func render[T any](w http.ResponseWriter, r *http.Request, status int, response T) error {
if body, err := json.MarshalIndent(response, "", " "); err == nil {
w.Header().Add("content-type", "application/json")
w.WriteHeader(status)
_, err = w.Write(body)
return err
} else {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("couldn't parse response")
_, err = w.Write([]byte{})
return err
}
}