Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (112 loc) · 3.76 KB
/
Copy pathmain.go
File metadata and controls
134 lines (112 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"crypto/rand"
"fmt"
_frontendHttpDelivery "github.com/cooljar/go-postgres-fiber/frontend/delivery/http"
"github.com/cooljar/go-postgres-fiber/frontend/delivery/http/configs"
_frontendDeliveryMiddleware "github.com/cooljar/go-postgres-fiber/frontend/delivery/http/middleware"
_frontendRepo "github.com/cooljar/go-postgres-fiber/frontend/repository/pgsql"
_frontendUcase "github.com/cooljar/go-postgres-fiber/frontend/usecase"
"github.com/cooljar/go-postgres-fiber/utils"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v4/pgxpool"
"io"
"os"
"time"
// docs are generated by Swag CLI, you have to import them.
_ "github.com/cooljar/go-postgres-fiber/docs" // load API Docs files (Swagger)
)
var dbConn *pgxpool.Pool
var err error
func init() {
if os.Getenv("SERVER_URL") == "" {
exitf("SERVER_URL env is required")
}
if os.Getenv("SERVER_READ_TIMEOUT") == "" {
exitf("SERVER_READ_TIMEOUT env is required")
}
if os.Getenv("JWT_SECRET_KEY") == "" {
exitf("JWT_SECRET_KEY env is required")
}
if os.Getenv("JWT_SECRET_KEY_EXPIRE_MINUTES") == "" {
exitf("JWT_SECRET_KEY_EXPIRE_MINUTES env is required")
}
if os.Getenv("DB_SERVER_URL") == "" {
exitf("DB_SERVER_URL env is required")
}
if os.Getenv("SMTP_HOST") == "" {
exitf("SMTP_HOST env is required")
}
if os.Getenv("SMTP_PORT") == "" {
exitf("SMTP_PORT env is required")
}
if os.Getenv("SMTP_SENDER_NAME") == "" {
exitf("SMTP_SENDER_NAME env is required")
}
if os.Getenv("SMTP_AUTH_EMAIL") == "" {
exitf("SMTP_AUTH_EMAIL env is required")
}
if os.Getenv("SMTP_AUTH_PASSWORD") == "" {
exitf("SMTP_AUTH_PASSWORD env is required")
}
// Adapted from https://elithrar.github.io/article/generating-secure-random-numbers-crypto-rand/
assertAvailablePRNG()
}
// assertAvailablePRNG Assert that a cryptographically secure PRNG is available.
// For generation of secure random strings
func assertAvailablePRNG() {
buf := make([]byte, 1)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
exitf("crypto/rand is unavailable: Read() failed with", err)
}
}
// @title Go Rest API
// @version 1.0
// @description Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.email lifelinejar@mail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @BasePath /api
func main() {
dbConn, err = pgxpool.Connect(context.Background(), os.Getenv("DB_SERVER_URL"))
if err != nil {
exitf("Unable to connect to database: %v\n", err)
}
defer dbConn.Close()
// Define a validator
validator := utils.NewValidator()
// Define Fiber config.
config := configs.FiberConfig()
app := fiber.New(config)
// Swagger handler
_frontendHttpDelivery.NewSwaggerHandler(app)
middL := _frontendDeliveryMiddleware.InitMiddleware(app)
app.Use(middL.CORS())
app.Use(middL.LOGGER())
// router for public access
rPublic := app.Group("/api/v1")
// router for private access
rPrivate := app.Group("/api/v1/auth", middL.JWT())
timeoutContext := time.Duration(2) * time.Second
bookRepo := _frontendRepo.NewPgsqlBookRepository(dbConn)
bookUsecae := _frontendUcase.NewBookUsecase(bookRepo, timeoutContext)
_frontendHttpDelivery.NewBookHandler(app, bookUsecae, rPublic, rPrivate)
userRepo := _frontendRepo.NewPgsqlUserRepository(dbConn)
userUsecase := _frontendUcase.NewUserUsecase(userRepo, timeoutContext)
_frontendHttpDelivery.NewUserHandler(app, validator, userUsecase, rPublic, rPrivate)
utils.StartServer(app)
}
func exitf(s string, args ...interface{}) {
errorf(s, args...)
os.Exit(1)
}
func errorf(s string, args ...interface{}) {
fmt.Fprintf(os.Stderr, s+"\n", args...)
}