-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (44 loc) · 1.16 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/alabarjasteh/url-shortener/config"
"github.com/alabarjasteh/url-shortener/mysqldb"
"github.com/alabarjasteh/url-shortener/rediscache"
urlshortenersvc "github.com/alabarjasteh/url-shortener/urlshortener"
"github.com/go-kit/log"
)
func main() {
configPath := "./config/config"
cfgFile, err := config.Load(configPath)
if err != nil {
panic(err)
}
cfg, err := config.ParseConfig(cfgFile)
if err != nil {
panic(err)
}
file, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
var logger log.Logger
{
logger = log.NewLogfmtLogger(file)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
mysql := mysqldb.New(&cfg.Mysql)
redis := rediscache.New(&cfg.Redis)
var svc urlshortenersvc.Shortener
{
svc = urlshortenersvc.NewShortenerService(redis, mysql)
svc = urlshortenersvc.LoggingMiddleware(logger)(svc)
}
var h http.Handler
{
h = urlshortenersvc.MakeHTTPHandler(svc, log.With(logger, "component", "HTTP"), mysql, redis)
}
http.ListenAndServe(fmt.Sprintf(":%s", cfg.Server.Port), h)
}