-
-
Save kgoralski/e197863f6b201a9790914e5158cdfbbc to your computer and use it in GitHub Desktop.
basic_rest_server
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" | |
"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