diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-18 13:07:24 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-21 11:22:19 +0300 |
commit | e9c7b6205f94a7edac0e0df2cd18d1456cb26a06 (patch) | |
tree | 7c71d49c556f22497c3e0072ac25a3375f690074 /commands/server.go | |
parent | 3d1a6e109ce9b25fc2e9731098a82fb4c0abff68 (diff) |
Allow themes to define output formats, media types and params
This allows a `config.toml` (or `yaml`, ´yml`, or `json`) in the theme to set:
1) `params` (but cannot override params in project. Will also get its own "namespace", i.e. `{{ .Site.Params.mytheme.my_param }}` will be the same as `{{ .Site.Params.my_param }}` providing that the main project does not define a param with that key.
2) `menu` -- but cannot redefine/add menus in the project. Must create its own menus with its own identifiers.
3) `languages` -- only `params` and `menu`. Same rules as above.
4) **new** `outputFormats`
5) **new** `mediaTypes`
This should help with the "theme portability" issue and people having to copy and paste lots of setting into their projects.
Fixes #4490
Diffstat (limited to 'commands/server.go')
-rw-r--r-- | commands/server.go | 68 |
1 files changed, 37 insertions, 31 deletions
diff --git a/commands/server.go b/commands/server.go index 130ac18be..278ba7f37 100644 --- a/commands/server.go +++ b/commands/server.go @@ -24,6 +24,7 @@ import ( "runtime" "strconv" "strings" + "sync" "syscall" "time" @@ -111,12 +112,16 @@ func init() { } +var serverPorts []int + func server(cmd *cobra.Command, args []string) error { // If a Destination is provided via flag write to disk if destination != "" { renderToDisk = true } + var serverCfgInit sync.Once + cfgInit := func(c *commandeer) error { c.Set("renderToMemory", !renderToDisk) if cmd.Flags().Changed("navigateToChanged") { @@ -132,37 +137,42 @@ func server(cmd *cobra.Command, args []string) error { c.Set("watch", true) } - serverPorts := make([]int, 1) + var err error - if c.languages.IsMultihost() { - if !serverAppend { - return newSystemError("--appendPort=false not supported when in multihost mode") + // We can only do this once. + serverCfgInit.Do(func() { + serverPorts = make([]int, 1) + + if c.languages.IsMultihost() { + if !serverAppend { + err = newSystemError("--appendPort=false not supported when in multihost mode") + } + serverPorts = make([]int, len(c.languages)) } - serverPorts = make([]int, len(c.languages)) - } - currentServerPort := serverPort + currentServerPort := serverPort - for i := 0; i < len(serverPorts); i++ { - l, err := net.Listen("tcp", net.JoinHostPort(serverInterface, strconv.Itoa(currentServerPort))) - if err == nil { - l.Close() - serverPorts[i] = currentServerPort - } else { - if i == 0 && serverCmd.Flags().Changed("port") { - // port set explicitly by user -- he/she probably meant it! - return newSystemErrorF("Server startup failed: %s", err) - } - jww.ERROR.Println("port", serverPort, "already in use, attempting to use an available port") - sp, err := helpers.FindAvailablePort() - if err != nil { - return newSystemError("Unable to find alternative port to use:", err) + for i := 0; i < len(serverPorts); i++ { + l, err := net.Listen("tcp", net.JoinHostPort(serverInterface, strconv.Itoa(currentServerPort))) + if err == nil { + l.Close() + serverPorts[i] = currentServerPort + } else { + if i == 0 && serverCmd.Flags().Changed("port") { + // port set explicitly by user -- he/she probably meant it! + err = newSystemErrorF("Server startup failed: %s", err) + } + jww.ERROR.Println("port", serverPort, "already in use, attempting to use an available port") + sp, err := helpers.FindAvailablePort() + if err != nil { + err = newSystemError("Unable to find alternative port to use:", err) + } + serverPorts[i] = sp.Port } - serverPorts[i] = sp.Port - } - currentServerPort = serverPorts[i] + 1 - } + currentServerPort = serverPorts[i] + 1 + } + }) c.serverPorts = serverPorts @@ -184,7 +194,7 @@ func server(cmd *cobra.Command, args []string) error { baseURL, err := fixURL(language, baseURL, serverPort) if err != nil { - return err + return nil } if isMultiHost { language.Set("baseURL", baseURL) @@ -194,7 +204,7 @@ func server(cmd *cobra.Command, args []string) error { } } - return nil + return err } @@ -215,10 +225,6 @@ func server(cmd *cobra.Command, args []string) error { s.RegisterMediaTypes() } - if serverWatch { - c.watchConfig() - } - // Watch runs its own server as part of the routine if serverWatch { |