-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (82 loc) · 2.64 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
96
97
98
99
100
101
102
package main
import (
"context"
"errors"
kitError "github.com/bloock/go-kit/errors"
gin_middleware "github.com/bloock/go-kit/http/middleware/gin"
"github.com/bloock/go-kit/http/presenters"
pinned "github.com/bloock/go-kit/http/versioning"
"github.com/bloock/go-kit/test_utils"
"github.com/gin-gonic/gin"
"github.com/go-chi/chi/v5"
"net/http"
"sync"
"time"
"github.com/bloock/go-kit/client"
"github.com/bloock/go-kit/observability"
"github.com/bloock/go-kit/runtime"
)
func main() {
ctx := context.Background()
l := observability.InitLogger("local", "test_service", "1.0.0", true)
err := observability.InitTracer(ctx, "connection", "dev", "1.0.0", observability.Logger{})
wg := sync.WaitGroup{}
wg.Add(2)
cronClient, err := client.NewCronClient(context.Background(), l)
if err != nil {
panic(err.Error())
}
cronRuntime, err := runtime.NewCronRuntime(cronClient, 5*time.Second, l)
if err != nil {
panic(err.Error())
}
cronRuntime.AddHandler("test", 5*time.Second, CronHandler())
go func() {
defer wg.Done()
cronRuntime.Run(context.Background())
}()
ginClient := client.NewGinEngine("0.0.0.0", 8080, true, l)
chiClient := client.NewChiRouter("0.0.0.0", 8001, true, l)
var vm = &pinned.VersionManager{
Layout: "2006-01-02",
}
ginRuntime, err := runtime.NewVersionedGinRuntime("service-gin", ginClient, 5*time.Second, vm, l)
if err != nil {
panic(err.Error())
}
ginRuntime.SetHandlers(func(e *gin.Engine) {
e.GET("/test", gin_middleware.HandlerVersioning(vm, test_utils.TestHandlerInstance.Versions()), test_utils.TestHandlerInstance.Handler())
})
chiRuntime, err := runtime.NewVersionedChiRuntime("service-chi", chiClient, 5*time.Second, vm, l)
if err != nil {
panic(err.Error())
}
chiRuntime.SetHandlers(ctx, func(r *chi.Mux) {
r.Get("/test", func(writer http.ResponseWriter, request *http.Request) {
presenters.RenderJSON(writer, request, http.StatusOK, "Hello World")
return
})
r.Post("/test", func(writer http.ResponseWriter, request *http.Request) {
presenters.RenderError(writer, request, kitError.ErrInvalidBodyJSON(errors.New("error bad request")))
return
})
r.Patch("/test", func(writer http.ResponseWriter, request *http.Request) {
presenters.RenderError(writer, request, kitError.ErrUnexpected(errors.New("error internal server")))
return
})
})
// Run API server
go func() {
defer wg.Done()
chiRuntime.Run(ctx)
}()
wg.Wait()
}
func CronHandler() client.CronHandler {
return func(c context.Context) error {
ctx := context.Background()
l := observability.InitLogger("local", "test_service", "1.0.0", true)
l.Error(ctx).Str("t", "user").Msg("a cron message")
return nil
}
}