2018-07-06 14:53:13 +02:00
|
|
|
package chartserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-07-11 11:31:34 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-07-06 14:53:13 +02:00
|
|
|
"net/http"
|
2018-07-11 11:31:34 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2018-07-06 14:53:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
contentTypeHeader = "content-type"
|
|
|
|
contentTypeJSON = "application/json"
|
|
|
|
)
|
|
|
|
|
2018-07-23 11:41:40 +02:00
|
|
|
//WriteError writes error to http client
|
|
|
|
func WriteError(w http.ResponseWriter, code int, err error) {
|
2018-07-06 14:53:13 +02:00
|
|
|
errorObj := make(map[string]string)
|
|
|
|
errorObj["error"] = err.Error()
|
2018-08-03 13:25:39 +02:00
|
|
|
errorContent, errorMarshal := json.Marshal(errorObj)
|
|
|
|
if errorMarshal != nil {
|
|
|
|
errorContent = []byte(err.Error())
|
|
|
|
}
|
2018-07-06 14:53:13 +02:00
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write(errorContent)
|
|
|
|
}
|
|
|
|
|
2018-07-23 11:41:40 +02:00
|
|
|
//WriteInternalError writes error with statusCode == 500
|
|
|
|
func WriteInternalError(w http.ResponseWriter, err error) {
|
|
|
|
WriteError(w, http.StatusInternalServerError, err)
|
2018-07-06 14:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Write JSON data to http client
|
|
|
|
func writeJSONData(w http.ResponseWriter, data []byte) {
|
|
|
|
w.Header().Set(contentTypeHeader, contentTypeJSON)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(data)
|
|
|
|
}
|
2018-07-11 11:31:34 +02:00
|
|
|
|
2018-07-23 11:41:40 +02:00
|
|
|
//Extract error object '{"error": "****---***"}' from the content if existing
|
|
|
|
//nil error will be returned if it does exist
|
|
|
|
func extractError(content []byte) error {
|
|
|
|
if len(content) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
errorObj := make(map[string]string)
|
|
|
|
err := json.Unmarshal(content, &errorObj)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if errText, ok := errorObj["error"]; ok {
|
|
|
|
return errors.New(errText)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 11:31:34 +02:00
|
|
|
//Parse the redis configuration to the beego cache pattern
|
|
|
|
//Config pattern is "address:port[,weight,password,db_index]"
|
|
|
|
func parseRedisConfig(redisConfigV string) (string, error) {
|
|
|
|
if len(redisConfigV) == 0 {
|
|
|
|
return "", errors.New("empty redis config")
|
|
|
|
}
|
|
|
|
|
|
|
|
redisConfig := make(map[string]string)
|
|
|
|
redisConfig["key"] = cacheCollectionName
|
|
|
|
|
2018-08-23 07:59:13 +02:00
|
|
|
//Try best to parse the configuration segments.
|
|
|
|
//If the related parts are missing, assign default value.
|
|
|
|
//The default database index for UI process is 0.
|
|
|
|
configSegments := strings.Split(redisConfigV, ",")
|
|
|
|
for i, segment := range configSegments {
|
|
|
|
if i > 3 {
|
|
|
|
//ignore useless segments
|
|
|
|
break
|
2018-07-11 11:31:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 07:59:13 +02:00
|
|
|
switch i {
|
|
|
|
//address:port
|
|
|
|
case 0:
|
|
|
|
redisConfig["conn"] = segment
|
|
|
|
//password, may not exist
|
|
|
|
case 2:
|
|
|
|
redisConfig["password"] = segment
|
|
|
|
//database index, may not exist
|
|
|
|
case 3:
|
|
|
|
redisConfig["dbNum"] = segment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Assign default value
|
|
|
|
if len(redisConfig["dbNum"]) == 0 {
|
2018-07-11 11:31:34 +02:00
|
|
|
redisConfig["dbNum"] = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
//Try to validate the connection address
|
|
|
|
fullAddr := redisConfig["conn"]
|
|
|
|
if strings.Index(fullAddr, "://") == -1 {
|
|
|
|
//Append schema
|
|
|
|
fullAddr = fmt.Sprintf("redis://%s", fullAddr)
|
|
|
|
}
|
|
|
|
//Validate it by url
|
|
|
|
_, err := url.Parse(fullAddr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
//Convert config map to string
|
|
|
|
cfgData, err := json.Marshal(redisConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(cfgData), nil
|
|
|
|
}
|