Skip to content

Commit

Permalink
feat: add an option to specify UI's assets path
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Nov 25, 2020
1 parent 506fa7a commit e3edb25
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Caddyfile
Expand Up @@ -6,7 +6,7 @@
experimental_http3
}

:80
localhost

log

Expand Down
12 changes: 8 additions & 4 deletions caddy/caddy.go
Expand Up @@ -50,7 +50,7 @@ type Mercure struct {
Anonymous bool `json:"anonymous,omitempty"`

// Enable the demo.
Demo bool `json:"demo,omitempty"`
Demo string `json:"demo,omitempty"`

// Dispatch updates when subscriptions are created or terminated
Subscriptions bool `json:"subscriptions,omitempty"`
Expand Down Expand Up @@ -145,8 +145,8 @@ func (m *Mercure) Provision(ctx caddy.Context) error { //nolint:funlen
if m.Anonymous {
opts = append(opts, mercure.WithAnonymous())
}
if m.Demo {
opts = append(opts, mercure.WithDemo())
if m.Demo != "" {
opts = append(opts, mercure.WithDemo(m.Demo))
}
if m.Subscriptions {
opts = append(opts, mercure.WithSubscriptions())
Expand Down Expand Up @@ -202,7 +202,11 @@ func (m *Mercure) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { //nolint:fu
m.Anonymous = true

case "demo":
m.Demo = true
if d.NextArg() {
m.Demo = d.Val()
} else {
m.Demo = "public/"
}

case "subscriptions":
m.Subscriptions = true
Expand Down
2 changes: 1 addition & 1 deletion config.go
Expand Up @@ -173,7 +173,7 @@ func NewHubFromViper(v *viper.Viper) (*Hub, error) { //nolint:funlen
options = append(options, WithAnonymous())
}
if v.GetBool("demo") {
options = append(options, WithDemo())
options = append(options, WithDemo(""))
}
if d := v.GetDuration("write_timeout"); d != 600*time.Second {
options = append(options, WithWriteTimeout(d))
Expand Down
2 changes: 1 addition & 1 deletion docs/hub/config.md
Expand Up @@ -41,7 +41,7 @@ The following Mercure-specific directives are available:
| `transport_url <url>` | URL representation of the transport to use. Use `local://local` to disabled history, (example `bolt:///var/run/mercure.db?size=100&cleanup_frequency=0.4`), see also [the cluster mode](cluster.md) | `bolt://mercure.db` |
| `dispatch_timeout <duration>` | maximum duration of the dispatch of a single update, set to `0s` disable | `5s` |
| `write_timeout <duration>` | maximum duration before closing the connection, set to `0s` disable | `600s` |
| `demo` | enabled the demo mode and the UI | |
| `demo [<assets-path>]` | enabled the demo mode and the UI | |

See also [the list of built-in Caddyfile directives](https://caddyserver.com/docs/caddyfile/directives).

Expand Down
8 changes: 4 additions & 4 deletions handler.go
Expand Up @@ -27,9 +27,9 @@ func (h *Hub) initHandler() {
router.SkipClean(true)

csp := "default-src 'self'"
if h.debug || h.demo {
if h.uiPath != "" {
router.PathPrefix(defaultDemoURL).HandlerFunc(Demo).Methods("GET", "HEAD")
router.PathPrefix(defaultUIURL).Handler(http.StripPrefix(defaultUIURL, http.FileServer(http.Dir("public"))))
router.PathPrefix(defaultUIURL).Handler(http.StripPrefix(defaultUIURL, http.FileServer(http.Dir(h.uiPath))))

csp += " mercure.rocks cdn.jsdelivr.net"
}
Expand Down Expand Up @@ -175,9 +175,9 @@ func (h *Hub) chainHandlers() http.Handler {
r.HandleFunc(defaultHubURL, h.PublishHandler).Methods("POST")

csp := "default-src 'self'"
if h.debug || h.demo {
if h.uiPath != "" {
r.PathPrefix("/demo").HandlerFunc(Demo).Methods("GET", "HEAD")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("public")))
r.PathPrefix("/").Handler(http.FileServer(http.Dir(h.uiPath)))
csp += " mercure.rocks cdn.jsdelivr.net"
} else {
r.HandleFunc("/", welcomeHandler).Methods("GET", "HEAD")
Expand Down
10 changes: 7 additions & 3 deletions hub.go
Expand Up @@ -32,9 +32,13 @@ func WithDebug() Option {
}

// WithDemo enables the demo.
func WithDemo() Option {
func WithDemo(uiPath string) Option {
return func(o *opt) error {
o.demo = true
if uiPath == "" {
o.uiPath = "public/"
} else {
o.uiPath = uiPath
}

return nil
}
Expand Down Expand Up @@ -176,8 +180,8 @@ type opt struct {
transport Transport
anonymous bool
debug bool
demo bool
subscriptions bool
uiPath string
logger Logger
writeTimeout time.Duration
dispatchTimeout time.Duration
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Expand Up @@ -59,7 +59,7 @@ func TestForwardedHeaders(t *testing.T) {
}

func TestSecurityOptions(t *testing.T) {
h := createAnonymousDummy(WithSubscriptions(), WithDemo(), WithCORSOrigins([]string{"*"}))
h := createAnonymousDummy(WithSubscriptions(), WithDemo(""), WithCORSOrigins([]string{"*"}))
h.config.Set("cert_file", "fixtures/tls/server.crt")
h.config.Set("key_file", "fixtures/tls/server.key")
h.config.Set("compress", true)
Expand Down

0 comments on commit e3edb25

Please sign in to comment.