-
Notifications
You must be signed in to change notification settings - Fork 6
/
environment.go
110 lines (92 loc) · 2.7 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Tideland Go REST Server Library - REST - Environment
//
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package rest
//--------------------
// IMPORTS
//--------------------
import (
"context"
"github.com/tideland/golib/etc"
"github.com/tideland/golib/stringex"
)
//--------------------
// ENVIRONMENT
//--------------------
type Environment interface {
// Context returns the context of the environment.
Context() context.Context
// Basepath returns the configured basepath.
Basepath() string
// DefaultDomain returns the configured default domain.
DefaultDomain() string
// DefaultResource returns the configured default resource.
DefaultResource() string
// TemplatesCache returns the template cache.
TemplatesCache() TemplatesCache
}
// environment implements the Environment interface.
type environment struct {
ctx context.Context
basepath string
baseparts []string
basepartsLen int
defaultDomain string
defaultResource string
templatesCache TemplatesCache
}
// newEnvironment crerates an environment using the
// passed context and configuration.
func newEnvironment(ctx context.Context, cfg etc.Etc) *environment {
env := &environment{
basepath: "/",
baseparts: []string{},
defaultDomain: "default",
defaultResource: "default",
templatesCache: newTemplatesCache(),
}
// Check configuration.
if cfg != nil {
env.basepath = cfg.ValueAsString("basepath", env.basepath)
env.defaultDomain = cfg.ValueAsString("default-domain", env.defaultDomain)
env.defaultResource = cfg.ValueAsString("default-resource", env.defaultResource)
}
// Check basepath and remove empty parts.
env.baseparts = stringex.SplitMap(env.basepath, "/", func(p string) (string, bool) {
if p == "" {
return "", false
}
return p, true
})
env.basepartsLen = len(env.baseparts)
// Set context.
if ctx == nil {
ctx = context.Background()
}
env.ctx = newEnvironmentContext(ctx, env)
return env
}
// Context implements the Environment interface.
func (env *environment) Context() context.Context {
return env.ctx
}
// Basepath implements the Environment interface.
func (env *environment) Basepath() string {
return env.basepath
}
// DefaultDomain implements the Environment interface.
func (env *environment) DefaultDomain() string {
return env.defaultDomain
}
// DefaultResource implements the Environment interface.
func (env *environment) DefaultResource() string {
return env.defaultResource
}
// TemplatesCache implements the Environment interface.
func (env *environment) TemplatesCache() TemplatesCache {
return env.templatesCache
}
// EOF