-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (71 loc) · 2.12 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
package main
import (
"log"
"reflect"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/w1png/urlshortener/graphqlHandlers"
"github.com/w1png/urlshortener/handlers"
"github.com/w1png/urlshortener/logger"
"github.com/w1png/urlshortener/middleware"
"github.com/w1png/urlshortener/models"
"github.com/w1png/urlshortener/storage"
"github.com/w1png/urlshortener/utils"
)
func autoMigrate() error {
db := storage.SelectedStorage.(*storage.PostgresStorage).DB
err := db.AutoMigrate(&models.Url{})
if err != nil {
return err
}
return nil
}
func onStartup() error {
err := utils.ConfigInstance.Init()
if err != nil {
return err
}
err = logger.InitLogger()
if err != nil {
return err
}
err = storage.InitSelectedStorage()
if err != nil {
return err
}
err = utils.InitGRPCConnection()
if err != nil {
return err
}
return nil
}
func main() {
err := onStartup()
if err != nil {
log.Fatal(err.Error())
}
if reflect.TypeOf(storage.SelectedStorage) == reflect.TypeOf(&storage.PostgresStorage{}) {
err = autoMigrate()
if err != nil {
logger.LoggerInstance.Fatal(err.Error())
}
}
log.Println("Starting http server on port 8081")
server := NewApiServer(":8081")
server.UseMiddleware(middleware.LoggingMiddleware)
server.UseMiddleware(middleware.PrometheusDurationMiddleware)
server.UseMiddleware(middleware.PrometheusCounterMiddleware)
server.RegisterHandlerFunc("/api/v1/urls", handlers.CreateUrl, "POST")
server.RegisterHandlerFunc("/api/v1/urls/{alias}", handlers.GetUrl, "GET")
server.RegisterHandlerFunc("/metrics", promhttp.Handler().ServeHTTP, "GET")
graphQLHTTPHandler, err := graphqlHandlers.GetHTTPHandler()
if err != nil {
logger.LoggerInstance.Fatal(err.Error())
}
server.RegisterHandlerFunc("/graphql", graphQLHTTPHandler.ServeHTTP, "POST", "GET")
// get url: curl -g 'http://localhost:8081/graphql?query={url(alias:"test"){alias,url}}'
// create url: curl -g 'http://localhost:8081/graphql?query=mutation+_{createUrl(url:"test"){alias,url}}'
err = server.Run()
if err != nil {
logger.LoggerInstance.Fatal(err.Error())
}
}