Skip to content

Commit 7ff9d8a

Browse files
ziddah edemziddah edem
authored andcommitted
updated
1 parent 274ed22 commit 7ff9d8a

File tree

13 files changed

+700
-625
lines changed

13 files changed

+700
-625
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ TODO.md
1818
logs.txt
1919
.idea/
2020
secret.md
21-
app.env
21+
app.env
22+
tmp/

cmd/server/main.go

Lines changed: 82 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,82 @@
1-
package main
2-
3-
import (
4-
"context"
5-
"database/sql"
6-
"fmt"
7-
"log"
8-
"net/http"
9-
10-
"github.com/gin-gonic/gin"
11-
"github.com/wpcodevo/golang-postgresql-grpc/config"
12-
"github.com/wpcodevo/golang-postgresql-grpc/controllers"
13-
dbConn "github.com/wpcodevo/golang-postgresql-grpc/db/sqlc"
14-
"github.com/wpcodevo/golang-postgresql-grpc/routes"
15-
16-
_ "github.com/lib/pq"
17-
)
18-
19-
var (
20-
server *gin.Engine
21-
db *dbConn.Queries
22-
ctx context.Context
23-
24-
AuthController controllers.AuthController
25-
AuthRoutes routes.AuthRoutes
26-
)
27-
28-
func init() {
29-
ctx = context.TODO()
30-
config, err := config.LoadConfig(".")
31-
32-
if err != nil {
33-
log.Fatalf("could not load config: %v", err)
34-
}
35-
36-
conn, err := sql.Open(config.PostgreDriver, config.PostgresSource)
37-
if err != nil {
38-
log.Fatalf("could not connect to postgres database: %v", err)
39-
}
40-
41-
db = dbConn.New(conn)
42-
43-
fmt.Println("PostgreSQL connected successfully...")
44-
45-
AuthController = *controllers.NewAuthController(db, ctx)
46-
AuthRoutes = routes.NewAuthRoutes(AuthController, db)
47-
48-
server = gin.Default()
49-
}
50-
51-
func main() {
52-
config, err := config.LoadConfig(".")
53-
54-
if err != nil {
55-
log.Fatalf("could not load config: %v", err)
56-
}
57-
58-
router := server.Group("/api")
59-
60-
router.GET("/healthchecker", func(ctx *gin.Context) {
61-
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": "Welcome to Golang with PostgreSQL"})
62-
})
63-
64-
AuthRoutes.AuthRoute(router)
65-
66-
server.NoRoute(func(ctx *gin.Context) {
67-
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": fmt.Sprintf("Route %s not found", ctx.Request.URL)})
68-
})
69-
log.Fatal(server.Run(":" + config.Port))
70-
}
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
10+
"github.com/gin-contrib/cors"
11+
"github.com/gin-gonic/gin"
12+
"github.com/wpcodevo/golang-postgresql-grpc/config"
13+
"github.com/wpcodevo/golang-postgresql-grpc/controllers"
14+
dbConn "github.com/wpcodevo/golang-postgresql-grpc/db/sqlc"
15+
"github.com/wpcodevo/golang-postgresql-grpc/routes"
16+
17+
_ "github.com/lib/pq"
18+
)
19+
20+
var (
21+
server *gin.Engine
22+
db *dbConn.Queries
23+
ctx context.Context
24+
25+
AuthController controllers.AuthController
26+
UserController controllers.UserController
27+
AuthRoutes routes.AuthRoutes
28+
UserRoutes routes.UserRoutes
29+
)
30+
31+
func init() {
32+
ctx = context.TODO()
33+
config, err := config.LoadConfig(".")
34+
35+
if err != nil {
36+
log.Fatalf("could not load config: %v", err)
37+
}
38+
39+
conn, err := sql.Open(config.PostgreDriver, config.PostgresSource)
40+
if err != nil {
41+
log.Fatalf("could not connect to postgres database: %v", err)
42+
}
43+
44+
db = dbConn.New(conn)
45+
46+
fmt.Println("PostgreSQL connected successfully...")
47+
48+
AuthController = *controllers.NewAuthController(db, ctx)
49+
UserController = controllers.NewUserController(db, ctx)
50+
AuthRoutes = routes.NewAuthRoutes(AuthController, db)
51+
UserRoutes = routes.NewUserRoutes(UserController, db)
52+
53+
server = gin.Default()
54+
}
55+
56+
func main() {
57+
config, err := config.LoadConfig(".")
58+
59+
if err != nil {
60+
log.Fatalf("could not load config: %v", err)
61+
}
62+
63+
corsConfig := cors.DefaultConfig()
64+
corsConfig.AllowOrigins = []string{config.Origin}
65+
corsConfig.AllowCredentials = true
66+
67+
server.Use(cors.New(corsConfig))
68+
69+
router := server.Group("/api")
70+
71+
router.GET("/healthchecker", func(ctx *gin.Context) {
72+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": "Welcome to Golang with PostgreSQL"})
73+
})
74+
75+
AuthRoutes.AuthRoute(router)
76+
UserRoutes.UserRoute(router)
77+
78+
server.NoRoute(func(ctx *gin.Context) {
79+
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": fmt.Sprintf("Route %s not found", ctx.Request.URL)})
80+
})
81+
log.Fatal(server.Run(":" + config.Port))
82+
}

