-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (77 loc) · 2.01 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
_ "github.com/joho/godotenv/autoload"
"github.com/RisingStack/almandite-user-service/internal/config"
"github.com/RisingStack/almandite-user-service/internal/dal"
"github.com/RisingStack/almandite-user-service/internal/handlers"
"github.com/RisingStack/almandite-user-service/internal/handlers/middleware"
"github.com/RisingStack/almandite-user-service/internal/handlers/middleware/authentication"
)
const DefaultHTTPAddr = ":0"
var httpAddr string
func init() {
flag.StringVar(&httpAddr, "addr", DefaultHTTPAddr, "Set the HTTP bind address")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
listener, err := net.Listen("tcp", httpAddr)
if err != nil {
log.Fatal(err)
}
tcpAddr := listener.Addr().(*net.TCPAddr)
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
db := dal.NewDAL()
if err := db.OpenConnection(
config.GetConfiguration().PostgresURL,
config.GetConfiguration().DebugSQL,
); err != nil {
log.Fatal("Failed to open DB conenction", err)
}
go func() {
select {
case <-signalCh:
log.Println("Signal received, shutting down...")
db.CloseConnection()
os.Exit(0)
}
}()
log.Printf("Open the following URL in the browser: http://%s:%d\n", convertIPtoString(tcpAddr.IP), tcpAddr.Port)
authStore := authentication.AuthStore{
UserRepository: db.Users(),
}
http.HandleFunc("/healthcheck",
middleware.Chain(
middleware.Timer,
middleware.Logger,
)(handlers.Healthcheck))
http.HandleFunc("/alma",
middleware.Chain(
middleware.Timer,
middleware.Logger,
authStore.Basic,
)(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "korte")
},
),
)
if err := http.Serve(listener, nil); err != nil {
log.Fatal(err)
}
}
func convertIPtoString(ip net.IP) string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}