Address issue #50.
[stompngo.git] / senv / senv.go
blob09c5df247b8a34e74b1eda32337d24b6fff75a54
1 //
2 // Copyright © 2014-2019 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 Helper package for stompngo users.
20 Extract commonly used data elements from the environment, and expose
21 this data to users.
24 package senv
26 import (
27 "log"
28 "os"
29 "strconv"
30 "sync"
33 var (
34 host = "localhost" // default host
35 port = "61613" // default port
36 protocol = "1.2" // Default protocol level
37 login = "guest" // default login
38 passcode = "guest" // default passcode
39 vhost = "localhost" // default vhost
40 heartbeats = "0,0" // default (no) heartbeats
41 dest = "/queue/sng.sample.stomp.destination" // default destination
42 scc = 1 // Subchannel capacity
43 nmsgs = 1 // default number of messages (useful at times)
44 maxbl = -1 // Max body length to dump (-1 => no limit)
45 vhLock sync.Mutex // vhsost set lock
46 nmLock sync.Mutex // nmsgs set lock
49 // Destination
50 func Dest() string {
51 // Destination
52 de := os.Getenv("STOMP_DEST")
53 if de != "" {
54 dest = de
56 return dest
59 // Heartbeats returns client requested heart beat values.
60 func Heartbeats() string {
61 // Heartbeats
62 hb := os.Getenv("STOMP_HEARTBEATS")
63 if hb != "" {
64 heartbeats = hb
66 return heartbeats
69 // Host returns a default connection hostname.
70 func Host() string {
71 // Host
72 he := os.Getenv("STOMP_HOST")
73 if he != "" {
74 host = he
76 return host
79 // HostAndPort returns a default host and port (useful for Dial).
80 func HostAndPort() (string, string) {
81 return Host(), Port()
84 // Login returns a default login ID.
85 func Login() string {
86 // Login
87 l := os.Getenv("STOMP_LOGIN")
88 if l != "" {
89 login = l
91 if l == "NONE" {
92 login = ""
94 return login
97 // Number of messages
98 func Nmsgs() int {
99 // Number of messages
100 nmLock.Lock()
101 defer nmLock.Unlock()
102 ns := os.Getenv("STOMP_NMSGS")
103 if ns == "" {
104 return nmsgs
106 n, e := strconv.ParseInt(ns, 10, 0)
107 if e != nil {
108 log.Printf("NMSGS Conversion error: %v\n", e)
109 return nmsgs
111 nmsgs = int(n)
112 return nmsgs
115 // Passcode returns a default passcode.
116 func Passcode() string {
117 // Passcode
118 pc := os.Getenv("STOMP_PASSCODE")
119 if pc != "" {
120 passcode = pc
122 if pc == "NONE" {
123 passcode = ""
125 return passcode
128 // True if persistent messages are desired.
129 func Persistent() bool {
130 f := os.Getenv("STOMP_PERSISTENT")
131 if f == "" {
132 return false
134 return true
137 // Port returns a default connection port.
138 func Port() string {
139 // Port
140 pt := os.Getenv("STOMP_PORT")
141 if pt != "" {
142 port = pt
144 return port
147 // Protocol returns a default level.
148 func Protocol() string {
149 // Protocol
150 pr := os.Getenv("STOMP_PROTOCOL")
151 if pr != "" {
152 protocol = pr
154 return protocol
157 func SubChanCap() int {
158 if s := os.Getenv("STOMP_SUBCHANCAP"); s != "" {
159 i, e := strconv.ParseInt(s, 10, 32)
160 if nil != e {
161 log.Println("SUBCHANCAP conversion error", e)
162 } else {
163 scc = int(i)
166 return scc
169 // Vhost returns a default vhost name.
170 func Vhost() string {
171 // Vhost
172 vhLock.Lock()
173 defer vhLock.Unlock()
174 vh := os.Getenv("STOMP_VHOST")
175 if vh != "" {
176 vhost = vh
177 } else {
178 vhost = Host()
180 return vhost
183 func MaxBodyLength() int {
184 if s := os.Getenv("STOMP_MAXBODYLENGTH"); s != "" {
185 i, e := strconv.ParseInt(s, 10, 32)
186 if nil != e {
187 log.Println("MAXBODYLENGTH conversion error", e)
188 } else {
189 maxbl = int(i)
192 return maxbl
195 // Optional set logger during connection start
196 func WantLogger() string {
197 return os.Getenv("STOMP_LOGGER")