ex3_hellodrone.go 586 B

12345678910111213141516171819202122232425262728293031
  1. package main
  2. import (
  3. "net/http"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/drone/routes"
  7. )
  8. type API struct {
  9. Message string "json:message"
  10. }
  11. func Hello(w http.ResponseWriter, r *http.Request) {
  12. urlParams := r.URL.Query()
  13. name := urlParams.Get(":name")
  14. HelloMessage := "Hello, " + name
  15. message := API{Message:HelloMessage}
  16. output, err := json.Marshal(message)
  17. if err != nil {
  18. fmt.Println("Something went wrong", err)
  19. }
  20. fmt.Fprint(w, string(output))
  21. }
  22. func main() {
  23. mux := routes.New()
  24. mux.Get("/api/:name", Hello)
  25. http.Handle("/", mux)
  26. http.ListenAndServe(":8080", nil)
  27. }