Skip to content

Instantly share code, notes, and snippets.

@perigee
Created March 20, 2017 23:07
Show Gist options
  • Save perigee/4f479499a59d3563b51cfa007e59d2b1 to your computer and use it in GitHub Desktop.
Save perigee/4f479499a59d3563b51cfa007e59d2b1 to your computer and use it in GitHub Desktop.
Simple Go restful server
package main
import (
"encoding/json"
"log"
"net/http"
"flag"
"github.com/golang/glog"
"github.com/gorilla/mux"
)
type Sample struct {
Output string `json:"output"`
}
func CreateResult(w http.ResponseWriter, req *http.Request) {
var s Sample
_ = json.NewDecoder(req.Body).Decode(&s)
glog.Infof("receive: %v", s.Output)
}
func main() {
flag.Parse()
//logger := log.New(os.Stdout, "terraform: ", log.Lshortfile|log.LstdFlags)
router := mux.NewRouter()
router.HandleFunc("/result", CreateResult).Methods("PUT")
log.Fatal(http.ListenAndServe(":8089", router))
}
@perigee
Copy link
Author

perigee commented Mar 20, 2017

Put json to endpoints

curl -H "Content-Type:application/json" -X PUT -d @tmp.json http://localhost:8089/result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment