Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite59948b

Browse files
ziddah edemziddah edem
ziddah edem
authored and
ziddah edem
committed
gin sqlc postgres setup
0 parents  commite59948b

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+
funcinit() {
27+
config,err:=config.LoadConfig(".")
28+
29+
iferr!=nil {
30+
log.Fatalf("could not load config: %v",err)
31+
}
32+
33+
conn,err:=sql.Open(config.PostgreDriver,config.PostgresSource)
34+
iferr!=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+
funcmain() {
49+
config,err:=config.LoadConfig(".")
50+
51+
iferr!=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+
typeConfigstruct {
8+
PostgreDriverstring`mapstructure:"POSTGRES_DRIVER"`
9+
PostgresSourcestring`mapstructure:"POSTGRES_SOURCE"`
10+
11+
Portstring`mapstructure:"PORT"`
12+
}
13+
14+
funcLoadConfig(pathstring) (configConfig,errerror) {
15+
viper.AddConfigPath(path)
16+
viper.SetConfigType("env")
17+
viper.SetConfigName("app")
18+
19+
viper.AutomaticEnv()
20+
21+
err=viper.ReadInConfig()
22+
iferr!=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+
typeAuthControllerstruct {
13+
db*db.Queries
14+
}
15+
16+
funcNewAuthController(db*db.Queries)*AuthController {
17+
return&AuthController{db}
18+
}
19+
20+
func (ac*AuthController)SignUpUser(ctx*gin.Context) {
21+
varcredentials*db.User
22+
23+
iferr:=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+
iferr!=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+
DROPTABLE IF EXISTS users;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATETABLE "users" (
2+
"id" UUIDNOT NULL DEFAULT (uuid_generate_v4()),
3+
"name"VARCHARNOT NULL,
4+
"email"VARCHARNOT NULL,
5+
"photo"VARCHARNOT NULL,
6+
"verified"BOOLEANNOT NULL,
7+
"password"VARCHARNOT NULL,
8+
"role"VARCHARNOT NULL,
9+
"created_at"TIMESTAMP(3)NOT NULL DEFAULTCURRENT_TIMESTAMP,
10+
"updated_at"TIMESTAMP(3)NOT NULL,
11+
12+
CONSTRAINT"users_pkey"PRIMARY KEY ("id")
13+
);
14+
15+
CREATEUNIQUE 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= $1LIMIT1;
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+
DELETEFROM users
39+
WHERE id= $1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp