Skip to content

Commit

Permalink
chore: remove v0-version of appspacerouter and related types
Browse files Browse the repository at this point in the history
- rename domain.V0AppRoute and others
- merge AppspaceRouter and V0AppspaceRouter
- rename appspacerouter.V0AppRoutes to AppRoutes
- tweak ds-dev frontend with changed route hit event  field names
  • Loading branch information
teleclimber committed May 16, 2024
1 parent 0e63d4e commit bb9f3ef
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 498 deletions.
52 changes: 23 additions & 29 deletions cmd/ds-dev/ds-dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func main() {

devSandboxRunsModel := &DevSandboxRunsModel{}

v0AppRoutes := &appspacerouter.V0AppRoutes{
AppRoutes := &appspacerouter.AppRoutes{
AppModel: devAppModel,
AppFilesModel: devAppFilesModel,
Config: runtimeConfig,
Expand All @@ -178,7 +178,7 @@ func main() {
AppFilesModel: devAppFilesModel,
AppLocation2Path: appLocation2Path,
AppModel: devAppModel,
V0AppRoutes: v0AppRoutes,
AppRoutes: AppRoutes,
AppLogger: appLogger,
}
appGetter.Init()
Expand Down Expand Up @@ -319,32 +319,26 @@ func main() {
sandboxProxy := &sandboxproxy.SandboxProxy{
SandboxManager: devSandboxManager}

appspaceRouterV0 := &appspacerouter.V0{
dropserverRoutes := &appspacerouter.DropserverRoutes{
V0DropServerRoutes: &appspacerouter.V0DropserverRoutes{
AppspaceModel: devAppspaceModel,
Authenticator: devAuth,
},
}

appspaceRouter := &appspacerouter.AppspaceRouter{
Authenticator: devAuth,
AppModel: devAppModel,
AppspaceModel: devAppspaceModel,
AppspaceStatus: appspaceStatus,
DropserverRoutes: dropserverRoutes,
AppspaceUserModel: appspaceUserModel,
V0AppRoutes: v0AppRoutes,
AppRoutes: AppRoutes,
SandboxProxy: sandboxProxy,
Authenticator: devAuth,
RouteHitEvents: routeHitEvents,
Config: runtimeConfig,
AppLocation2Path: appLocation2Path,
AppspaceLocation2Path: appspaceLocation2Path}
appspaceRouterV0.Init()

v0dropserverRoutes := &appspacerouter.V0DropserverRoutes{
AppspaceModel: devAppspaceModel,
Authenticator: devAuth,
}
dropserverRoutes := &appspacerouter.DropserverRoutes{
V0DropServerRoutes: v0dropserverRoutes,
}

appspaceRouter := &appspacerouter.AppspaceRouter{
Authenticator: devAuth,
AppModel: devAppModel,
AppspaceModel: devAppspaceModel,
AppspaceStatus: appspaceStatus,
V0AppspaceRouter: appspaceRouterV0,
DropserverRoutes: dropserverRoutes,
AppspaceLocation2Path: appspaceLocation2Path,
}
appspaceRouter.Init()
appspaceStatus.AppspaceRouter = appspaceRouter
Expand Down Expand Up @@ -372,14 +366,14 @@ func main() {
AppVersionEvents: appVersionEvents,
}
userService := &UserService{
DevAuthenticator: devAuth,
AppspaceUsersModelV0: appspaceUserModel,
Avatars: avatars,
AppspaceFilesEvents: appspaceFilesEvents}
DevAuthenticator: devAuth,
AppspaceUsersModel: appspaceUserModel,
Avatars: avatars,
AppspaceFilesEvents: appspaceFilesEvents}

routeHitService := &RouteHitService{
RouteHitEvents: routeHitEvents,
AppspaceUsersModelV0: appspaceUserModel}
RouteHitEvents: routeHitEvents,
AppspaceUsersModel: appspaceUserModel}

migrationJobTwine := &twineservices.MigrationJobService{
AppspaceModel: devAppspaceModel,
Expand Down
24 changes: 12 additions & 12 deletions cmd/ds-dev/routehitservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type RequestJSON struct {
Method string `json:"method"`
}
type RouteHitEventJSON struct {
Timestamp time.Time `json:"timestamp"`
Request RequestJSON `json:"request"`
V0RouteConfig *domain.V0AppRoute `json:"v0_route_config"` // this might be nil.OK?
User *domain.AppspaceUser `json:"user"` //make nil OK
Authorized bool `json:"authorized"`
Status int `json:"status"`
Timestamp time.Time `json:"timestamp"`
Request RequestJSON `json:"request"`
RouteConfig *domain.AppRoute `json:"route_config"` // this might be nil.OK?
User *domain.AppspaceUser `json:"user"` //make nil OK
Authorized bool `json:"authorized"`
Status int `json:"status"`
}

// RouteHitService forwards route hit events to provided twine instance
Expand All @@ -30,7 +30,7 @@ type RouteHitService struct {
Subscribe(ch chan<- *domain.AppspaceRouteHitEvent)
Unsubscribe(ch chan<- *domain.AppspaceRouteHitEvent)
} `checkinject:"required"`
AppspaceUsersModelV0 interface {
AppspaceUsersModel interface {
Get(appspaceID domain.AppspaceID, proxyID domain.ProxyID) (domain.AppspaceUser, error)
} `checkinject:"required"`
}
Expand Down Expand Up @@ -63,15 +63,15 @@ func (s *RouteHitService) sendRouteEvent(twine *twine.Twine, routeEvent *domain.
Request: RequestJSON{
URL: routeEvent.Request.URL.String(),
Method: routeEvent.Request.Method},
V0RouteConfig: routeEvent.V0RouteConfig,
Authorized: routeEvent.Authorized,
Status: routeEvent.Status}
RouteConfig: routeEvent.RouteConfig,
Authorized: routeEvent.Authorized,
Status: routeEvent.Status}

if routeEvent.Credentials.ProxyID != "" {
user, err := s.AppspaceUsersModelV0.Get(appspaceID, routeEvent.Credentials.ProxyID)
user, err := s.AppspaceUsersModel.Get(appspaceID, routeEvent.Credentials.ProxyID)
if err != nil {
// very possible the user is no loger in DB if appspace data was changed externally (re-imported for ex)
fmt.Println("sendRouteEvent s.AppspaceUsersModelV0.Get() Error: " + err.Error())
fmt.Println("sendRouteEvent s.AppspaceUsersModel.Get() Error: " + err.Error())
} else {
send.User = &user
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/ds-dev/userservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
// UserService is a twine service that sets the desired user params
// and keeps the frontend up to date with app's declared permissions
type UserService struct {
DevAuthenticator *DevAuthenticator `checkinject:"required"`
AppspaceUsersModelV0 interface {
DevAuthenticator *DevAuthenticator `checkinject:"required"`
AppspaceUsersModel interface {
Get(appspaceID domain.AppspaceID, proxyID domain.ProxyID) (domain.AppspaceUser, error)
GetAll(appspaceID domain.AppspaceID) ([]domain.AppspaceUser, error)
Create(appspaceID domain.AppspaceID, authType string, authID string) (domain.ProxyID, error)
Expand Down Expand Up @@ -85,7 +85,7 @@ const (
)

func (u *UserService) sendUsers(twine *twine.Twine) {
users, err := u.AppspaceUsersModelV0.GetAll(appspaceID)
users, err := u.AppspaceUsersModel.GetAll(appspaceID)
if err != nil {
fmt.Println("sendUsers error getting users: " + err.Error())
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (u *UserService) handleUserCreateMessage(m twine.ReceivedMessageI) {
}

u.dummyDropidNum++
proxyID, err := u.AppspaceUsersModelV0.Create(appspaceID, "dropid", fmt.Sprintf("dropid.dummy.develop/%v", u.dummyDropidNum))
proxyID, err := u.AppspaceUsersModel.Create(appspaceID, "dropid", fmt.Sprintf("dropid.dummy.develop/%v", u.dummyDropidNum))
if err != nil {
m.SendError(err.Error())
panic(err)
Expand All @@ -169,7 +169,7 @@ func (u *UserService) handleUserCreateMessage(m twine.ReceivedMessageI) {
}
}

err = u.AppspaceUsersModelV0.UpdateMeta(appspaceID, proxyID, incomingUser.DisplayName, avatar, incomingUser.Permissions)
err = u.AppspaceUsersModel.UpdateMeta(appspaceID, proxyID, incomingUser.DisplayName, avatar, incomingUser.Permissions)
if err != nil {
m.SendError(err.Error())
panic(err)
Expand All @@ -192,7 +192,7 @@ func (u *UserService) handleUserUpdateMessage(m twine.ReceivedMessageI) {
}

avatar := ""
user, err := u.AppspaceUsersModelV0.Get(appspaceID, incomingUser.ProxyID)
user, err := u.AppspaceUsersModel.Get(appspaceID, incomingUser.ProxyID)
if err != nil {
m.SendError(err.Error())
panic(err)
Expand Down Expand Up @@ -220,7 +220,7 @@ func (u *UserService) handleUserUpdateMessage(m twine.ReceivedMessageI) {
}
}

err = u.AppspaceUsersModelV0.UpdateMeta(appspaceID, incomingUser.ProxyID, incomingUser.DisplayName, avatar, incomingUser.Permissions)
err = u.AppspaceUsersModel.UpdateMeta(appspaceID, incomingUser.ProxyID, incomingUser.DisplayName, avatar, incomingUser.Permissions)
if err != nil {
m.SendError(err.Error())
panic(err)
Expand All @@ -237,7 +237,7 @@ func (u *UserService) handleUserUpdateMessage(m twine.ReceivedMessageI) {
func (u *UserService) handleUserDeleteMessage(m twine.ReceivedMessageI) {
proxyID := domain.ProxyID(string(m.Payload()))

user, err := u.AppspaceUsersModelV0.Get(appspaceID, proxyID)
user, err := u.AppspaceUsersModel.Get(appspaceID, proxyID)
if err != nil {
m.SendError(err.Error())
panic(err)
Expand All @@ -251,7 +251,7 @@ func (u *UserService) handleUserDeleteMessage(m twine.ReceivedMessageI) {
}
}

err = u.AppspaceUsersModelV0.Delete(appspaceID, proxyID)
err = u.AppspaceUsersModel.Delete(appspaceID, proxyID)
if err != nil {
m.SendError(err.Error())
panic(err)
Expand Down
10 changes: 5 additions & 5 deletions cmd/ds-host/appops/appgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ type AppGetter struct {
SandboxManager interface {
ForApp(appVersion *domain.AppVersion) (domain.SandboxI, error)
} `checkinject:"required"`
V0AppRoutes interface {
ValidateRoutes(routes []domain.V0AppRoute) error
AppRoutes interface {
ValidateRoutes(routes []domain.AppRoute) error
} `checkinject:"required"`

keysMux sync.Mutex
Expand Down Expand Up @@ -531,7 +531,7 @@ func (g *AppGetter) getDataFromSandbox(keyData appGetData) error {
return err
}

err = g.V0AppRoutes.ValidateRoutes(routesData) // pass meta, assume err is internal / fatal.
err = g.AppRoutes.ValidateRoutes(routesData) // pass meta, assume err is internal / fatal.
if err != nil {
return err
}
Expand Down Expand Up @@ -594,7 +594,7 @@ func (g *AppGetter) getMigrations(keyData appGetData, s domain.SandboxI) error {
}

// Note this is a versioned API
func (g *AppGetter) getRoutes(s domain.SandboxI) ([]domain.V0AppRoute, error) {
func (g *AppGetter) getRoutes(s domain.SandboxI) ([]domain.AppRoute, error) {
sent, err := s.SendMessage(domain.SandboxAppService, 11, nil)
if err != nil {
g.getLogger("getRoutes, s.SendMessage").Error(err)
Expand All @@ -610,7 +610,7 @@ func (g *AppGetter) getRoutes(s domain.SandboxI) ([]domain.V0AppRoute, error) {

// Should also verify that the response is command 11?

var routes []domain.V0AppRoute
var routes []domain.AppRoute

err = json.Unmarshal(reply.Payload(), &routes)
if err != nil {
Expand Down
41 changes: 28 additions & 13 deletions cmd/ds-host/appspacerouter/appspacerouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (

// AppspaceRouter handles routes for appspaces.
type AppspaceRouter struct {
Config *domain.RuntimeConfig `checkinject:"required"`
Authenticator interface {
AppspaceUserProxyID(http.Handler) http.Handler
SetForAppspace(http.ResponseWriter, domain.ProxyID, domain.AppspaceID, string) (string, error)
} `checkinject:"required"`
AppModel interface {
GetFromID(domain.AppID) (domain.App, error)
Expand All @@ -28,13 +30,32 @@ type AppspaceRouter struct {
AppspaceModel interface {
GetFromDomain(string) (*domain.Appspace, error)
} `checkinject:"required"`
AppspaceUserModel interface {
Get(appspaceID domain.AppspaceID, proxyID domain.ProxyID) (domain.AppspaceUser, error)
} `checkinject:"required"`
AppspaceStatus interface {
Ready(domain.AppspaceID) bool
} `checkinject:"required"`
V0TokenManager interface {
CheckToken(appspaceID domain.AppspaceID, token string) (domain.V0AppspaceLoginToken, bool)
} `checkinject:"required"`
DropserverRoutes interface {
Router() http.Handler
} `checkinject:"required"`
V0AppspaceRouter http.Handler `checkinject:"required"`
AppRoutes interface {
Match(appID domain.AppID, version domain.Version, method string, reqPath string) (domain.AppRoute, error)
} `checkinject:"required"`
SandboxProxy http.Handler `checkinject:"required"` // versioned?
RouteHitEvents interface {
Send(*domain.AppspaceRouteHitEvent)
} `checkinject:"optional"`
AppLocation2Path interface {
Files(string) string
} `checkinject:"required"`
AppspaceLocation2Path interface {
Files(string) string
Avatars(string) string
} `checkinject:"required"`

liveCounterMux sync.Mutex
liveCounter map[domain.AppspaceID]int
Expand All @@ -45,7 +66,7 @@ type AppspaceRouter struct {
mux *chi.Mux
}

// Init initializes data structures
// Init initializes the router
func (a *AppspaceRouter) Init() {
a.liveCounter = make(map[domain.AppspaceID]int)
a.subscribers = make(map[domain.AppspaceID][]chan<- int)
Expand All @@ -56,14 +77,13 @@ func (a *AppspaceRouter) Init() {
mux.Use(a.errorPage)
mux.Use(a.appspaceAvailable, a.countRequest)
mux.Use(a.loadApp)
// Not sure we need all these middlewares for all routes.
// - dropserver routes like get login token do not need appspace user, and may not care if available or to count request?
// -> actually may need available + count because there may be side-effects to appspace, like recording last used or whatever
// - also does not need app and ap version

// first match dropserver routes
mux.Mount("/.dropserver", a.DropserverRoutes.Router())
mux.Handle("/*", http.HandlerFunc(a.branchToVersionedRouters))
mux.Route("/", func(r chi.Router) {
r.Use(a.securityHeaders)
r.Use(a.loadRouteConfig, a.routeHit, a.processLoginToken, a.loadAppspaceUser, a.authorizeRoute)
r.Handle("/*", http.HandlerFunc(a.handleRoute))
})

a.mux = mux
}
Expand Down Expand Up @@ -151,11 +171,6 @@ func (a *AppspaceRouter) loadApp(next http.Handler) http.Handler {
})
}

func (a *AppspaceRouter) branchToVersionedRouters(w http.ResponseWriter, r *http.Request) {
// Here eventually we will branch off to different versions of appspace routers.
a.V0AppspaceRouter.ServeHTTP(w, r)
}

// errorPage shows an HTML error page for certian statuses
func (a *AppspaceRouter) errorPage(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading

0 comments on commit bb9f3ef

Please sign in to comment.