Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created July 25, 2014 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkdai/7a2edf58fcbbf5bc1ce4 to your computer and use it in GitHub Desktop.
Save kkdai/7a2edf58fcbbf5bc1ce4 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
"os"
)
type Payload struct {
Staff Data
}
type Data struct {
Fruit Fruits
Vaggies Vagetables
}
type Fruits map[string]int
type Vagetables map[string]int
func main() {
//init gorilla/mux
m := mux.NewRouter()
// Get all lists.
m.HandleFunc("/", GetAllLists).Methods("GET")
// route http to mux
http.Handle("/", m)
fmt.Println("listening...")
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic(err)
}
}
func GetAllLists(w http.ResponseWriter, r *http.Request) {
response, err := getJSONResponse()
if err != nil {
panic(err)
}
fmt.Fprint(w, string(response))
}
func getJSONResponse() ([]byte, error) {
fruits := make(map[string]int)
fruits["Apple"] = 2
fruits["Tomato"] = 3
vagetables := make(map[string]int)
vagetables["Pappers"] = 5
d := Data{fruits, vagetables}
p := Payload{d}
return json.MarshalIndent(p, "", " ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment