Skip to content

Instantly share code, notes, and snippets.

@zeayes
Created April 12, 2014 15:44
Show Gist options
  • Save zeayes/10542175 to your computer and use it in GitHub Desktop.
Save zeayes/10542175 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"strconv"
"strings"
"io/ioutil"
"net/http"
"net/url"
"encoding/json"
"github.com/codegangsta/martini"
)
func JsonRequest(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.Header.Get("Content-Type") == "application/json" {
args := url.Values{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
data := make(map[string]interface{})
err = json.Unmarshal(body, &data)
if err != nil {
return
}
for key, value := range data {
switch value.(type) {
case string:
args.Add(key, value.(string))
case float64:
args.Add(key, strconv.FormatFloat(value.(float64), 'f', 0, 64))
}
}
r.Form = args
} else {
r.ParseForm()
}
}
func main() {
m := martini.Classic()
m.Use(JsonRequest)
m.Use(func(res http.ResponseWriter, req *http.Request, ctx martini.Context) {
start := time.Now()
rw := martini.NewResponseWriter(res)
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Connection", "close")
ctx.MapTo(rw, (*http.ResponseWriter)(nil))
ctx.Next()
fmt.Println("consume time: ", time.Since(start)/time.Microsecond)
bucket := strings.Trim(strings.Replace(req.URL.Path, "/", ".", -1), ".")
fmt.Println("url path", bucket)
//client.Timing(bucket, time.Since(start)/time.Microsecond)
})
http.ListenAndServe("127.0.0.1:9000", m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment