Skip to content

Commit

Permalink
Merge pull request #3 from GenerateNU/db-setup
Browse files Browse the repository at this point in the history
Db setup
  • Loading branch information
ddusichka authored Aug 21, 2024
2 parents 27c0241 + 0c6b9e4 commit 9a1a01c
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Sample .env file - fill in the values from Supabase
DB_HOST=""
DB_USER=""
DB_PASSWORD=""
DB_PORT=""
DB_NAME="postgres"
2 changes: 2 additions & 0 deletions .github/workflows/backend-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "1.22"

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
Expand Down
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ server
# Test binary, built with `go test -c`
*.test

# Autogenerated files
# /database/sqlc

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

Expand Down
34 changes: 34 additions & 0 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
package main

import (
"context"
"database/sql"
"fmt"
"log"
"os"
sqlc "platnm/database/sqlc"

"platnm/routes"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/joho/godotenv"
)

func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}

app := fiber.New()

// Middleware
app.Use(logger.New())

host := os.Getenv("DB_HOST")
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
port := os.Getenv("DB_PORT")
name := os.Getenv("DB_NAME")

connStr := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=require", host, user, password, name, port)
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal("Failed to initialize the database: ", err)
}

queries := sqlc.New(db)
ctx := context.Background()

// list all authors
testData, err := queries.GetTestData(ctx)
if err != nil {
fmt.Println(err)
}
log.Println(testData)

// Routes
routes.HelloRoutes(app)

Expand Down
10 changes: 10 additions & 0 deletions backend/database/migrations/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE albums (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE TABLE test (
id number PRIMARY KEY,
created_at timestamp
);
5 changes: 5 additions & 0 deletions backend/database/queries/queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- name: GetAlbums :many
select * from albums;

-- name: GetTestData :many
select * from test;
31 changes: 31 additions & 0 deletions backend/database/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions backend/database/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions backend/database/sqlc/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module platnm

go 1.22.6

require github.com/gofiber/fiber/v2 v2.52.5
require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
Expand All @@ -15,5 +19,5 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)
8 changes: 6 additions & 2 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yG
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
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-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand All @@ -23,5 +27,5 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
10 changes: 10 additions & 0 deletions backend/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
queries: "/database/queries"
schema: "/database/migrations"
gen:
go:
package: "database"
out: "/database/sqlc"
sql_package: "database/sql"
8 changes: 8 additions & 0 deletions backend/taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ tasks:
summary: Watch for changes to the backend server and rebuild. (Make sure watchexec is installed)
cmds:
- watchexec -r -e go -- task run
generate:
summary: Generates auto-gen code (sqlc)
cmds:
- sqlc generate
build:
deps:
- generate
summary: Build backend executables
sources:
- ./**/*.go
Expand All @@ -30,6 +36,8 @@ tasks:
cmds:
- go test ./...
lint:
deps:
- generate
summary: Run lint checks
cmds:
- golangci-lint run ./...
Expand Down

0 comments on commit 9a1a01c

Please sign in to comment.