mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-19 07:43:17 +00:00
Rudimentary start of HTTP servers
This commit is contained in:
parent
859b5d7ea3
commit
86e2d1b0a4
6 changed files with 311 additions and 44 deletions
30
modules.go
30
modules.go
|
@ -1,7 +1,9 @@
|
|||
package caddy2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -104,6 +106,34 @@ func Modules() []string {
|
|||
return names
|
||||
}
|
||||
|
||||
// LoadModule decodes rawMsg into a new instance of mod and
|
||||
// returns the value. If mod.New() does not return a pointer
|
||||
// value, it is converted to one so that it is unmarshaled
|
||||
// into the underlying concrete type. If mod.New is nil, an
|
||||
// error is returned.
|
||||
func LoadModule(mod Module, rawMsg json.RawMessage) (interface{}, error) {
|
||||
if mod.New == nil {
|
||||
return nil, fmt.Errorf("no constructor")
|
||||
}
|
||||
|
||||
val, err := mod.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initializing module '%s': %v", mod.Name, err)
|
||||
}
|
||||
|
||||
// value must be a pointer for unmarshaling into concrete type
|
||||
if rv := reflect.ValueOf(val); rv.Kind() != reflect.Ptr {
|
||||
val = reflect.New(rv.Type()).Elem().Addr().Interface()
|
||||
}
|
||||
|
||||
err = json.Unmarshal(rawMsg, &val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err)
|
||||
}
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
var (
|
||||
modules = make(map[string]Module)
|
||||
modulesMu sync.Mutex
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue