-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (60 loc) · 1.67 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
package main
import (
"fmt"
mux "github.com/gorilla/mux"
"log"
"net/http"
"os"
"postgresProject/db"
"postgresProject/db/repository"
"postgresProject/queue"
"postgresProject/service"
"strconv"
)
const (
defaultHost = "localhost"
defaultPort = 8080
hostKey = "HOST"
portKey = "PORT"
)
func main() {
pool, err := db.InitDb()
if err != nil {
log.Printf("Error during init db: %s", err)
os.Exit(-1)
}
defer pool.Close()
rep := &repository.UserRepository{Pool: pool}
queue.InitKafka(rep)
userService := &service.UserService{Repository: rep}
r := mux.NewRouter()
r.HandleFunc("/users/{id:[0-9]+}", userService.GetUser).Methods(http.MethodGet)
r.HandleFunc("/users", userService.GetUsers).Methods(http.MethodGet)
r.HandleFunc("/users", userService.AddUser).Methods(http.MethodPost)
r.HandleFunc("/users/{id:[0-9]+}", userService.UpdateUser).Methods(http.MethodPut)
r.HandleFunc("/users/{id:[0-9]+}", userService.DeleteUser).Methods(http.MethodDelete)
r.HandleFunc("/users", userService.DeleteUsers).Methods(http.MethodDelete)
r.HandleFunc("/queue/users", userService.AddUserViaKafka).Methods(http.MethodPost)
addr := getAddr()
if err = http.ListenAndServe(addr, r); err != nil {
log.Printf("Error during server working: %s", err)
os.Exit(-1)
}
}
func getAddr() string {
port := defaultPort
if v, exists := os.LookupEnv(portKey); exists {
intV, err := strconv.Atoi(v)
if err != nil {
log.Printf("Environment contains incorrect port format: %s\n", v)
log.Printf("Using default port: %v\n", port)
} else {
port = intV
}
}
host := defaultHost
if v, exists := os.LookupEnv(hostKey); exists {
host = v
}
return fmt.Sprintf("%s:%v", host, port)
}