|
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 | +} |
0 commit comments