forked from japhy-team/backend-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (82 loc) · 2.38 KB
/
main.go
File metadata and controls
102 lines (82 loc) · 2.38 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
package main
import (
"fmt"
"net"
"net/http"
"os"
"time"
charmLog "github.com/charmbracelet/log"
"github.com/gorilla/mux"
"github.com/japhy-tech/backend-test/database_actions"
"github.com/japhy-tech/backend-test/internal/database"
"github.com/japhy-tech/backend-test/internal/server"
_ "github.com/japhy-tech/backend-test/docs"
httpSwagger "github.com/swaggo/http-swagger"
)
const (
ApiPort = "5000"
BreedsFilePath = "database_actions/seeds/breeds.csv"
)
func main() {
logger := charmLog.NewWithOptions(os.Stderr, charmLog.Options{
Formatter: charmLog.TextFormatter,
ReportCaller: true,
ReportTimestamp: true,
TimeFormat: time.Kitchen,
Prefix: "🧑💻 backend-test",
Level: charmLog.DebugLevel,
})
err := validateEnv()
if err != nil {
logger.Fatal(err.Error())
}
db := database.NewMysqlDB(logger)
defer db.Close()
db.SetMaxIdleConns(0)
err = db.Ping()
if err != nil {
logger.Fatal(err.Error())
}
logger.Info("Database connected")
err = database_actions.InitMigrator(db)
if err != nil {
logger.Fatal(err.Error())
}
msg, err := database_actions.RunMigrate("up", 0)
if err != nil {
logger.Fatal(err.Error())
} else {
logger.Info(msg)
}
// Loading data into the pets table
nbRowsAffected, err := database_actions.LoadPetsTable(db, BreedsFilePath)
if err != nil {
logger.Fatal(fmt.Sprintf("Unable to load pets table %s", err.Error()))
}
if nbRowsAffected > 0 {
logger.Info(fmt.Sprintf("%d lines were successfully loaded into the pets table", nbRowsAffected))
}
app := server.NewApp(logger, db)
r := mux.NewRouter()
app.RegisterRoutes(r.PathPrefix("/v1").Subrouter())
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}).Methods(http.MethodGet)
r.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
err = http.ListenAndServe(
net.JoinHostPort("", ApiPort),
r,
)
if err != nil {
logger.Fatal(fmt.Sprintf("Unable to start service %s", err.Error()))
}
// =============================== Starting Msg ===============================
logger.Info(fmt.Sprintf("Service started and listen on port %s", ApiPort))
}
// validateEnv checks that all the environment variables required to run the app are set.
func validateEnv() error {
if os.Getenv("MYSQL_ROOT_PASSWORD") == "" {
return fmt.Errorf("MYSQL_ROOT_PASSWORD is not set")
}
return nil
}