-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (75 loc) · 2.43 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
package main
import (
"fmt"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
serivcemiddleware "github.com/vrazdalovschi/url-shortener/internal/middleware/service"
"github.com/vrazdalovschi/url-shortener/internal/repository"
"github.com/vrazdalovschi/url-shortener/internal/repository/postgres"
"github.com/vrazdalovschi/url-shortener/internal/router"
"github.com/vrazdalovschi/url-shortener/internal/service"
"log"
"os"
"os/signal"
"syscall"
"net/http"
)
func main() {
fs := flag.NewFlagSetWithEnvPrefix(os.Args[0], "", flag.ExitOnError)
var (
httpAddr = fs.String("HTTP_ADDR", ":8080", "HTTP endpoint to use for endpoints")
dbHost = fs.String("DB_HOST", "localhost", "DB Host")
dbPort = fs.String("DB_PORT", "5432", "DB Port")
dbUser = fs.String("DB_USER", "url-shortener", "DB Username")
dbPassword = fs.String("DB_PASSWORD", "root", "DB Password")
dbName = fs.String("DB_NAME", "shortener", "DB name")
)
_ = fs.Parse(os.Args[1:])
labelNames := []string{"method", "error"}
counterOpts := stdprometheus.CounterOpts{
Namespace: "url_shortener",
Subsystem: "shortener",
Name: "request_count",
Help: "Number of requests received.",
}
summaryOpts := stdprometheus.SummaryOpts{
Namespace: "url_shortener",
Subsystem: "shortener",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}
requestCounter := prometheus.NewCounterVec(counterOpts, labelNames)
requestLatencySummary := prometheus.NewSummaryVec(summaryOpts, labelNames)
prometheus.MustRegister(requestCounter, requestLatencySummary)
configuration := repository.Configuration{
Host: *dbHost,
Port: *dbPort,
User: *dbUser,
Password: *dbPassword,
DbName: *dbName,
}
st, err := postgres.NewRepository(configuration)
if err != nil {
log.Fatal(err)
}
defer st.Close()
var svc service.Service
{
svc = service.NewService(st)
svc = serivcemiddleware.NewLogging()(svc)
svc = serivcemiddleware.NewMetrics(requestCounter, requestLatencySummary)(svc)
}
r := router.New(svc)
errs := make(chan error)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
go func() {
log.Println("transport", "HTTP", "addr", *httpAddr)
errs <- http.ListenAndServe(*httpAddr, r)
}()
log.Fatal("exit", <-errs)
}