Skip to content

Instantly share code, notes, and snippets.

@kgoralski kgoralski/basic_rest_server.go Secret
Created Nov 14, 2016

Embed
What would you like to do?
basic_rest_server
package main
import (
"encoding/json"
"log"
"net/http"
)
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
}
type People []Person
func getPersonByIDHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
people := People{
Person{ID: 1, Name: "Janusz"},
Person{ID: 2, Name: "Bożena"},
}
if err := json.NewEncoder(w).Encode(people); err != nil {
http.Error(w, "something went wrong", http.StatusInternalServerError)
}
}
func main() {
startServer()
}
func startServer() {
http.HandleFunc("/rest/people/", getPersonByIDHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.