-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
58 lines (51 loc) · 1.04 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
package main
import (
"fmt"
"jwtauthwithgo/api"
"jwtauthwithgo/auth"
"jwtauthwithgo/repository/psql"
"jwtauthwithgo/routers"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
const (
host = "localhost"
port = 5432
user = "dipto"
password = "12345"
dbname = "auth"
)
func main() {
repo := chooseDb("postgres")
service := auth.NewAuthService(repo)
handler := api.NewHandler(service)
r := routers.InitRoutes(handler)
errs := make(chan error, 2)
go func() {
fmt.Println("Listening on port 8000")
errs <- http.ListenAndServe(":8000", r)
}()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()
fmt.Printf("Terminated %s", <-errs)
}
func chooseDb(dbType string) auth.Repository{
switch dbType {
case "postgres":
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
repo, err := psql.NewPsqlRepository(psqlInfo)
if err != nil{
log.Fatal(err)
}
return repo
}
return nil
}