| 1234567891011121314151617181920212223242526272829303132333435 |
- package server
- import (
- "encoding/json"
- "fmt"
- "net/http"
- )
- func index(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- w.WriteHeader(http.StatusMethodNotAllowed)
- fmt.Fprintf(w, "method not allowed")
- return
- }
- fmt.Fprintf(w, "Hellow there %s", "visitor")
- }
- func getCountries(w http.ResponseWriter, _ *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(countries)
- }
- func addCountry(w http.ResponseWriter, r *http.Request) {
- country := &Country{}
- err := json.NewDecoder(r.Body).Decode(country)
- if err != nil {
- w.WriteHeader(http.StatusBadRequest)
- fmt.Fprintf(w, "%v", err)
- return
- }
- countries = append(countries, country)
- fmt.Fprintf(w, "country was added")
- }
|