-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathapi.go
More file actions
70 lines (62 loc) · 2.45 KB
/
api.go
File metadata and controls
70 lines (62 loc) · 2.45 KB
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
package router
import (
"fmt"
"opencsg.com/csghub-server/builder/instrumentation"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"opencsg.com/csghub-server/accounting/handler"
"opencsg.com/csghub-server/api/middleware"
bldmq "opencsg.com/csghub-server/builder/mq"
"opencsg.com/csghub-server/common/config"
"opencsg.com/csghub-server/mq"
)
func NewAccountRouter(config *config.Config, mqHandler mq.MessageQueue, mqFactory bldmq.MessageQueueFactory) (*gin.Engine, error) {
r := gin.New()
middleware.SetInfraMiddleware(r, config, instrumentation.Account)
needAPIKey := middleware.NeedAPIKey(config)
//add router for golang pprof
debugGroup := r.Group("/debug", needAPIKey)
pprof.RouteRegister(debugGroup, "pprof")
r.Use(middleware.Authenticator(config))
err := createCustomValidator()
if err != nil {
return nil, fmt.Errorf("error create validator, error: %w", err)
}
apiGroup := r.Group("/api/v1/accounting")
// metering
meterHandler, err := handler.NewMeteringHandler()
if err != nil {
return nil, fmt.Errorf("error creating multi sync handler, error: %w", err)
}
createMeteringRoutes(apiGroup, meterHandler)
err = createAdvancedRoutes(apiGroup, config, mqHandler, mqFactory)
if err != nil {
return nil, fmt.Errorf("error creating accounting advanced routes, error:%w", err)
}
return r, nil
}
func createMeteringRoutes(apiGroup *gin.RouterGroup, meterHandler *handler.MeteringHandler) {
meterGroup := apiGroup.Group("/metering")
{
meterGroup.GET("/:id/statements", meterHandler.QueryMeteringStatementByUserID)
meterGroup.GET("/stat", meterHandler.QueryMeteringStatByDate)
}
}
func createCustomValidator() error {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
if err := v.RegisterValidation("optional_date_format", OptionalDateFormat); err != nil {
return fmt.Errorf("fail to register custom validation functions:%w", err)
}
if err := v.RegisterValidation("year_month_format", OptionalYearMonthFormat); err != nil {
return fmt.Errorf("fail to register custom validation functions:%w", err)
}
if err := v.RegisterValidation("phone_format", OptionalPhoneFormat); err != nil {
return fmt.Errorf("fail to register custom validation functions:%w", err)
}
return nil // Return nil if no error occurred during registration of custom validation functions
} else {
return fmt.Errorf("fail to register custom validation functions")
}
}