2015-05-04 12:49:49 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-10-12 21:59:11 -05:00
|
|
|
"github.com/mholt/caddy/middleware/websocket"
|
2015-05-04 12:49:49 -05:00
|
|
|
)
|
|
|
|
|
2015-10-12 21:59:11 -05:00
|
|
|
// WebSocket configures a new WebSocket middleware instance.
|
2015-05-04 12:49:49 -05:00
|
|
|
func WebSocket(c *Controller) (middleware.Middleware, error) {
|
2015-06-23 04:10:27 -05:00
|
|
|
|
2015-06-23 20:35:53 -05:00
|
|
|
websocks, err := webSocketParse(c)
|
2015-06-23 04:10:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-12 21:59:11 -05:00
|
|
|
websocket.GatewayInterface = c.AppName + "-CGI/1.1"
|
|
|
|
websocket.ServerSoftware = c.AppName + "/" + c.AppVersion
|
2015-06-23 04:10:27 -05:00
|
|
|
|
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
2015-10-12 21:59:11 -05:00
|
|
|
return websocket.WebSocket{Next: next, Sockets: websocks}
|
2015-06-23 04:10:27 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-10-12 21:59:11 -05:00
|
|
|
func webSocketParse(c *Controller) ([]websocket.Config, error) {
|
|
|
|
var websocks []websocket.Config
|
2015-05-04 12:49:49 -05:00
|
|
|
var respawn bool
|
|
|
|
|
|
|
|
optionalBlock := func() (hadBlock bool, err error) {
|
|
|
|
for c.NextBlock() {
|
|
|
|
hadBlock = true
|
|
|
|
if c.Val() == "respawn" {
|
|
|
|
respawn = true
|
|
|
|
} else {
|
|
|
|
return true, c.Err("Expected websocket configuration parameter in block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
var val, path, command string
|
|
|
|
|
|
|
|
// Path or command; not sure which yet
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
val = c.Val()
|
|
|
|
|
|
|
|
// Extra configuration may be in a block
|
|
|
|
hadBlock, err := optionalBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hadBlock {
|
|
|
|
// The next argument on this line will be the command or an open curly brace
|
|
|
|
if c.NextArg() {
|
|
|
|
path = val
|
|
|
|
command = c.Val()
|
|
|
|
} else {
|
|
|
|
path = "/"
|
|
|
|
command = val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, check again for optional block
|
|
|
|
hadBlock, err = optionalBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split command into the actual command and its arguments
|
|
|
|
cmd, args, err := middleware.SplitCommandAndArgs(command)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-12 21:59:11 -05:00
|
|
|
websocks = append(websocks, websocket.Config{
|
2015-05-04 12:49:49 -05:00
|
|
|
Path: path,
|
|
|
|
Command: cmd,
|
|
|
|
Arguments: args,
|
|
|
|
Respawn: respawn, // TODO: This isn't used currently
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-23 04:10:27 -05:00
|
|
|
return websocks, nil
|
2015-05-04 12:49:49 -05:00
|
|
|
|
|
|
|
}
|