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

Commit7ff9d8a

Browse files
ziddah edemziddah edem
ziddah edem
authored and
ziddah edem
committed
updated
1 parent274ed22 commit7ff9d8a

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-
funcinit() {
29-
ctx=context.TODO()
30-
config,err:=config.LoadConfig(".")
31-
32-
iferr!=nil {
33-
log.Fatalf("could not load config: %v",err)
34-
}
35-
36-
conn,err:=sql.Open(config.PostgreDriver,config.PostgresSource)
37-
iferr!=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-
funcmain() {
52-
config,err:=config.LoadConfig(".")
53-
54-
iferr!=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+
funcinit() {
32+
ctx=context.TODO()
33+
config,err:=config.LoadConfig(".")
34+
35+
iferr!=nil {
36+
log.Fatalf("could not load config: %v",err)
37+
}
38+
39+
conn,err:=sql.Open(config.PostgreDriver,config.PostgresSource)
40+
iferr!=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+
funcmain() {
57+
config,err:=config.LoadConfig(".")
58+
59+
iferr!=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-
typeConfigstruct {
10-
PostgreDriverstring`mapstructure:"POSTGRES_DRIVER"`
11-
PostgresSourcestring`mapstructure:"POSTGRES_SOURCE"`
12-
13-
Portstring`mapstructure:"PORT"`
14-
15-
AccessTokenPrivateKeystring`mapstructure:"ACCESS_TOKEN_PRIVATE_KEY"`
16-
AccessTokenPublicKeystring`mapstructure:"ACCESS_TOKEN_PUBLIC_KEY"`
17-
RefreshTokenPrivateKeystring`mapstructure:"REFRESH_TOKEN_PRIVATE_KEY"`
18-
RefreshTokenPublicKeystring`mapstructure:"REFRESH_TOKEN_PUBLIC_KEY"`
19-
AccessTokenExpiresIn time.Duration`mapstructure:"ACCESS_TOKEN_EXPIRED_IN"`
20-
RefreshTokenExpiresIn time.Duration`mapstructure:"REFRESH_TOKEN_EXPIRED_IN"`
21-
AccessTokenMaxAgeint`mapstructure:"ACCESS_TOKEN_MAXAGE"`
22-
RefreshTokenMaxAgeint`mapstructure:"REFRESH_TOKEN_MAXAGE"`
23-
}
24-
25-
funcLoadConfig(pathstring) (configConfig,errerror) {
26-
viper.AddConfigPath(path)
27-
viper.SetConfigType("env")
28-
viper.SetConfigName("app")
29-
30-
viper.AutomaticEnv()
31-
32-
err=viper.ReadInConfig()
33-
iferr!=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+
typeConfigstruct {
10+
PostgreDriverstring`mapstructure:"POSTGRES_DRIVER"`
11+
PostgresSourcestring`mapstructure:"POSTGRES_SOURCE"`
12+
13+
Portstring`mapstructure:"PORT"`
14+
15+
Originstring`mapstructure:"ORIGIN"`
16+
17+
AccessTokenPrivateKeystring`mapstructure:"ACCESS_TOKEN_PRIVATE_KEY"`
18+
AccessTokenPublicKeystring`mapstructure:"ACCESS_TOKEN_PUBLIC_KEY"`
19+
RefreshTokenPrivateKeystring`mapstructure:"REFRESH_TOKEN_PRIVATE_KEY"`
20+
RefreshTokenPublicKeystring`mapstructure:"REFRESH_TOKEN_PUBLIC_KEY"`
21+
AccessTokenExpiresIn time.Duration`mapstructure:"ACCESS_TOKEN_EXPIRED_IN"`
22+
RefreshTokenExpiresIn time.Duration`mapstructure:"REFRESH_TOKEN_EXPIRED_IN"`
23+
AccessTokenMaxAgeint`mapstructure:"ACCESS_TOKEN_MAXAGE"`
24+
RefreshTokenMaxAgeint`mapstructure:"REFRESH_TOKEN_MAXAGE"`
25+
}
26+
27+
funcLoadConfig(pathstring) (configConfig,errerror) {
28+
viper.AddConfigPath(path)
29+
viper.SetConfigType("env")
30+
viper.SetConfigName("app")
31+
32+
viper.AutomaticEnv()
33+
34+
err=viper.ReadInConfig()
35+
iferr!=nil {
36+
return
37+
}
38+
39+
err=viper.Unmarshal(&config)
40+
return
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp