feat(layer,queue): display templatized page for queued users
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
@ -15,11 +15,11 @@ type LayerOptions struct {
|
||||
KeepAlive time.Duration `mapstructure:"keepAlive"`
|
||||
}
|
||||
|
||||
func fromStoreOptions(storeOptions store.LayerOptions) (*LayerOptions, error) {
|
||||
func fromStoreOptions(storeOptions store.LayerOptions, defaultKeepAlive time.Duration) (*LayerOptions, error) {
|
||||
layerOptions := LayerOptions{
|
||||
Capacity: 1000,
|
||||
Matchers: []string{"*"},
|
||||
KeepAlive: 30 * time.Second,
|
||||
KeepAlive: defaultKeepAlive,
|
||||
}
|
||||
|
||||
config := mapstructure.DecoderConfig{
|
||||
|
@ -1,9 +1,29 @@
|
||||
package queue
|
||||
|
||||
type Options struct{}
|
||||
import "time"
|
||||
|
||||
type Options struct {
|
||||
TemplateDir string
|
||||
DefaultKeepAlive time.Duration
|
||||
}
|
||||
|
||||
type OptionFunc func(*Options)
|
||||
|
||||
func defaultOptions() *Options {
|
||||
return &Options{}
|
||||
return &Options{
|
||||
TemplateDir: "./templates",
|
||||
DefaultKeepAlive: time.Minute,
|
||||
}
|
||||
}
|
||||
|
||||
func WithTemplateDir(templateDir string) OptionFunc {
|
||||
return func(o *Options) {
|
||||
o.TemplateDir = templateDir
|
||||
}
|
||||
}
|
||||
|
||||
func WithDefaultKeepAlive(keepAlive time.Duration) OptionFunc {
|
||||
return func(o *Options) {
|
||||
o.DefaultKeepAlive = keepAlive
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,17 @@ package queue
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Cadoles/go-proxy"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
@ -18,7 +22,14 @@ import (
|
||||
const LayerType store.LayerType = "queue"
|
||||
|
||||
type Queue struct {
|
||||
adapter Adapter
|
||||
adapter Adapter
|
||||
|
||||
defaultKeepAlive time.Duration
|
||||
|
||||
templateDir string
|
||||
loadOnce sync.Once
|
||||
tmpl *template.Template
|
||||
|
||||
refreshJobRunning uint32
|
||||
}
|
||||
|
||||
@ -33,7 +44,7 @@ func (q *Queue) Middleware(layer *store.Layer) proxy.Middleware {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
options, err := fromStoreOptions(layer.Options)
|
||||
options, err := fromStoreOptions(layer.Options, q.defaultKeepAlive)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "could not parse layer options", logger.E(errors.WithStack(err)))
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
@ -102,7 +113,52 @@ func (q *Queue) renderQueuePage(w http.ResponseWriter, r *http.Request, queueNam
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, fmt.Sprintf("queued (rank: %d, status: %d/%d)", rank+1, status.Sessions, options.Capacity), http.StatusServiceUnavailable)
|
||||
q.loadOnce.Do(func() {
|
||||
pattern := filepath.Join(q.templateDir, "*.gohtml")
|
||||
|
||||
logger.Info(ctx, "loading queue page templates", logger.F("pattern", pattern))
|
||||
|
||||
tmpl, err := template.New("").Funcs(sprig.FuncMap()).ParseGlob(pattern)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "could not load queue templates", logger.E(errors.WithStack(err)))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
q.tmpl = tmpl
|
||||
})
|
||||
|
||||
if q.tmpl == nil {
|
||||
logger.Error(ctx, "queue page templates not loaded", logger.E(errors.WithStack(err)))
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
templateData := struct {
|
||||
QueueName string
|
||||
LayerOptions *LayerOptions
|
||||
Rank int64
|
||||
CurrentSessions int64
|
||||
MaxSessions int64
|
||||
RefreshRate int64
|
||||
}{
|
||||
QueueName: queueName,
|
||||
LayerOptions: options,
|
||||
Rank: rank + 1,
|
||||
CurrentSessions: status.Sessions,
|
||||
MaxSessions: options.Capacity,
|
||||
RefreshRate: 5,
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
|
||||
if err := q.tmpl.ExecuteTemplate(w, "queue", templateData); err != nil {
|
||||
logger.Error(ctx, "could not render queue page", logger.E(errors.WithStack(err)))
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) refreshQueue(queueName string, keepAlive time.Duration) {
|
||||
@ -136,7 +192,9 @@ func New(adapter Adapter, funcs ...OptionFunc) *Queue {
|
||||
}
|
||||
|
||||
return &Queue{
|
||||
adapter: adapter,
|
||||
adapter: adapter,
|
||||
templateDir: opts.TemplateDir,
|
||||
defaultKeepAlive: opts.DefaultKeepAlive,
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user