-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (47 loc) · 1.13 KB
/
main.go
File metadata and controls
61 lines (47 loc) · 1.13 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
package main
import (
"database/sql"
"fmt"
"gosample/controllers"
"gosample/migrations"
"gosample/redisApi"
"gosample/repository"
"log"
"os"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq"
)
var config Config
func main() {
_, exists := os.LookupEnv("PROD")
if !exists {
err := godotenv.Load("./dev.env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
config.Load()
err := redisApi.Init(config.RedisHost, config.RedisPort, config.RedisPassword, config.RedisDb)
if err != nil {
log.Fatal(err)
}
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", config.DbHost, config.DbPort, config.DbUser, config.DbPassword, config.DbName)
db, err := sql.Open("postgres", psqlconn)
CheckError(err)
defer db.Close()
err = db.Ping()
CheckError(err)
migrations.Migrate(db)
repository.Init(db)
e := echo.New()
e.GET("/", controllers.IndexGetAll)
e.GET("/:id", controllers.IndexGetById)
e.POST("/", controllers.IndexPost)
e.Logger.Fatal(e.Start(":" + config.HTTPPort))
}
func CheckError(err error) {
if err != nil {
log.Fatal(err)
}
}