-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
147 lines (126 loc) · 3.19 KB
/
http.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"net/http"
// "crypto/tls"
// "crypto/x509"
"fmt"
"io/ioutil"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"crypto/hmac"
"crypto/sha256"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const cfile = "/home/lka/dg/skopos/cfile.pem"
var m []byte
var dflt_qry string
type ApiHandler struct{}
func (ApiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var v string
var a, u bool
var data string
var http_status int
qry := r.URL.RawQuery
if qry == "" {
qry = dflt_qry
}
vals, _ := url.ParseQuery(qry) // map[string] []string ; NB: use vals.Get() to get the 1st value
http_status = 200
if v = vals.Get("busy"); v != "" {
t := time.Now()
c, _ := strconv.ParseUint(v, 10, 64)
k := make([]byte, 32)
mac := hmac.New(sha256.New, k)
for i := 0; i < int(c); i++ {
mac.Write(k)
mac.Write(k)
runtime.GC()
}
data += fmt.Sprintf("busy for %d us\n", time.Now().Sub(t)/1000)
}
if v = vals.Get("call"); v != "" {
if !strings.ContainsRune(v, '/') {
v = fmt.Sprintf("http://%s:8080/", v)
}
rsp, err := http.Get(v)
if err != nil {
data = "err: " + err.Error() + "\n"
http_status = 400
} else {
var d []byte
d, err = ioutil.ReadAll(rsp.Body) // TODO: err
data += "call: " + string(d)
}
}
if v = vals.Get("alloc"); v != "" {
a = true
}
if vals.Get("use") != "" {
u = true
}
if a {
var sz uint64
sz, _ = strconv.ParseUint(v, 10, 64)
//@err
m = nil
runtime.GC()
m = make([]byte, sz*4096)
data += fmt.Sprintf("allocated memory %d bytes (%d pages)\n", len(m), len(m)/4096)
}
if u {
var i int
for i = 0; i < len(m); i += 4096 {
m[i] = 1
}
data += fmt.Sprintf("accessed %d bytes (%d pages)\n", len(m), len(m)/4096)
}
w.Header().Add("Content-length", strconv.Itoa(len(data)))
w.Header().Add("Content-type", "text/plain")
w.WriteHeader(http_status)
w.Write([]byte(data))
}
func main() {
var s http.Server
var err error
s.Addr = ":8080"
if addr := os.Getenv("HTTP_ADDR"); addr != "" {
s.Addr = addr
}
// default query from command line
if len(os.Args) > 1 {
dflt_qry = os.Args[1]
}
/*
// We could call ListenAndServeTLS without initializing the TLS config, but we
// set it up explicitly here anyway, to allow adding things like client cert verify, etc. later
var tlscfg tls.Config
tlscfg.Certificates = make([]tls.Certificate, 1)
tlscfg.Certificates[0], err = tls.LoadX509KeyPair(cfile,cfile)
// @@ err
// pre-init the leaf cert, too (so it isn't parsed every time when serving requests)
tlscfg.Certificates[0].Leaf, err = x509.ParseCertificate(tlscfg.Certificates[0].Certificate[0])
// @@ err
s.TLSConfig = &tlscfg
*/
// Handler - default
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "api_requests_total",
Help: "A counter for requests to the wrapped handler.",
},
[]string{"code", "method"},
)
prometheus.MustRegister(counter)
apiHandlerFn := promhttp.InstrumentHandlerCounter(counter, ApiHandler{})
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", apiHandlerFn)
// err = s.ListenAndServeTLS("","")
runtime.GC()
err = s.ListenAndServe()
fmt.Println(err)
}