-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
230 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,21 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/formancehq/auth/pkg/api/routing" | ||
sharedhealth "github.com/numary/go-libs/sharedhealth/pkg" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func Module(addr string, baseUrl *url.URL) fx.Option { | ||
return fx.Options( | ||
sharedhealth.Module(), | ||
sharedhealth.ProvideHealthCheck(delegatedOIDCServerAvailability), | ||
fx.Provide(func(healthController *sharedhealth.HealthController) *mux.Router { | ||
return NewRouter(baseUrl, healthController) | ||
}), | ||
routing.Module(addr, baseUrl), | ||
fx.Invoke( | ||
addClientRoutes, | ||
addScopeRoutes, | ||
addUserRoutes, | ||
fx.Annotate(addClientRoutes, fx.ParamTags(``, `name:"prefixedRouter"`)), | ||
fx.Annotate(addScopeRoutes, fx.ParamTags(``, `name:"prefixedRouter"`)), | ||
fx.Annotate(addUserRoutes, fx.ParamTags(``, `name:"prefixedRouter"`)), | ||
), | ||
fx.Invoke(func(lc fx.Lifecycle, router *mux.Router) { | ||
lc.Append(fx.Hook{ | ||
OnStart: func(ctx context.Context) error { | ||
return StartServer(addr, router) | ||
}, | ||
}) | ||
}), | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package routing | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
var contextKey = struct{}{} | ||
|
||
type contextHolder struct { | ||
port int | ||
} | ||
|
||
func NewContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, contextKey, &contextHolder{}) | ||
} | ||
|
||
func setPort(ctx context.Context, port int) { | ||
v := ctx.Value(contextKey) | ||
if v == nil { | ||
return | ||
} | ||
v.(*contextHolder).port = port | ||
} | ||
|
||
func ListeningPort(ctx context.Context) int { | ||
v := ctx.Value(contextKey) | ||
if v == nil { | ||
return 0 | ||
} | ||
return v.(*contextHolder).port | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package routing | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/gorilla/mux" | ||
sharedhealth "github.com/numary/go-libs/sharedhealth/pkg" | ||
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func CreateRootRouter(baseUrl *url.URL, healthController *sharedhealth.HealthController) (*mux.Router, *mux.Router) { | ||
rootRouter := mux.NewRouter() | ||
rootRouter.Use(otelmux.Middleware("auth")) | ||
rootRouter.Use(func(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
handler.ServeHTTP(w, r) | ||
}) | ||
}) | ||
rootRouter.Path("/_healthcheck").HandlerFunc(healthController.Check) | ||
|
||
subRouter := rootRouter | ||
if baseUrl.Path != "/" { | ||
subRouter = subRouter.PathPrefix(baseUrl.Path).Subrouter() | ||
subRouter.Path("/_healthcheck").HandlerFunc(healthController.Check) | ||
} | ||
|
||
return rootRouter, subRouter | ||
} | ||
|
||
func Module(addr string, baseUrl *url.URL) fx.Option { | ||
return fx.Options( | ||
sharedhealth.Module(), | ||
fx.Provide(fx.Annotate(func(healthController *sharedhealth.HealthController) (*mux.Router, *mux.Router) { | ||
return CreateRootRouter(baseUrl, healthController) | ||
}, fx.ResultTags(`name:"rootRouter"`, `name:"prefixedRouter"`))), | ||
fx.Invoke(fx.Annotate(func(lc fx.Lifecycle, router *mux.Router, healthController *sharedhealth.HealthController) { | ||
lc.Append(fx.Hook{ | ||
OnStart: func(ctx context.Context) error { | ||
return StartServer(ctx, addr, router) | ||
}, | ||
}) | ||
}, fx.ParamTags(``, `name:"rootRouter"`))), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package routing | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func testWithUrl(t *testing.T, urlStr string) { | ||
u, err := url.Parse(urlStr) | ||
require.NoError(t, err) | ||
|
||
ctx := NewContext(context.Background()) | ||
|
||
app := fx.New( | ||
fx.Invoke(fx.Annotate(func(router *mux.Router) { | ||
router.Path("/subpath-with-prefix").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
}, fx.ParamTags(`name:"prefixedRouter"`))), | ||
fx.Invoke(fx.Annotate(func(router *mux.Router) { | ||
router.Path("/subpath-to-root").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
}, fx.ParamTags(`name:"rootRouter"`))), | ||
Module(":0", u), | ||
fx.NopLogger, | ||
) | ||
|
||
require.NoError(t, app.Start(ctx)) | ||
defer func() { | ||
require.NoError(t, app.Stop(ctx)) | ||
}() | ||
|
||
serverUrl := fmt.Sprintf("http://localhost:%d", ListeningPort(ctx)) | ||
serverUrlWithPath := fmt.Sprintf("%s%s", serverUrl, u.Path) | ||
|
||
rsp, err := http.Get(fmt.Sprintf("%s/_healthcheck", serverUrlWithPath)) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, rsp.StatusCode) | ||
|
||
rsp, err = http.Get(fmt.Sprintf("%s/_healthcheck", serverUrl)) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, rsp.StatusCode) | ||
|
||
rsp, err = http.Get(fmt.Sprintf("%s/subpath-with-prefix", serverUrlWithPath)) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusNoContent, rsp.StatusCode) | ||
|
||
rsp, err = http.Get(fmt.Sprintf("%s/subpath-to-root", serverUrl)) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusNoContent, rsp.StatusCode) | ||
} | ||
|
||
func TestModule(t *testing.T) { | ||
testWithUrl(t, "http://localhost") | ||
testWithUrl(t, "http://localhost/any/sub/path") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package routing | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
func StartServer(ctx context.Context, addr string, handler http.Handler) error { | ||
socket, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tcpAddr, err := net.ResolveTCPAddr(socket.Addr().Network(), socket.Addr().String()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
setPort(ctx, tcpAddr.Port) | ||
|
||
go func() { | ||
err := http.Serve(socket, handler) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.