config/default.go

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
package config
2-
3-
import (
4-
"time"
5-
6-
"github.com/spf13/viper"
7-
)
8-
9-
type Config struct {
10-
PostgreDriver string `mapstructure:"POSTGRES_DRIVER"`
11-
PostgresSource string `mapstructure:"POSTGRES_SOURCE"`
12-
13-
Port string `mapstructure:"PORT"`
14-
15-
AccessTokenPrivateKey string `mapstructure:"ACCESS_TOKEN_PRIVATE_KEY"`
16-
AccessTokenPublicKey string `mapstructure:"ACCESS_TOKEN_PUBLIC_KEY"`
17-
RefreshTokenPrivateKey string `mapstructure:"REFRESH_TOKEN_PRIVATE_KEY"`
18-
RefreshTokenPublicKey string `mapstructure:"REFRESH_TOKEN_PUBLIC_KEY"`
19-
AccessTokenExpiresIn time.Duration `mapstructure:"ACCESS_TOKEN_EXPIRED_IN"`
20-
RefreshTokenExpiresIn time.Duration `mapstructure:"REFRESH_TOKEN_EXPIRED_IN"`
21-
AccessTokenMaxAge int `mapstructure:"ACCESS_TOKEN_MAXAGE"`
22-
RefreshTokenMaxAge int `mapstructure:"REFRESH_TOKEN_MAXAGE"`
23-
}
24-
25-
func LoadConfig(path string) (config Config, err error) {
26-
viper.AddConfigPath(path)
27-
viper.SetConfigType("env")
28-
viper.SetConfigName("app")
29-
30-
viper.AutomaticEnv()
31-
32-
err = viper.ReadInConfig()
33-
if err != nil {
34-
return
35-
}
36-
37-
err = viper.Unmarshal(&config)
38-
return
39-
}
1+
package config
2+
3+
import (
4+
"time"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
type Config struct {
10+
PostgreDriver string `mapstructure:"POSTGRES_DRIVER"`
11+
PostgresSource string `mapstructure:"POSTGRES_SOURCE"`
12+
13+
Port string `mapstructure:"PORT"`
14+
15+
Origin string `mapstructure:"ORIGIN"`
16+
17+
AccessTokenPrivateKey string `mapstructure:"ACCESS_TOKEN_PRIVATE_KEY"`
18+
AccessTokenPublicKey string `mapstructure:"ACCESS_TOKEN_PUBLIC_KEY"`
19+
RefreshTokenPrivateKey string `mapstructure:"REFRESH_TOKEN_PRIVATE_KEY"`
20+
RefreshTokenPublicKey string `mapstructure:"REFRESH_TOKEN_PUBLIC_KEY"`
21+
AccessTokenExpiresIn time.Duration `mapstructure:"ACCESS_TOKEN_EXPIRED_IN"`
22+
RefreshTokenExpiresIn time.Duration `mapstructure:"REFRESH_TOKEN_EXPIRED_IN"`
23+
AccessTokenMaxAge int `mapstructure:"ACCESS_TOKEN_MAXAGE"`
24+
RefreshTokenMaxAge int `mapstructure:"REFRESH_TOKEN_MAXAGE"`
25+
}
26+
27+
func LoadConfig(path string) (config Config, err error) {
28+
viper.AddConfigPath(path)
29+
viper.SetConfigType("env")
30+
viper.SetConfigName("app")
31+
32+
viper.AutomaticEnv()
33+
34+
err = viper.ReadInConfig()
35+
if err != nil {
36+
return
37+
}
38+
39+
err = viper.Unmarshal(&config)
40+
return
41+
}

0 commit comments

Comments
 (0)