/
server.go
109 lines (91 loc) · 3.34 KB
/
server.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
103
104
105
106
107
108
109
package http_chi
import (
"context"
"github.com/batazor/shortlink/pkg/http/server"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/render"
"github.com/riandyrn/otelchi"
"github.com/spf13/viper"
"go.opentelemetry.io/otel/trace"
"golang.org/x/text/message"
"google.golang.org/protobuf/encoding/protojson"
_ "github.com/batazor/shortlink/internal/pkg/i18n"
"github.com/batazor/shortlink/internal/pkg/logger"
cqrs_api "github.com/batazor/shortlink/internal/services/api/application/http-chi/controllers/cqrs"
link_api "github.com/batazor/shortlink/internal/services/api/application/http-chi/controllers/link"
sitemap_api "github.com/batazor/shortlink/internal/services/api/application/http-chi/controllers/sitemap"
link_cqrs "github.com/batazor/shortlink/internal/services/link/infrastructure/rpc/cqrs/link/v1"
link_rpc "github.com/batazor/shortlink/internal/services/link/infrastructure/rpc/link/v1"
sitemap_rpc "github.com/batazor/shortlink/internal/services/link/infrastructure/rpc/sitemap/v1"
"github.com/batazor/shortlink/pkg/http/handler"
additionalMiddleware "github.com/batazor/shortlink/pkg/http/middleware"
)
// Run HTTP-server
// @title Shortlink API
// @version 1.0
// @description Shortlink API
// @termsOfService http://swagger.io/terms/
//
// @contact.name Shortlink repository
// @contact.url https://github.com/batazor/shortlink/issues
// @contact.email support@swagger.io
//
// @license.name MIT
// @license.url http://www.opensource.org/licenses/MIT
//
// @host localhost:7070
// @BasePath /api
// @schemes http https
func (api *API) Run(
ctx context.Context,
i18n *message.Printer,
config http_server.Config,
log logger.Logger,
tracer *trace.TracerProvider,
// Delivery
link_rpc link_rpc.LinkServiceClient,
link_command link_cqrs.LinkCommandServiceClient,
link_query link_cqrs.LinkQueryServiceClient,
sitemap_rpc sitemap_rpc.SitemapServiceClient,
) error {
api.ctx = ctx
api.jsonpb = protojson.MarshalOptions{
UseProtoNames: true,
}
log.Info("Run HTTP-CHI API")
r := chi.NewRouter()
// CORS
corsPolicy := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // nolint:gomnd
// Debug: true,
})
r.Use(corsPolicy.Handler)
r.Use(render.SetContentType(render.ContentTypeJSON))
// A good base middleware stack
r.Use(middleware.RealIP)
r.Use(middleware.Heartbeat("/healthz"))
r.Use(middleware.Recoverer)
// Set a timeout value on the request context (ctx), that will signal
// through ctx.Done() that the request has timed out and further
// processing should be stopped.
r.Use(middleware.Timeout(config.Timeout))
// Additional middleware
r.Use(otelchi.Middleware(viper.GetString("SERVICE_NAME")))
r.Use(additionalMiddleware.Logger(log))
r.NotFound(handler.NotFoundHandler)
r.Mount("/api/links", link_api.Routes(link_rpc))
r.Mount("/api/cqrs", cqrs_api.Routes(link_command, link_query))
r.Mount("/api/sitemap", sitemap_api.Routes(sitemap_rpc))
srv := http_server.New(ctx, r, config)
// start HTTP-server
log.Info(i18n.Sprintf("API run on port %d", config.Port))
err := srv.ListenAndServe()
return err
}