Skip to content

Commit 9da615e

Browse files
ziddah edemziddah edem
authored andcommitted
updated
1 parent e8b71b5 commit 9da615e

File tree

14 files changed

+1150
-1150
lines changed

14 files changed

+1150
-1150
lines changed

.air.toml

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +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
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: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +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
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
2121
app.env

cmd/server/main.go

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +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-
}
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 & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +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-
}
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 & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +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-
}
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+
}

0 commit comments

Comments
 (0)