-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
62 lines (49 loc) · 1.28 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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/HamzaRahmani/urlShortner/internal/config"
"github.com/HamzaRahmani/urlShortner/internal/database"
"github.com/HamzaRahmani/urlShortner/internal/manager"
"github.com/HamzaRahmani/urlShortner/internal/server"
)
func main() {
fmt.Printf("Starting URL GO\n")
srv, err := initializeApp()
if err != nil {
log.Fatalf("Failed to initialize app")
return
}
err = srv.Start()
if err != nil {
log.Fatalf("Failed to start server")
return
}
quitChannel := make(chan os.Signal, 1)
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
<-quitChannel
log.Printf("Shutting Down")
defer func() {
srv.Stop()
}()
}
func initializeApp() (*server.HTTPServer, error) {
// "postgresql://postgres:gobank@localhost:5432/postgres"
env := config.Init(os.Environ())
port, _ := env.GetListeningPort()
dbHost, _ := env.GetDatabaseHost()
dbPass, _ := env.GetDatabasePassword()
dbUser, _ := env.GetDatabaseUser()
connString := fmt.Sprintf("postgresql://%s:%s@%s", dbUser, dbPass, dbHost)
db, err := database.NewPostgresStore(connString)
db.Init()
if err != nil {
log.Printf("Failed to connect to db: %s", connString)
}
m := manager.NewManager(db)
srv := server.NewHTTPServer(port, m)
return srv, err
}