pool: Reddo component-like fields
This commit is contained in:
parent
99983ee6db
commit
337a5fbeba
48 changed files with 291 additions and 163 deletions
|
|
@ -15,9 +15,10 @@ import (
|
|||
// Control represents the running control server.
|
||||
type Control struct {
|
||||
component.Base
|
||||
|
||||
Servables []component.Servable
|
||||
Cronables []component.Cronable
|
||||
Dependencies struct {
|
||||
Servables []component.Servable
|
||||
Cronables []component.Cronable
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
|||
|
|
@ -16,8 +16,9 @@ import (
|
|||
|
||||
type Cron struct {
|
||||
component.Base
|
||||
|
||||
Tasks []component.Cronable
|
||||
Dependencies struct {
|
||||
Tasks []component.Cronable
|
||||
}
|
||||
}
|
||||
|
||||
// Listen returns a channel that listens for triggers in the current process.
|
||||
|
|
@ -51,11 +52,11 @@ func (control *Cron) Listen(ctx context.Context) (<-chan struct{}, func()) {
|
|||
// Once should not be called concurrently with Cron.
|
||||
func (control *Cron) Once(ctx context.Context) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(control.Tasks))
|
||||
wg.Add(len(control.Dependencies.Tasks))
|
||||
|
||||
zerolog.Ctx(ctx).Info().Time("time", time.Now()).Msg("Starting Cron")
|
||||
|
||||
for _, task := range control.Tasks {
|
||||
for _, task := range control.Dependencies.Tasks {
|
||||
go func(task component.Cronable) {
|
||||
defer wg.Done()
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ import (
|
|||
|
||||
type UpdateInstanceList struct {
|
||||
component.Base
|
||||
Home *Home
|
||||
Dependencies struct {
|
||||
Home *Home
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -20,18 +22,20 @@ func (*UpdateInstanceList) TaskName() string {
|
|||
}
|
||||
|
||||
func (ul *UpdateInstanceList) Cron(ctx context.Context) error {
|
||||
names, err := ul.Home.instanceMap(ctx)
|
||||
names, err := ul.Dependencies.Home.instanceMap(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ul.Home.instanceNames.Set(names)
|
||||
ul.Dependencies.Home.instanceNames.Set(names)
|
||||
return nil
|
||||
}
|
||||
|
||||
type UpdateRedirect struct {
|
||||
component.Base
|
||||
Home *Home
|
||||
Dependencies struct {
|
||||
Home *Home
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -43,18 +47,20 @@ func (ur *UpdateRedirect) TaskName() string {
|
|||
}
|
||||
|
||||
func (ur *UpdateRedirect) Cron(ctx context.Context) error {
|
||||
redirect, err := ur.Home.loadRedirect(ctx)
|
||||
redirect, err := ur.Dependencies.Home.loadRedirect(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ur.Home.redirect.Set(&redirect)
|
||||
ur.Dependencies.Home.redirect.Set(&redirect)
|
||||
return nil
|
||||
}
|
||||
|
||||
type UpdateHome struct {
|
||||
component.Base
|
||||
Home *Home
|
||||
Dependencies struct {
|
||||
Home *Home
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -66,11 +72,11 @@ func (ur *UpdateHome) TaskName() string {
|
|||
}
|
||||
|
||||
func (ur *UpdateHome) Cron(ctx context.Context) error {
|
||||
bytes, err := ur.Home.homeRender(ctx)
|
||||
bytes, err := ur.Dependencies.Home.homeRender(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ur.Home.homeBytes.Set(bytes)
|
||||
ur.Dependencies.Home.homeBytes.Set(bytes)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ import (
|
|||
|
||||
type Home struct {
|
||||
component.Base
|
||||
|
||||
Instances *instances.Instances
|
||||
Dependencies struct {
|
||||
Instances *instances.Instances
|
||||
}
|
||||
|
||||
redirect lazy.Lazy[*Redirect]
|
||||
instanceNames lazy.Lazy[map[string]struct{}]
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import (
|
|||
)
|
||||
|
||||
func (home *Home) instanceMap(ctx context.Context) (map[string]struct{}, error) {
|
||||
wissKIs, err := home.Instances.All(ctx)
|
||||
wissKIs, err := home.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -37,7 +37,7 @@ func (home *Home) homeRender(ctx context.Context) ([]byte, error) {
|
|||
context.SelfRedirect = home.Config.SelfRedirect.String()
|
||||
|
||||
// find all the WissKIs
|
||||
wissKIs, err := home.Instances.All(ctx)
|
||||
wissKIs, err := home.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ func (info *Info) ingredients(r *http.Request) (cp ingredientsContext, err error
|
|||
cp.Time = time.Now().UTC()
|
||||
|
||||
// find the instance itself!
|
||||
instance, err := info.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
|
||||
instance, err := info.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
|
||||
if err == instances.ErrWissKINotFound {
|
||||
return cp, httpx.ErrNotFound
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func (info *Info) Status(ctx context.Context, QuickInformation bool) (target sta
|
|||
|
||||
group.Go(func() error {
|
||||
// list all the instances
|
||||
all, err := info.Instances.All(ctx)
|
||||
all, err := info.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -63,7 +63,7 @@ func (info *Info) Status(ctx context.Context, QuickInformation bool) (target sta
|
|||
flags := component.FetcherFlags{
|
||||
Context: ctx,
|
||||
}
|
||||
for _, o := range info.Fetchers {
|
||||
for _, o := range info.Dependencies.Fetchers {
|
||||
o := o
|
||||
group.Go(func() error {
|
||||
return o.Fetch(flags, &target)
|
||||
|
|
|
|||
|
|
@ -16,13 +16,15 @@ import (
|
|||
|
||||
type Info struct {
|
||||
component.Base
|
||||
Dependencies struct {
|
||||
Fetchers []component.DistilleryFetcher
|
||||
|
||||
Exporter *exporter.Exporter
|
||||
Instances *instances.Instances
|
||||
SnapshotsLog *logger.Logger
|
||||
}
|
||||
|
||||
Analytics *lazy.PoolAnalytics
|
||||
Fetchers []component.DistilleryFetcher
|
||||
|
||||
Exporter *exporter.Exporter
|
||||
Instances *instances.Instances
|
||||
SnapshotsLog *logger.Logger
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -33,7 +35,9 @@ var (
|
|||
func (*Info) Routes() []string { return []string{"/dis/"} }
|
||||
|
||||
func (info *Info) Handler(ctx context.Context, route string) (handler http.Handler, err error) {
|
||||
|
||||
router := httprouter.New()
|
||||
|
||||
{
|
||||
socket := &httpx.WebSocket{
|
||||
Context: ctx,
|
||||
|
|
@ -47,41 +51,41 @@ func (info *Info) Handler(ctx context.Context, route string) (handler http.Handl
|
|||
|
||||
// handle everything
|
||||
router.HandlerFunc(http.MethodGet, route, func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "index", http.StatusTemporaryRedirect)
|
||||
http.Redirect(w, r, route+"index", http.StatusTemporaryRedirect)
|
||||
})
|
||||
|
||||
// add a handler for the index page
|
||||
router.Handler(http.MethodGet, "index", httpx.HTMLHandler[indexContext]{
|
||||
router.Handler(http.MethodGet, route+"index", httpx.HTMLHandler[indexContext]{
|
||||
Handler: info.index,
|
||||
Template: indexTemplate,
|
||||
})
|
||||
|
||||
// add a handler for the component page
|
||||
router.Handler(http.MethodGet, "components", httpx.HTMLHandler[componentContext]{
|
||||
router.Handler(http.MethodGet, route+"components", httpx.HTMLHandler[componentContext]{
|
||||
Handler: info.components,
|
||||
Template: componentsTemplate,
|
||||
})
|
||||
|
||||
// add a handler for the component page
|
||||
router.Handler(http.MethodGet, "ingredients/{slug}", httpx.HTMLHandler[ingredientsContext]{
|
||||
router.Handler(http.MethodGet, route+"ingredients/:slug", httpx.HTMLHandler[ingredientsContext]{
|
||||
Handler: info.ingredients,
|
||||
Template: ingredientsTemplate,
|
||||
})
|
||||
|
||||
// add a handler for the instance page
|
||||
router.Handler(http.MethodGet, "instance/{slug}", httpx.HTMLHandler[instanceContext]{
|
||||
router.Handler(http.MethodGet, route+"instance/:slug", httpx.HTMLHandler[instanceContext]{
|
||||
Handler: info.instance,
|
||||
Template: instanceTemplate,
|
||||
})
|
||||
|
||||
router.Handler(http.MethodPost, "api/login", httpx.RedirectHandler(func(r *http.Request) (string, int, error) {
|
||||
router.Handler(http.MethodPost, route+"api/login", httpx.RedirectHandler(func(r *http.Request) (string, int, error) {
|
||||
// parse the form
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
// get the instance
|
||||
instance, err := info.Instances.WissKI(r.Context(), r.PostFormValue("slug"))
|
||||
instance, err := info.Dependencies.Instances.WissKI(r.Context(), r.PostFormValue("slug"))
|
||||
if err != nil {
|
||||
return "", 0, httpx.ErrNotFound
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ type instanceContext struct {
|
|||
|
||||
func (info *Info) instance(r *http.Request) (is instanceContext, err error) {
|
||||
// find the instance itself!
|
||||
instance, err := info.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
|
||||
instance, err := info.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
|
||||
if err == instances.ErrWissKINotFound {
|
||||
return is, httpx.ErrNotFound
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ type InstanceAction struct {
|
|||
var socketInstanceActions = map[string]InstanceAction{
|
||||
"snapshot": {
|
||||
HandleInteractive: func(ctx context.Context, info *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
|
||||
return info.Exporter.MakeExport(
|
||||
return info.Dependencies.Exporter.MakeExport(
|
||||
ctx,
|
||||
out,
|
||||
exporter.ExportTask{
|
||||
|
|
@ -78,7 +78,7 @@ func (info *Info) handleInstanceAction(conn httpx.WebSocketConnection, action In
|
|||
}
|
||||
|
||||
// resolve the instance
|
||||
instance, err := info.Instances.WissKI(conn.Context(), string(slug.Bytes))
|
||||
instance, err := info.Dependencies.Instances.WissKI(conn.Context(), string(slug.Bytes))
|
||||
if err != nil {
|
||||
<-conn.WriteText("Instance not found")
|
||||
return
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ func (control *Control) Server(ctx context.Context, progress io.Writer) (*http.S
|
|||
mux := http.NewServeMux()
|
||||
|
||||
// add all the servable routes!
|
||||
for _, s := range control.Servables {
|
||||
for _, s := range control.Dependencies.Servables {
|
||||
for _, route := range s.Routes() {
|
||||
zerolog.Ctx(ctx).Info().Str("component", s.Name()).Str("route", route).Msg("mounting route")
|
||||
handler, err := s.Handler(ctx, route)
|
||||
|
|
|
|||
|
|
@ -36,6 +36,17 @@
|
|||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ range $name, $comp := .DCFields }}
|
||||
<tr>
|
||||
<td>Component Pointer</td>
|
||||
<td>
|
||||
<code>Dependencies/{{ $name }}</code>
|
||||
</td>
|
||||
<td>
|
||||
<code><a href="#{{ $comp }}">{{ $comp }}</a></code>
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ range $name, $iface := .IFields }}
|
||||
<tr>
|
||||
<td>Interface Slice</td>
|
||||
|
|
@ -47,6 +58,17 @@
|
|||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ range $name, $iface := .DIFields }}
|
||||
<tr>
|
||||
<td>Interface Slice</td>
|
||||
<td>
|
||||
<code>Dependencies/{{ $name }}</code>
|
||||
</td>
|
||||
<td>
|
||||
<code><a href="#{{ $iface }}">[]{{ $iface }}</a></code>
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ range $name, $sig := $comp.Methods }}
|
||||
<tr>
|
||||
<td>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue