2019-07-01 06:07:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-15 01:58:28 +08:00
|
|
|
package caddy
|
2019-03-27 05:45:51 +08:00
|
|
|
|
|
|
|
import (
|
2019-11-05 03:05:20 +08:00
|
|
|
"bytes"
|
2019-05-17 06:05:38 +08:00
|
|
|
"context"
|
2019-03-27 05:45:51 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-11-05 03:05:20 +08:00
|
|
|
"io"
|
2019-03-27 09:42:52 +08:00
|
|
|
"log"
|
2019-11-05 03:05:20 +08:00
|
|
|
"net/http"
|
|
|
|
"path"
|
2019-06-29 09:28:28 +08:00
|
|
|
"runtime/debug"
|
2019-11-05 03:05:20 +08:00
|
|
|
"strconv"
|
2019-03-27 05:45:51 +08:00
|
|
|
"strings"
|
2019-03-27 09:42:52 +08:00
|
|
|
"sync"
|
2019-03-27 05:45:51 +08:00
|
|
|
"time"
|
2019-04-26 03:54:48 +08:00
|
|
|
|
|
|
|
"github.com/mholt/certmagic"
|
2019-03-27 05:45:51 +08:00
|
|
|
)
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// Config represents a Caddy configuration. It is the
|
|
|
|
// top of the module structure: all Caddy modules will
|
|
|
|
// be loaded, starting with this struct. In order to
|
|
|
|
// be loaded and run successfully, a Config and all its
|
|
|
|
// modules must be JSON-encodable; i.e. when filling a
|
|
|
|
// Config struct manually, its JSON-encodable fields
|
|
|
|
// (the ones with JSON struct tags, usually ending with
|
|
|
|
// "Raw" if they decode into a separate field) must be
|
|
|
|
// set so that they can be unmarshaled and provisioned.
|
|
|
|
// Setting the fields for the decoded values instead
|
|
|
|
// will result in those values being overwritten at
|
|
|
|
// unmarshaling/provisioning.
|
2019-06-29 09:28:28 +08:00
|
|
|
type Config struct {
|
2019-10-29 04:39:37 +08:00
|
|
|
Admin *AdminConfig `json:"admin,omitempty"`
|
|
|
|
Logging *Logging `json:"logging,omitempty"`
|
|
|
|
StorageRaw json.RawMessage `json:"storage,omitempty"`
|
|
|
|
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
|
2019-06-29 09:28:28 +08:00
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
apps map[string]App
|
|
|
|
storage certmagic.Storage
|
2019-06-29 09:28:28 +08:00
|
|
|
|
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// App is a thing that Caddy runs.
|
|
|
|
type App interface {
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// Run runs the given config, replacing any existing config.
|
|
|
|
func Run(cfg *Config) error {
|
|
|
|
cfgJSON, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return Load(cfgJSON, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the given config JSON and runs it only
|
|
|
|
// if it is different from the current config or
|
|
|
|
// forceReload is true.
|
|
|
|
func Load(cfgJSON []byte, forceReload bool) error {
|
|
|
|
return changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, forceReload)
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeConfig changes the current config (rawCfg) according to the
|
|
|
|
// method, traversed via the given path, and uses the given input as
|
|
|
|
// the new value (if applicable; i.e. "DELETE" doesn't have an input).
|
|
|
|
// If the resulting config is the same as the previous, no reload will
|
|
|
|
// occur unless forceReload is true. This function is safe for
|
|
|
|
// concurrent use.
|
|
|
|
func changeConfig(method, path string, input []byte, forceReload bool) error {
|
|
|
|
switch method {
|
|
|
|
case http.MethodGet,
|
|
|
|
http.MethodHead,
|
|
|
|
http.MethodOptions,
|
|
|
|
http.MethodConnect,
|
|
|
|
http.MethodTrace:
|
|
|
|
return fmt.Errorf("method not allowed")
|
|
|
|
}
|
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
currentCfgMu.Lock()
|
|
|
|
defer currentCfgMu.Unlock()
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
err := unsyncedConfigAccess(method, path, input, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find any IDs in this config and index them
|
|
|
|
idx := make(map[string]string)
|
|
|
|
err = indexConfigObjects(rawCfg[rawConfigKey], "/"+rawConfigKey, idx)
|
|
|
|
if err != nil {
|
|
|
|
return APIError{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
Err: fmt.Errorf("indexing config: %v", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the mutation is complete, so encode the entire config as JSON
|
|
|
|
newCfg, err := json.Marshal(rawCfg[rawConfigKey])
|
|
|
|
if err != nil {
|
|
|
|
return APIError{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Err: fmt.Errorf("encoding new config: %v", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing changed, no need to do a whole reload unless the client forces it
|
|
|
|
if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
|
|
|
|
Log().Named("admin.api.change_config").Info("config is unchanged")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// load this new config; if it fails, we need to revert to
|
|
|
|
// our old representation of caddy's actual config
|
|
|
|
err = unsyncedDecodeAndRun(newCfg)
|
|
|
|
if err != nil {
|
|
|
|
if len(rawCfgJSON) > 0 {
|
|
|
|
// restore old config state to keep it consistent
|
|
|
|
// with what caddy is still running; we need to
|
|
|
|
// unmarshal it again because it's likely that
|
|
|
|
// pointers deep in our rawCfg map were modified
|
|
|
|
var oldCfg interface{}
|
|
|
|
err2 := json.Unmarshal(rawCfgJSON, &oldCfg)
|
|
|
|
if err2 != nil {
|
|
|
|
err = fmt.Errorf("%v; additionally, restoring old config: %v", err, err2)
|
|
|
|
}
|
|
|
|
rawCfg[rawConfigKey] = oldCfg
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("loading new config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// success, so update our stored copy of the encoded
|
|
|
|
// config to keep it consistent with what caddy is now
|
|
|
|
// running (storing an encoded copy is not strictly
|
|
|
|
// necessary, but avoids an extra json.Marshal for
|
|
|
|
// each config change)
|
|
|
|
rawCfgJSON = newCfg
|
|
|
|
rawCfgIndex = idx
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readConfig traverses the current config to path
|
|
|
|
// and writes its JSON encoding to out.
|
|
|
|
func readConfig(path string, out io.Writer) error {
|
|
|
|
currentCfgMu.RLock()
|
|
|
|
defer currentCfgMu.RUnlock()
|
|
|
|
return unsyncedConfigAccess(http.MethodGet, path, nil, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexConfigObjects recurisvely searches ptr for object fields named
|
|
|
|
// "@id" and maps that ID value to the full configPath in the index.
|
|
|
|
// This function is NOT safe for concurrent access; obtain a write lock
|
|
|
|
// on currentCfgMu.
|
|
|
|
func indexConfigObjects(ptr interface{}, configPath string, index map[string]string) error {
|
|
|
|
switch val := ptr.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range val {
|
|
|
|
if k == idKey {
|
|
|
|
switch idVal := v.(type) {
|
|
|
|
case string:
|
|
|
|
index[idVal] = configPath
|
|
|
|
case float64: // all JSON numbers decode as float64
|
|
|
|
index[fmt.Sprintf("%v", idVal)] = configPath
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: %s field must be a string or number", configPath, idKey)
|
|
|
|
}
|
|
|
|
delete(val, idKey) // field is no longer needed, and will break config if not removed
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// traverse this object property recursively
|
|
|
|
err := indexConfigObjects(val[k], path.Join(configPath, k), index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
// traverse each element of the array recursively
|
|
|
|
for i := range val {
|
|
|
|
err := indexConfigObjects(val[i], path.Join(configPath, strconv.Itoa(i)), index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsyncedDecodeAndRun decodes cfgJSON and runs
|
|
|
|
// it as the new config, replacing any other
|
|
|
|
// current config. It does not update the raw
|
|
|
|
// config state, as this is a lower-level function;
|
|
|
|
// most callers will want to use Load instead.
|
|
|
|
// A write lock on currentCfgMu is required!
|
|
|
|
func unsyncedDecodeAndRun(cfgJSON []byte) error {
|
|
|
|
var newCfg *Config
|
|
|
|
err := strictUnmarshalJSON(cfgJSON, &newCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// run the new config and start all its apps
|
2019-11-05 03:05:20 +08:00
|
|
|
err = run(newCfg, true)
|
2019-09-30 23:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// swap old config with the new one
|
|
|
|
oldCfg := currentCfg
|
|
|
|
currentCfg = newCfg
|
2019-05-17 06:05:38 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// Stop, Cleanup each old app
|
|
|
|
unsyncedStop(oldCfg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// run runs newCfg and starts all its apps if
|
|
|
|
// start is true. If any errors happen, cleanup
|
|
|
|
// is performed if any modules were provisioned;
|
|
|
|
// apps that were started already will be stopped,
|
|
|
|
// so this function should not leak resources if
|
2019-10-29 04:39:37 +08:00
|
|
|
// an error is returned. However, if no error is
|
|
|
|
// returned and start == false, you should cancel
|
|
|
|
// the config if you are not going to start it,
|
|
|
|
// so that each provisioned module will be
|
|
|
|
// cleaned up.
|
2019-11-05 03:05:20 +08:00
|
|
|
//
|
|
|
|
// This is a low-level function; most callers
|
|
|
|
// will want to use Run instead, which also
|
|
|
|
// updates the config's raw state.
|
2019-09-30 23:16:01 +08:00
|
|
|
func run(newCfg *Config, start bool) error {
|
|
|
|
// because we will need to roll back any state
|
|
|
|
// modifications if this function errors, we
|
|
|
|
// keep a single error value and scope all
|
|
|
|
// sub-operations to their own functions to
|
|
|
|
// ensure this error value does not get
|
|
|
|
// overridden or missed when it should have
|
|
|
|
// been set by a short assignment
|
|
|
|
var err error
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// start the admin endpoint (and stop any prior one)
|
|
|
|
err = replaceAdmin(newCfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("starting caddy administration endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newCfg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// prepare the new config for use
|
|
|
|
newCfg.apps = make(map[string]App)
|
|
|
|
|
|
|
|
// create a context within which to load
|
|
|
|
// modules - essentially our new config's
|
|
|
|
// execution environment; be sure that
|
|
|
|
// cleanup occurs when we return if there
|
|
|
|
// was an error; if no error, it will get
|
|
|
|
// cleaned up on next config cycle
|
|
|
|
ctx, cancel := NewContext(Context{Context: context.Background(), cfg: newCfg})
|
|
|
|
defer func() {
|
2019-05-17 06:05:38 +08:00
|
|
|
if err != nil {
|
2019-10-29 04:39:37 +08:00
|
|
|
// if there were any errors during startup,
|
|
|
|
// we should cancel the new context we created
|
|
|
|
// since the associated config won't be used;
|
|
|
|
// this will cause all modules that were newly
|
|
|
|
// provisioned to clean themselves up
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// also undo any other state changes we made
|
|
|
|
if currentCfg != nil {
|
|
|
|
certmagic.Default.Storage = currentCfg.storage
|
|
|
|
}
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
}()
|
|
|
|
newCfg.cancelFunc = cancel // clean up later
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
// set up logging before anything bad happens
|
2019-10-30 01:58:29 +08:00
|
|
|
if newCfg.Logging == nil {
|
|
|
|
newCfg.Logging = new(Logging)
|
|
|
|
}
|
|
|
|
err = newCfg.Logging.openLogs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// set up global storage and make it CertMagic's default storage, too
|
2019-09-30 23:16:01 +08:00
|
|
|
err = func() error {
|
|
|
|
if newCfg.StorageRaw != nil {
|
|
|
|
val, err := ctx.LoadModuleInline("module", "caddy.storage", newCfg.StorageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading storage module: %v", err)
|
2019-05-17 06:05:38 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
stor, err := val.(StorageConverter).CertMagicStorage()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating storage value: %v", err)
|
|
|
|
}
|
|
|
|
newCfg.storage = stor
|
|
|
|
newCfg.StorageRaw = nil // allow GC to deallocate
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
if newCfg.storage == nil {
|
|
|
|
newCfg.storage = &certmagic.FileStorage{Path: dataDir()}
|
|
|
|
}
|
|
|
|
certmagic.Default.Storage = newCfg.storage
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load, Provision, Validate each app and their submodules
|
|
|
|
err = func() error {
|
|
|
|
for modName, rawMsg := range newCfg.AppsRaw {
|
|
|
|
val, err := ctx.LoadModule(modName, rawMsg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading app module '%s': %v", modName, err)
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
newCfg.apps[modName] = val.(App)
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
if !start {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// Start
|
|
|
|
return func() error {
|
|
|
|
var started []string
|
|
|
|
for name, a := range newCfg.apps {
|
|
|
|
err := a.Start()
|
|
|
|
if err != nil {
|
|
|
|
// an app failed to start, so we need to stop
|
|
|
|
// all other apps that were already started
|
|
|
|
for _, otherAppName := range started {
|
|
|
|
err2 := newCfg.apps[otherAppName].Stop()
|
|
|
|
if err2 != nil {
|
|
|
|
err = fmt.Errorf("%v; additionally, aborting app %s: %v",
|
|
|
|
err, otherAppName, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s app module: start: %v", name, err)
|
|
|
|
}
|
|
|
|
started = append(started, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
2019-07-13 00:07:11 +08:00
|
|
|
}
|
2019-04-03 05:31:02 +08:00
|
|
|
|
2019-07-13 00:07:11 +08:00
|
|
|
// Stop stops running the current configuration.
|
|
|
|
// It is the antithesis of Run(). This function
|
|
|
|
// will log any errors that occur during the
|
|
|
|
// stopping of individual apps and continue to
|
2019-11-05 03:05:20 +08:00
|
|
|
// stop the others. Stop should only be called
|
|
|
|
// if not replacing with a new config.
|
2019-07-13 00:07:11 +08:00
|
|
|
func Stop() error {
|
|
|
|
currentCfgMu.Lock()
|
|
|
|
defer currentCfgMu.Unlock()
|
|
|
|
unsyncedStop(currentCfg)
|
|
|
|
currentCfg = nil
|
2019-11-05 03:05:20 +08:00
|
|
|
rawCfgJSON = nil
|
|
|
|
rawCfgIndex = nil
|
|
|
|
rawCfg[rawConfigKey] = nil
|
2019-03-27 05:45:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// unsyncedStop stops cfg from running, but has
|
|
|
|
// no locking around cfg. It is a no-op if cfg is
|
|
|
|
// nil. If any app returns an error when stopping,
|
|
|
|
// it is logged and the function continues stopping
|
|
|
|
// the next app. This function assumes all apps in
|
|
|
|
// cfg were successfully started first.
|
2019-09-30 23:16:01 +08:00
|
|
|
func unsyncedStop(cfg *Config) {
|
|
|
|
if cfg == nil {
|
2019-07-13 00:07:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop each app
|
2019-09-30 23:16:01 +08:00
|
|
|
for name, a := range cfg.apps {
|
2019-07-13 00:07:11 +08:00
|
|
|
err := a.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] stop %s: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// clean up all modules
|
|
|
|
cfg.cancelFunc()
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// stopAndCleanup calls stop and cleans up anything
|
|
|
|
// else that is expedient. This should only be used
|
|
|
|
// when stopping and not replacing with a new config.
|
|
|
|
func stopAndCleanup() error {
|
|
|
|
if err := Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certmagic.CleanUpOwnLocks()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// Validate loads, provisions, and validates
|
|
|
|
// cfg, but does not start running it.
|
|
|
|
func Validate(cfg *Config) error {
|
2019-10-29 04:39:37 +08:00
|
|
|
err := run(cfg, false)
|
|
|
|
if err == nil {
|
|
|
|
cfg.cancelFunc() // call Cleanup on all modules
|
|
|
|
}
|
|
|
|
return err
|
2019-07-13 00:07:11 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// Duration is a JSON-string-unmarshable duration type.
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
2019-04-01 10:41:29 +08:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2019-11-05 03:54:46 +08:00
|
|
|
if len(b) == 0 {
|
|
|
|
return io.EOF
|
2019-04-01 10:41:29 +08:00
|
|
|
}
|
2019-11-05 03:54:46 +08:00
|
|
|
var dur time.Duration
|
|
|
|
var err error
|
|
|
|
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
|
|
|
dur, err = time.ParseDuration(strings.Trim(string(b), `"`))
|
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(b, &dur)
|
|
|
|
}
|
|
|
|
*d = Duration(dur)
|
|
|
|
return err
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 09:28:28 +08:00
|
|
|
// GoModule returns the build info of this Caddy
|
|
|
|
// build from debug.BuildInfo (requires Go modules).
|
|
|
|
// If no version information is available, a non-nil
|
|
|
|
// value will still be returned, but with an
|
|
|
|
// unknown version.
|
2019-08-08 13:59:02 +08:00
|
|
|
func GoModule() *debug.Module {
|
|
|
|
var mod debug.Module
|
|
|
|
return goModule(&mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// goModule holds the actual implementation of GoModule.
|
|
|
|
// Allocating debug.Module in GoModule() and passing a
|
|
|
|
// reference to goModule enables mid-stack inlining.
|
|
|
|
func goModule(mod *debug.Module) *debug.Module {
|
|
|
|
mod.Version = "unknown"
|
2019-06-29 09:28:28 +08:00
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if ok {
|
2019-07-13 00:15:27 +08:00
|
|
|
mod.Path = bi.Main.Path
|
2019-06-29 09:28:28 +08:00
|
|
|
// The recommended way to build Caddy involves
|
|
|
|
// creating a separate main module, which
|
|
|
|
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
|
2019-07-13 00:15:27 +08:00
|
|
|
// once that issue is fixed, we should just be able to use bi.Main... hopefully.
|
|
|
|
for _, dep := range bi.Deps {
|
2019-09-14 02:43:28 +08:00
|
|
|
if dep.Path == "github.com/caddyserver/caddy/v2" {
|
2019-08-18 09:14:55 +08:00
|
|
|
return dep
|
2019-06-29 09:28:28 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-18 09:14:55 +08:00
|
|
|
return &bi.Main
|
2019-06-29 09:28:28 +08:00
|
|
|
}
|
2019-07-13 00:15:27 +08:00
|
|
|
return mod
|
2019-06-29 09:28:28 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:42:55 +08:00
|
|
|
// CtxKey is a value type for use with context.WithValue.
|
|
|
|
type CtxKey string
|
2019-04-08 14:00:14 +08:00
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// This group of variables pertains to the current configuration.
|
2019-04-08 14:00:14 +08:00
|
|
|
var (
|
2019-11-05 03:05:20 +08:00
|
|
|
// currentCfgMu protects everything in this var block.
|
2019-04-26 03:54:48 +08:00
|
|
|
currentCfgMu sync.RWMutex
|
2019-11-05 03:05:20 +08:00
|
|
|
|
|
|
|
// currentCfg is the currently-running configuration.
|
|
|
|
currentCfg *Config
|
|
|
|
|
|
|
|
// rawCfg is the current, generic-decoded configuration;
|
|
|
|
// we initialize it as a map with one field ("config")
|
|
|
|
// to maintain parity with the API endpoint and to avoid
|
|
|
|
// the special case of having to access/mutate the variable
|
|
|
|
// directly without traversing into it.
|
|
|
|
rawCfg = map[string]interface{}{
|
|
|
|
rawConfigKey: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// rawCfgJSON is the JSON-encoded form of rawCfg. Keeping
|
|
|
|
// this around avoids an extra Marshal call during changes.
|
|
|
|
rawCfgJSON []byte
|
|
|
|
|
|
|
|
// rawCfgIndex is the map of user-assigned ID to expanded
|
|
|
|
// path, for converting /id/ paths to /config/ paths.
|
|
|
|
rawCfgIndex map[string]string
|
2019-04-08 14:00:14 +08:00
|
|
|
)
|