-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (57 loc) · 1.34 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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/femibiwoye/go-test/routes"
"github.com/femibiwoye/go-test/utils"
"github.com/gorilla/handlers"
"github.com/joho/godotenv"
"github.com/rs/cors"
)
type App struct {
Port string
}
func (app *App) Run() error {
_, err := utils.ConnectToDB(os.Getenv("SQL_DATABASE_URL"))
if err != nil {
return errors.New("could not connect to Database")
}
fmt.Println("database connected")
utils.Migrate()
handler := routes.NewHandler()
handler.SetupRoutes()
c := cors.AllowAll()
srv := &http.Server{
Handler: handlers.LoggingHandler(os.Stdout, c.Handler(handler.Router)),
Addr: ":" + app.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Pretest App running on port ", app.Port)
if err := srv.ListenAndServe(); err != nil {
return err
}
return nil
}
func main() {
// load .env file if it exists
err := godotenv.Load(".env")
if err != nil {
fmt.Printf("Error loading .env file: %v", err)
}
fmt.Println("Environment variables successfully loaded. Starting application...")
// get PORT from environment variables
port := os.Getenv("PORT")
if port == "" {
port = "7000"
}
app := App{Port: port}
if err := app.Run(); err != nil {
fmt.Println("Error occur while starting API.")
log.Fatal(err)
}
}