-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenv.go
More file actions
123 lines (98 loc) · 2.4 KB
/
env.go
File metadata and controls
123 lines (98 loc) · 2.4 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
package zebra
import (
"fmt"
"strconv"
"sync"
)
//Simple implement for zebra evironment, you can use this to save your configs.
//It's thread-safe, so you can have all your fun in erverywhere.
type Environment struct {
sync.RWMutex
data map[string]string
}
// Set env variable with a pair of key-value, you cann't use key with prefix "Zebra:", which
// is reserved for zebra system
func (e *Environment) Set(key, value string) {
e.Lock()
defer e.Unlock()
e.data[key] = value
}
//Get a env variable with key, "" will return if not exist
func (e *Environment) Get(key string) string {
e.RLock()
defer e.RUnlock()
if value, ok := e.data[key]; ok {
return value
}
return ""
}
//Delete a env variable with key
func (e *Environment) Del(key string) {
e.Lock()
defer e.Unlock()
delete(e.data, key)
}
//Enable HTTPS to create a secure channel for private data, you must provide
//valid cert file, key file and HTTPS port
func (e *Environment) EnableTLS(cert, key, host string, port int) {
e.Set("Zebra:TLS", "true")
e.Set("Zebra:TLSCERT", cert)
e.Set("Zebra:TLSKEY", key)
e.Set("Zebra:TLSHOST", host)
p := fmt.Sprintf("%d", port)
e.Set("Zebra:TLSPORT", p)
}
//Disable HTTPS
func (e *Environment) DisableTLS() {
e.Set("Zebra:TLS", "false")
}
//Check if HTTPS is enabled
func (e *Environment) TLS() bool {
tls := e.Get("Zebra:TLS")
if tls == "true" {
return true
}
return false
}
//Get https cert file
func (e *Environment) TLSCert() string {
return e.Get("Zebra:TLSCERT")
}
//Get https key file
func (e *Environment) TLSKey() string {
return e.Get("Zebra:TLSKEY")
}
//Get https host
func (e *Environment) TLSHost() string {
return e.Get("Zebra:TLSHOST")
}
//Get https port, if port not set or error happened, default port 443 will be returned
func (e *Environment) TLSPort() int {
port := e.Get("Zebra:TLSPORT")
if port != "" {
if p, err := strconv.Atoi(port); err == nil {
return p
}
}
return 443
}
//Set http host and port, if not set, "localhost:8080" will be used
func (e *Environment) Http(host string, port int) {
p := fmt.Sprintf("%d", port)
e.Set("Zebra:HOST", host)
e.Set("Zebra:PORT", p)
}
//Get host, works for both http and https
func (e *Environment) Host() string {
return e.Get("Zebra:HOST")
}
//Get http listen port
func (e *Environment) Port() int {
port := e.Get("Zebra:PORT")
if port != "" {
if p, err := strconv.Atoi(port); err == nil {
return p
}
}
return 8080
}