/
config.go
60 lines (47 loc) · 1.72 KB
/
config.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
package api
import (
"net/http"
"strings"
"github.com/IBAX-io/go-ibax/packages/conf"
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/publisher"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
type configOptionHandler func(w http.ResponseWriter, option string) error
func getConfigOptionHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
logger := getLogger(r)
if len(params["option"]) == 0 {
logger.WithFields(log.Fields{"type": consts.EmptyObject, "error": "option not specified"}).Error("on getting option in config handler")
errorResponse(w, errNotFound)
return
}
switch params["option"] {
case "centrifugo":
centrifugoAddressHandler(w, r)
return
}
errorResponse(w, errNotFound)
}
func replaceHttpSchemeToWs(centrifugoURL string) string {
if strings.HasPrefix(centrifugoURL, "http:") {
return strings.Replace(centrifugoURL, "http:", "ws:", -1)
} else if strings.HasPrefix(centrifugoURL, "https:") {
return strings.Replace(centrifugoURL, "https:", "wss:", -1)
}
return centrifugoURL
}
func centrifugoAddressHandler(w http.ResponseWriter, r *http.Request) {
logger := getLogger(r)
if _, err := publisher.GetStats(); err != nil {
logger.WithFields(log.Fields{"type": consts.CentrifugoError, "error": err}).Warn("on getting centrifugo stats")
errorResponse(w, err)
return
}
jsonResponse(w, replaceHttpSchemeToWs(conf.Config.Centrifugo.URL))
}