Created
September 3, 2015 17:16
-
-
Save eduardonunesp/0bdbecbfc0c43153fa45 to your computer and use it in GitHub Desktop.
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" | |
"github.com/codegangsta/negroni" | |
"github.com/gorilla/mux" | |
"log" | |
"net/http" | |
"strconv" | |
) | |
const ( | |
PORT int = 9000 | |
HOST string = "localhost" | |
) | |
type Item struct { | |
Name string `json:"name"` | |
Done bool `json:"done"` | |
} | |
type List struct { | |
Name string `json:"name"` | |
Items []Item `json:"items"` | |
} | |
func main() { | |
log.Printf("Startup on %s:%d", HOST, PORT) | |
router := mux.NewRouter() | |
router.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { | |
list := &List{ | |
"Todos", []Item{ | |
{"Clean the house", false}, | |
{"Feed the dog", false}, | |
{"Check mailbox", false}, | |
}, | |
} | |
w.Header().Set("Content-Type", "application/json; charset=utf-8") | |
json.NewEncoder(w).Encode(list) | |
}) | |
n := negroni.Classic() | |
n.UseHandler(router) | |
n.Run(HOST + ":" + strconv.Itoa(PORT)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment