NewAPI creates a new Api instance with the provided LedgerForge service and sets up the router. Parameters: - b: The LedgerForge service used to interact with business logic. Returns: - *Api: A new instance of the Api with the configured router.
(b *ledgerforge.LedgerForge)
| 151 | // Returns: |
| 152 | // - *Api: A new instance of the Api with the configured router. |
| 153 | func NewAPI(b *ledgerforge.LedgerForge) *Api { |
| 154 | gin.SetMode(gin.ReleaseMode) |
| 155 | conf, err := config.Fetch() |
| 156 | if err != nil { |
| 157 | return nil |
| 158 | } |
| 159 | r := gin.Default() |
| 160 | auth := middleware.NewAuthMiddleware(b) |
| 161 | r.Use(middleware.RateLimitMiddleware(conf)) |
| 162 | r.Use(middleware.SecurityHeaders()) |
| 163 | r.Use(otelgin.Middleware("LEDGERFORGE", |
| 164 | otelgin.WithFilter(func(r *http.Request) bool { |
| 165 | // Exclude high-frequency operational endpoints from tracing |
| 166 | // to avoid polluting the trace feed with noise. |
| 167 | return r.URL.Path != "/metrics" |
| 168 | }), |
| 169 | )) |
| 170 | |
| 171 | r.GET("/", func(c *gin.Context) { |
| 172 | c.JSON(200, "server running...") |
| 173 | }) |
| 174 | |
| 175 | return &Api{ledgerforge: b, router: r, auth: auth} |
| 176 | } |
| 177 | |
| 178 | // Search performs a search query on a specified collection. |
| 179 | // It binds the incoming JSON request to a SearchCollectionParams object, |