Skip to content

Instantly share code, notes, and snippets.

@alexrios
Created August 8, 2016 13:05
Show Gist options
  • Save alexrios/5a61e45e3bd3925fdfc9cc53c1f7b41c to your computer and use it in GitHub Desktop.
Save alexrios/5a61e45e3bd3925fdfc9cc53c1f7b41c to your computer and use it in GitHub Desktop.
package main
import (
"io/ioutil"
"log"
"net/http"
"time"
"io"
"flag"
"strconv"
)
var delay, statusCode, port int
var jsonData,uri string
func main() {
flag.IntVar(&delay, "delay", 100, "delay in ms")
flag.StringVar(&jsonData, "response", "{}", "json response")
flag.IntVar(&statusCode, "code", http.StatusOK, "status code response")
flag.IntVar(&port, "port", 8080, "Http listen port")
flag.StringVar(&uri, "uri", "/", "URI to handle")
flag.Parse()
http.HandleFunc(uri, service)
http.ListenAndServe(":" + strconv.Itoa(port), nil)
}
func service(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
log.Println("request: " + string(body))
w.Header().Set("Content-Type: application/json", "*")
w.WriteHeader(statusCode)
time.Sleep(time.Duration(delay) * time.Millisecond)
io.WriteString(w, jsonData)
log.Println("response: [" + strconv.Itoa(statusCode) + "]")
log.Println("response: [" + string(jsonData) + "]")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment