Browse Source

Implement additions to API and support attaching sub-subapplications.

multiapp-subsubapp
Benjamin Shelton 3 months ago
parent
commit
67fd1bcae5
1 changed files with 44 additions and 14 deletions
  1. +44
    -14
      application.go

+ 44
- 14
application.go View File

@ -1,7 +1,6 @@
package capstan
import (
"fmt"
"net/http"
"os"
"os/signal"
@ -23,7 +22,7 @@ type application struct {
dependencies *mappers.DependencyMapper
extensions *ExtensionManager
subapps map[string]Application
parent Application
master Application
shutdownHooks []ShutdownHook
proxy ProxyHandler
sync.RWMutex
@ -104,21 +103,29 @@ func New(conf ...*config.Config) *application {
// AttachApplication to the current application instance. This converts the
// current application to use multiapp proxying.
func (a *application) AttachApplication(app Application) {
if a.master == nil {
app.SetMaster(a)
} else {
app.SetMaster(a.master)
}
// If the current proxy isn't a multiapp proxy, we'll fix that and set the
// current proxy as the default handler.
p, ok := a.proxy.(MultiAppProxyHandler)
if !ok {
if a.parent == nil {
fmt.Println("re-attaching proxy to multiapp")
p = NewMultiAppProxy(NewRadixLoader(a.Router().proxy))
a.Router().SetupMuxer()
a.proxy = p
a.Router().SetProxy(p)
app.SetParent(a)
} else {
a.Router().SetProxy(a.parent.Router().proxy)
}
p = NewMultiAppProxy(NewRadixLoader(a.Router().proxy))
a.Router().SetupMuxer()
a.proxy = p
a.Router().SetProxy(p)
}
// Sub-sub-applications must be attached to the master instance.
if a.master != nil {
if pm, ok := a.master.Router().Proxy().(MultiAppProxyHandler); ok {
pm.Loader().AddApplicationHandler(app.Config().Server.CalculatedHostPath(), app.Router().Proxy())
a.master.Application().subapps[app.Config().Server.CalculatedHostPath()] = app
}
}
// TODO: Add options for attaching to the FQDN, hostname only, hostname plus
@ -129,6 +136,29 @@ func (a *application) AttachApplication(app Application) {
//p.Loader().AddApplicationHandler(app.Config().CalculatedPath(), app.Router().proxy)
app.Router().SetupMuxer()
a.subapps[app.Config().Server.CalculatedHostPath()] = app
}
// DestroyApplication disables the specified application by replacing its
// handler with a simple 404 handler. This will persist until it the application
// is restarted.
//
// To full implement this correctly, the underlying radix trie implementation
// must support deleting individual entries.
func (a *application) DestroyApplication(app Application) {
if p, ok := a.proxy.(MultiAppProxyHandler); ok {
p.Loader().AddApplicationHandler(app.Config().Server.CalculatedHostPath(), http.HandlerFunc(http.NotFound))
}
if a.master != nil {
if p, ok := a.master.Router().Proxy().(MultiAppProxyHandler); ok {
p.Loader().AddApplicationHandler(app.Config().Server.CalculatedHostPath(), http.HandlerFunc(http.NotFound))
}
}
}
func (a *application) Application() *application {
return a
}
func (a *application) LoadSession() {
@ -206,8 +236,8 @@ func (a *application) SetDefaultRenderer(renderer render.Renderer) Application {
return a
}
func (a *application) SetParent(app Application) {
a.parent = app
func (a *application) SetMaster(app Application) {
a.master = app
}
func (a *application) RegisterShutdownHook(hook ShutdownHook) {


Loading…
Cancel
Save