Skip to content

Commit

Permalink
feat: refine technical urls
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent d13197f commit 05f40d8
Show file tree
Hide file tree
Showing 47 changed files with 410 additions and 402 deletions.
78 changes: 60 additions & 18 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
apilib "github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/health"
"github.com/formancehq/go-libs/httpserver"
"github.com/formancehq/go-libs/otlp"
"github.com/formancehq/ledger/internal/storage/driver"
Expand Down Expand Up @@ -74,28 +76,30 @@ func NewServeCommand() *cobra.Command {
Version: Version,
Debug: service.IsDebug(cmd),
}),
fx.Decorate(func(
params struct {
fx.In

Handler chi.Router
HealthController *health.HealthController

MeterProvider *metric.MeterProvider `optional:"true"`
Exporter *otlpmetrics.InMemoryExporter `optional:"true"`
},
) chi.Router {
return assembleFinalRouter(
otelMetricsExporter == "memory",
enablePProf,
params.MeterProvider,
params.Exporter,
params.HealthController,
params.Handler,
)
}),
fx.Invoke(func(lc fx.Lifecycle, h chi.Router) {
lc.Append(httpserver.NewHook(h, httpserver.WithAddress(serveConfiguration.bind)))
}),
}
if otelMetricsExporter == "memory" || enablePProf {
options = append(options, fx.Decorate(func(
h chi.Router,
meterProvider *metric.MeterProvider,
exporter *otlpmetrics.InMemoryExporter,
) chi.Router {
wrappedRouter := chi.NewRouter()
if otelMetricsExporter == "memory" {
wrappedRouter.Handle("/_metrics", otlpmetrics.NewInMemoryExporterHandler(meterProvider, exporter))
}
if enablePProf {
wrappedRouter.Handle("/debug/pprof/*", http.HandlerFunc(pprof.Index))
}
wrappedRouter.Mount("/", h)

return wrappedRouter
}))
}

return service.New(cmd.OutOrStdout(), options...).Run(cmd)
},
Expand Down Expand Up @@ -135,3 +139,41 @@ func discoverServeConfiguration(cmd *cobra.Command) serveConfiguration {

return ret
}

func assembleFinalRouter(
exportMetrics, exportPProf bool,
meterProvider *metric.MeterProvider,
exporter *otlpmetrics.InMemoryExporter,
healthController *health.HealthController,
handler http.Handler,
) *chi.Mux {
wrappedRouter := chi.NewRouter()
wrappedRouter.Route("/_/", func(r chi.Router) {
if exportMetrics {
r.Handle("/metrics", otlpmetrics.NewInMemoryExporterHandler(
meterProvider,
exporter,
))
}
if exportPProf {
r.Handle("/debug/pprof/*", http.StripPrefix(
"/_",
http.HandlerFunc(pprof.Index),
))
}
r.Handle("/healthcheck", http.HandlerFunc(healthController.Check))
r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
apilib.RawOk(w, struct {
Server string `json:"server"`
Version string `json:"version"`
}{
Server: "ledger",
Version: Version,
})
})
})
wrappedRouter.Get("/_healthcheck", healthController.Check)
wrappedRouter.Mount("/", handler)

return wrappedRouter
}
8 changes: 5 additions & 3 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Base URLs:
|Scope|Scope Description|
|---|---|

<h1 id="ledger-api-ledger-v2">ledger.v2</h1>
<h1 id="ledger-api-ledger">ledger</h1>

## Show server information

Expand All @@ -28,13 +28,13 @@ Base URLs:
> Code samples
```http
GET http://localhost:8080/v2/_info HTTP/1.1
GET http://localhost:8080/_/info HTTP/1.1
Host: localhost:8080
Accept: application/json
```

`GET /v2/_info`
`GET /_/info`

> Example responses
Expand All @@ -60,6 +60,8 @@ To perform this operation, you must be authenticated by means of one of the foll
Authorization ( Scopes: ledger:read )
</aside>

<h1 id="ledger-api-ledger-v2">ledger.v2</h1>

## List ledgers

