forked from socialsky-io/go-actuator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
50 lines (45 loc) · 1.1 KB
/
utils.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
package actuator
import (
"bytes"
"encoding/json"
"net/http"
"strings"
)
// isValidEndpoint is used to check if the endpoint is valid or not
func isValidEndpoint(endpoint int) bool {
for _, e := range AllEndpoints {
if endpoint == e {
return true
}
}
return false
}
// encodeJSON is used to encode any type of data to byte array
func encodeJSON(v interface{}) ([]byte, error) {
var buffer bytes.Buffer
e := json.NewEncoder(&buffer)
err := e.Encode(v)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// getLastStringAfterDelimiter is used to return the string after last delimiter existence
// a/b/c with / returns c
// a returns a
func getLastStringAfterDelimiter(s, delim string) string {
splits := strings.Split(s, delim)
length := len(splits)
if length == 0 {
return ""
}
return splits[length-1]
}
// sendStringResponse is used to write string response to the output
func sendStringResponse(w http.ResponseWriter, status int, body string) {
w.WriteHeader(status)
if body != "" {
w.Header().Add(contentTypeHeader, textStringContentType)
_, _ = w.Write([]byte(body))
}
}