adds a /data path (#14)

This commit is contained in:
Gérald Croës
2018-10-29 14:25:39 +01:00
committed by Ludovic Fernandez
parent 2305eab132
commit 90c7fab968

63
app.go
View File

@ -1,9 +1,13 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"io"
"strconv"
"strings"
"sync" "sync"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -19,6 +23,14 @@ var cert string
var key string var key string
var port string var port string
const (
_ = iota
KB int64 = 1 << (10 * iota)
MB
GB
TB
)
func init() { func init() {
flag.StringVar(&cert, "cert", "", "give me a certificate") flag.StringVar(&cert, "cert", "", "give me a certificate")
flag.StringVar(&key, "key", "", "give me a key") flag.StringVar(&key, "key", "", "give me a key")
@ -32,6 +44,7 @@ var upgrader = websocket.Upgrader{
func main() { func main() {
flag.Parse() flag.Parse()
http.HandleFunc("/data", dataHandler)
http.HandleFunc("/echo", echoHandler) http.HandleFunc("/echo", echoHandler)
http.HandleFunc("/bench", benchHandler) http.HandleFunc("/bench", benchHandler)
http.HandleFunc("/", whoami) http.HandleFunc("/", whoami)
@ -74,7 +87,41 @@ func echoHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
} }
func dataHandler(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(r.URL.String())
queryParams := u.Query()
size, err := strconv.ParseInt(queryParams.Get("size"), 10, 64)
if err != nil {
size = 1
}
if size < 0 {
size = 0
}
unit := queryParams.Get("unit")
switch (strings.ToLower(unit)) {
case "kb": size = size * KB
case "mb": size = size * MB
case "gb": size = size * GB
case "tb": size = size * TB
}
attachment, err := strconv.ParseBool(queryParams.Get("attachment"))
if err != nil {
attachment = false
}
content := fillContent(size)
if attachment {
const name = "data.txt"
w.Header().Add("Content-Disposition", "Attachment")
http.ServeContent(w, r, name, time.Now(), content)
} else {
io.Copy(w, content)
}
}
func whoami(w http.ResponseWriter, req *http.Request) { func whoami(w http.ResponseWriter, req *http.Request) {
u, _ := url.Parse(req.URL.String()) u, _ := url.Parse(req.URL.String())
queryParams := u.Query() queryParams := u.Query()
@ -161,3 +208,19 @@ func healthHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(currentHealthState.StatusCode) w.WriteHeader(currentHealthState.StatusCode)
} }
} }
func fillContent(length int64) io.ReadSeeker {
charset := "-ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, length, length)
for i := range b {
b[i] = charset[i % len(charset)]
}
if length > 0 {
b[0] = '|'
b[length-1] = '|'
}
return bytes.NewReader(b)
}