Files
sonr/app/gateway/context/middleware.go
T

65 lines
1.6 KiB
Go
Raw Normal View History

2024-12-22 17:01:11 -05:00
package context
import (
gocontext "context"
"net/http"
"github.com/labstack/echo/v4"
"github.com/medama-io/go-useragent"
2024-12-24 11:10:20 -05:00
"github.com/onsonr/sonr/internal/crypto/mpc"
"github.com/onsonr/sonr/internal/common"
2024-12-22 17:01:11 -05:00
"github.com/onsonr/sonr/internal/config/hway"
hwayorm "github.com/onsonr/sonr/internal/database/hwayorm"
)
type GatewayContext struct {
echo.Context
2024-12-24 10:38:17 -05:00
hwayorm.Querier
2024-12-22 17:01:11 -05:00
id string
ipfsClient common.IPFS
2024-12-24 10:38:17 -05:00
agent useragent.UserAgent
2024-12-22 17:01:11 -05:00
tokenStore common.IPFSTokenStore
stagedEnclaves map[string]mpc.Enclave
grpcAddr string
turnstileSiteKey string
}
func GetGateway(c echo.Context) (*GatewayContext, error) {
cc, ok := c.(*GatewayContext)
if !ok {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Gateway Context not found")
}
return cc, nil
}
func UseGateway(env hway.Hway, ipc common.IPFS, db *hwayorm.Queries) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ua := useragent.NewParser()
ctx := &GatewayContext{
Context: c,
2024-12-24 10:38:17 -05:00
Querier: db,
2024-12-22 17:01:11 -05:00
ipfsClient: ipc,
2024-12-24 10:38:17 -05:00
agent: ua.Parse(c.Request().UserAgent()),
2024-12-22 17:01:11 -05:00
grpcAddr: env.GetSonrGrpcUrl(),
tokenStore: common.NewUCANStore(ipc),
2024-12-24 10:38:17 -05:00
turnstileSiteKey: env.GetTurnstileSiteKey(),
2024-12-22 17:01:11 -05:00
}
return next(ctx)
}
}
}
func BG() gocontext.Context {
ctx := gocontext.Background()
return ctx
}
func (cc *GatewayContext) ReadCookie(k common.CookieKey) string {
return common.ReadCookieUnsafe(cc.Context, k)
}
func (cc *GatewayContext) WriteCookie(k common.CookieKey, v string) {
common.WriteCookie(cc.Context, k, v)
}