This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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