-
Notifications
You must be signed in to change notification settings - Fork 135
/
utils.go
71 lines (59 loc) · 1.53 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Telize 4.0.0
* Copyright (c) 2013-2023, Frederic Cambus
* https://www.telize.com
*
* Created: 2013-08-15
* Last Updated: 2022-11-24
*
* Telize is released under the BSD 2-Clause license.
* See LICENSE file for details.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package main
import (
"encoding/json"
"io"
"net"
"net/http"
"strings"
)
// Return an HTTP Error along with a JSON-encoded error message
func errorCode(w http.ResponseWriter, status int, code int, message string) {
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Content-Type", "application/json")
if output, err := json.Marshal(Error{Code: code, Message: message}); err == nil {
w.WriteHeader(status)
io.WriteString(w, string(output))
}
}
func request_ip(r *http.Request) string {
var ip string
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
ips := strings.Split(xff, ",")
ip = ips[0]
}
if ip == "" {
ip, _, _ = net.SplitHostPort(r.RemoteAddr)
}
return ip
}
// Generate JSON output
func jsonify(w http.ResponseWriter, r *http.Request, payload *payload) {
callback := r.URL.Query().Get("callback")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
if json, err := json.Marshal(payload); err == nil {
if callback != "" {
io.WriteString(w, callback+"("+string(json)+");")
} else {
io.WriteString(w, string(json))
}
}
}