-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
108 lines (92 loc) · 2.13 KB
/
main.go
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
package main
import (
"busha/database/postgres"
"busha/database/redis"
"busha/handlers"
"busha/models"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
"time"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
port, exist := os.LookupEnv("PORT")
if !exist {
port = "8080"
}
_, route := Setup()
srv := &http.Server{
Addr: ":" + port,
WriteTimeout: time.Second * 60,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: route,
}
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}
func Setup() (*handlers.Handler, *mux.Router) {
dbUser, exist := os.LookupEnv("POSTGRES_USER")
if !exist {
log.Fatal("POSTGRES_USER not set in .env")
}
dbPass, exist := os.LookupEnv("POSTGRES_PASSWORD")
if !exist {
log.Fatal("POSTGRES_PASSWORD not set in .env")
}
dbHost, exist := os.LookupEnv("POSTGRES_HOST")
if !exist {
log.Fatal("POSTGRES_HOST not set in .env")
}
dbName, exist := os.LookupEnv("POSTGRES_DB")
if !exist {
log.Fatal("POSTGRES_DB not set in .env")
}
dbPort, exist := os.LookupEnv("POSTGRES_PORT")
if !exist {
log.Fatal("POSTGRES_PORT not set in .env")
}
sslMode, exist := os.LookupEnv("POSTGRES_SSLMode")
if !exist {
sslMode = "disable"
}
db, err := postgres.New(&postgres.Config{
User: dbUser,
Password: dbPass,
DBName: dbName,
Host: dbHost,
Port: dbPort,
SSLMode: sslMode,
})
if err != nil {
log.Fatal("Failed to connect to Postgres database", err)
}
//Migrate table(s)
if err = postgres.SetupDatabase(db, &models.Comments{}); err != nil {
log.Fatal("Failed To setup tables", err)
}
redisAddress, exist := os.LookupEnv("REDIS_ADDRESS")
if !exist {
log.Fatal("REDIS_ADDRESS not set in .env")
}
redisPassword, exist := os.LookupEnv("REDIS_PASSWORD")
//if !exist {
// log.Fatal("REDIS_PASSWORD not set in .env")
//}
//Initiate Redis
redisDB := redis.New(&redis.Config{
Addr: redisAddress,
Password: redisPassword,
})
//Initiate Routes
route := mux.NewRouter()
h := handlers.New(db, redisDB)
h.AddRoutes(route)
return h, route
}