<a id="opIdv2ListLedgers"></a>
Expand Down
4 changes: 2 additions & 2 deletions internal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import "github.com/formancehq/ledger/internal"
- [func LogTypeFromString\(logType string\) LogType](<#LogTypeFromString>)
- [func \(lt LogType\) MarshalJSON\(\) \(\[\]byte, error\)](<#LogType.MarshalJSON>)
- [func \(lt \*LogType\) Scan\(src interface\{\}\) error](<#LogType.Scan>)
- [func \(l LogType\) String\(\) string](<#LogType.String>)
- [func \(lt LogType\) String\(\) string](<#LogType.String>)
- [func \(lt \*LogType\) UnmarshalJSON\(data \[\]byte\) error](<#LogType.UnmarshalJSON>)
- [func \(lt LogType\) Value\(\) \(driver.Value, error\)](<#LogType.Value>)
- [type Memento](<#Memento>)
Expand Down Expand Up @@ -654,7 +654,7 @@ func (lt *LogType) Scan(src interface{}) error
### func \(LogType\) String

```go
func (l LogType) String() string
func (lt LogType) String() string
```


Expand Down
2 changes: 0 additions & 2 deletions internal/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ func Module(cfg Config) fx.Option {
return fx.Options(
fx.Provide(func(
backend system.Controller,
healthController *health.HealthController,
authenticator auth.Authenticator,
logger logging.Logger,
tracer trace.TracerProvider,
) chi.Router {
return NewRouter(
backend,
healthController,
authenticator,
logger,
"develop",
Expand Down
6 changes: 1 addition & 5 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/go-chi/cors"

"github.com/formancehq/go-libs/auth"
"github.com/formancehq/go-libs/health"
"github.com/formancehq/ledger/internal/api/common"
v1 "github.com/formancehq/ledger/internal/api/v1"
v2 "github.com/formancehq/ledger/internal/api/v2"
Expand All @@ -22,7 +21,6 @@ import (
// todo: refine textual errors
func NewRouter(
systemController system.Controller,
healthController *health.HealthController,
authenticator auth.Authenticator,
logger logging.Logger,
version string,
Expand Down Expand Up @@ -54,12 +52,10 @@ func NewRouter(
}).Handler,
common.LogID(),
)
mux.Get("/_healthcheck", healthController.Check)

v2Router := v2.NewRouter(
systemController,
authenticator,
version,
debug,
v2.WithTracer(routerOptions.tracer),
)
Expand Down Expand Up @@ -92,4 +88,4 @@ func WithTracer(tracer trace.Tracer) RouterOption {

var defaultRouterOptions = []RouterOption{
WithTracer(nooptracer.Tracer{}),
}
}
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_accounts_add_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestAccountsAddMetadata(t *testing.T) {
Return(nil)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodPost, "/", api.Buffer(t, testCase.body))
// httptest.NewRequest check for invalid urls while we want to test invalid urls
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_accounts_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestAccountsCount(t *testing.T) {
Return(10, testCase.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodHead, "/xxx/accounts?pit="+before.Format(time.RFC3339Nano), bytes.NewBufferString(testCase.body))
rec := httptest.NewRecorder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestAccountsDeleteMetadata(t *testing.T) {
Return(tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodDelete, "/", nil)
req.URL.Path = "/ledger0/accounts/" + tc.account + "/metadata/foo"
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_accounts_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestAccountsList(t *testing.T) {
Return(&expectedCursor, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodGet, "/xxx/accounts?pit="+before.Format(time.RFC3339Nano), bytes.NewBufferString(tc.body))
rec := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_accounts_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestAccountsRead(t *testing.T) {
Return(&ledger.Account{}, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodGet, "/", bytes.NewBufferString(tc.body))
req.URL.Path = "/xxx/accounts/" + tc.account
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestBalancesAggregates(t *testing.T) {
GetAggregatedBalances(gomock.Any(), testCase.expectQuery).
Return(expectedBalances, nil)

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodGet, "/xxx/aggregate/balances?pit="+now.Format(time.RFC3339Nano), bytes.NewBufferString(testCase.body))
rec := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestBulk(t *testing.T) {
systemController, ledgerController := newTestingSystemController(t, true)
testCase.expectations(ledgerController)

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodPost, "/xxx/_bulk", bytes.NewBufferString(testCase.body))
rec := httptest.NewRecorder()
Expand Down
22 changes: 0 additions & 22 deletions internal/api/v2/controllers_config.go

This file was deleted.

33 changes: 0 additions & 33 deletions internal/api/v2/controllers_config_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/api/v2/controllers_ledgers_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestLedgersCreate(t *testing.T) {
t.Parallel()

systemController, _ := newTestingSystemController(t, false)
router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

name := uuid.NewString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestLedgersDeleteMetadata(t *testing.T) {
Return(tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodDelete, "/"+name+"/metadata/foo", nil)
req = req.WithContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_ledgers_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestLedgersInfo(t *testing.T) {
t.Parallel()

systemController, ledgerController := newTestingSystemController(t, false)
router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

migrationInfo := []migrations.Info{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_ledgers_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestListLedgers(t *testing.T) {
}, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodGet, "/", nil)
req = req.WithContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_ledgers_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestLedgersRead(t *testing.T) {
t.Parallel()

systemController, _ := newTestingSystemController(t, false)
router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

name := uuid.NewString()
now := time.Now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLedgersUpdateMetadata(t *testing.T) {
UpdateLedgerMetadata(gomock.Any(), name, metadata).
Return(nil)

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodPut, "/"+name+"/metadata", api.Buffer(t, metadata))
req = req.WithContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_logs_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestLogsExport(t *testing.T) {
return nil
})

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

req := httptest.NewRequest(http.MethodPost, "/xxx/logs/export", nil)
rec := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_logs_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestLogsImport(t *testing.T) {
}
})

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
router := NewRouter(systemController, auth.NewNoAuth(), testing.Verbose())

buf := bytes.NewBuffer(nil)
require.NoError(t, json.NewEncoder(buf).Encode(log))
Expand Down
Loading

0 comments on commit 05f40d8

Please sign in to comment.