Skip to content

Commit e59948b

Browse files
ziddah edemziddah edem
authored andcommitted
gin sqlc postgres setup
0 parents  commit e59948b

File tree

20 files changed

+1261
-0
lines changed

20 files changed

+1261
-0
lines changed

.air.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
bin = "./tmp/main"
7+
cmd = "go build -o ./tmp/main ./cmd/server/main.go"
8+
delay = 1000
9+
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
10+
exclude_file = []
11+
exclude_regex = ["_test.go"]
12+
exclude_unchanged = false
13+
follow_symlink = false
14+
full_bin = ""
15+
include_dir = []
16+
include_ext = ["go", "tpl", "tmpl", "html"]
17+
kill_delay = "0s"
18+
log = "build-errors.log"
19+
send_interrupt = false
20+
stop_on_error = true
21+
22+
[color]
23+
app = ""
24+
build = "yellow"
25+
main = "magenta"
26+
runner = "green"
27+
watcher = "cyan"
28+
29+
[log]
30+
time = false
31+
32+
[misc]
33+
clean_on_exit = false
34+
35+
[screen]
36+
clear_on_rebuild = false

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
.DS_Store
17+
TODO.md
18+
logs.txt
19+
.idea/
20+
secret.md
21+
app.env

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# alias sqlc='$(go env GOPATH)/bin/sqlc'
2+
# alias air='$(go env GOPATH)/bin/air'
3+
4+
dev:
5+
docker-compose up -d
6+
7+
dev-down:
8+
docker-compose down
9+
10+
go:
11+
air
12+
13+
migrate:
14+
migrate create -ext sql -dir db/migrations -seq init_schema
15+
16+
migrate-up:
17+
migrate -path db/migrations -database "postgresql://admin:password123@localhost:6500/golang_postgres?sslmode=disable" -verbose up
18+
19+
migrate-down:
20+
migrate -path db/migrations -database "postgresql://admin:password123@localhost:6500/golang_postgres?sslmode=disable" -verbose down
21+
22+
sqlc:
23+
sqlc generate
24+
25+
.PHONY: sqlc

cmd/server/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/wpcodevo/golang-postgresql-grpc/config"
11+
"github.com/wpcodevo/golang-postgresql-grpc/controllers"
12+
dbConn "github.com/wpcodevo/golang-postgresql-grpc/db/sqlc"
13+
"github.com/wpcodevo/golang-postgresql-grpc/routes"
14+
15+
_ "github.com/lib/pq"
16+
)
17+
18+
var (
19+
server *gin.Engine
20+
db *dbConn.Queries
21+
22+
AuthController controllers.AuthController
23+
AuthRoutes routes.AuthRoutes
24+
)
25+
26+
func init() {
27+
config, err := config.LoadConfig(".")
28+
29+
if err != nil {
30+
log.Fatalf("could not load config: %v", err)
31+
}
32+
33+
conn, err := sql.Open(config.PostgreDriver, config.PostgresSource)
34+
if err != nil {
35+
log.Fatalf("could not connect to postgres database: %v", err)
36+
}
37+
38+
db = dbConn.New(conn)
39+
40+
fmt.Println("PostgreSQL connected successfully...")
41+
42+
AuthController = *controllers.NewAuthController(db)
43+
AuthRoutes = routes.NewAuthRoutes(AuthController)
44+
45+
server = gin.Default()
46+
}
47+
48+
func main() {
49+
config, err := config.LoadConfig(".")
50+
51+
if err != nil {
52+
log.Fatalf("could not load config: %v", err)
53+
}
54+
55+
router := server.Group("/api")
56+
57+
router.GET("/healthchecker", func(ctx *gin.Context) {
58+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": "Welcome to Golang with PostgreSQL"})
59+
})
60+
61+
AuthRoutes.AuthRoute(router)
62+
log.Fatal(server.Run(":" + config.Port))
63+
}

config/default.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import (
4+
"github.com/spf13/viper"
5+
)
6+
7+
type Config struct {
8+
PostgreDriver string `mapstructure:"POSTGRES_DRIVER"`
9+
PostgresSource string `mapstructure:"POSTGRES_SOURCE"`
10+
11+
Port string `mapstructure:"PORT"`
12+
}
13+
14+
func LoadConfig(path string) (config Config, err error) {
15+
viper.AddConfigPath(path)
16+
viper.SetConfigType("env")
17+
viper.SetConfigName("app")
18+
19+
viper.AutomaticEnv()
20+
21+
err = viper.ReadInConfig()
22+
if err != nil {
23+
return
24+
}
25+
26+
err = viper.Unmarshal(&config)
27+
return
28+
}

controllers/signup.controller.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/gin-gonic/gin"
8+
db "github.com/wpcodevo/golang-postgresql-grpc/db/sqlc"
9+
"github.com/wpcodevo/golang-postgresql-grpc/utils"
10+
)
11+
12+
type AuthController struct {
13+
db *db.Queries
14+
}
15+
16+
func NewAuthController(db *db.Queries) *AuthController {
17+
return &AuthController{db}
18+
}
19+
20+
func (ac *AuthController) SignUpUser(ctx *gin.Context) {
21+
var credentials *db.User
22+
23+
if err := ctx.ShouldBindJSON(&credentials); err != nil {
24+
ctx.JSON(http.StatusBadRequest, err.Error())
25+
return
26+
}
27+
28+
hashedPassword := utils.HashPassword(credentials.Password)
29+
30+
args := &db.CreateUserParams{
31+
Name: credentials.Name,
32+
Email: credentials.Email,
33+
Password: hashedPassword,
34+
Photo: "default.jpeg",
35+
Verified: true,
36+
Role: "user",
37+
UpdatedAt: time.Now(),
38+
}
39+
40+
user, err := ac.db.CreateUser(ctx, *args)
41+
42+
if err != nil {
43+
ctx.JSON(http.StatusBadGateway, err.Error())
44+
return
45+
}
46+
47+
ctx.JSON(http.StatusCreated, gin.H{"status": "success", "data": gin.H{"user": user}})
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS users;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE "users" (
2+
"id" UUID NOT NULL DEFAULT (uuid_generate_v4()),
3+
"name" VARCHAR NOT NULL,
4+
"email" VARCHAR NOT NULL,
5+
"photo" VARCHAR NOT NULL,
6+
"verified" BOOLEAN NOT NULL,
7+
"password" VARCHAR NOT NULL,
8+
"role" VARCHAR NOT NULL,
9+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
10+
"updated_at" TIMESTAMP(3) NOT NULL,
11+
12+
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
13+
);
14+
15+
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

db/query/user.sql

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
-- name: CreateUser :one
2+
INSERT INTO users (
3+
name,
4+
email,
5+
photo,
6+
verified,
7+
password,
8+
role,
9+
updated_at
10+
) VALUES (
11+
$1, $2, $3, $4, $5, $6, $7
12+
)
13+
RETURNING *;
14+
15+
-- name: GetUser :one
16+
SELECT * FROM users
17+
WHERE id = $1 LIMIT 1;
18+
19+
-- name: ListUsers :many
20+
SELECT * FROM users
21+
ORDER BY id
22+
LIMIT $1
23+
OFFSET $2;
24+
25+
-- name: UpdateUser :one
26+
UPDATE users
27+
set name = $2,
28+
email = $3,
29+
photo = $4,
30+
verified = $5,
31+
password = $6,
32+
role = $7,
33+
updated_at = $8
34+
WHERE id = $1
35+
RETURNING *;
36+
37+
-- name: DeleteUser :exec
38+
DELETE FROM users
39+
WHERE id = $1;

0 commit comments

Comments
 (0)