Browse Source

Ch1 pp56-57 Hello world with drone.io.

Frederic G. MARAND 8 years ago
parent
commit
47b56b1e03
1 changed files with 31 additions and 0 deletions
  1. 31 0
      ch1/ex3_hellodrone.go

+ 31 - 0
ch1/ex3_hellodrone.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"net/http"
+	"encoding/json"
+	"fmt"
+	"github.com/drone/routes"
+)
+
+type API struct {
+	Message string "json:message"
+}
+
+func Hello(w http.ResponseWriter, r *http.Request) {
+	urlParams := r.URL.Query()
+	name := urlParams.Get(":name")
+	HelloMessage := "Hello, " + name
+	message := API{Message:HelloMessage}
+	output, err := json.Marshal(message)
+	if err != nil {
+		fmt.Println("Something went wrong", err)
+	}
+	fmt.Fprint(w, string(output))
+}
+
+func main() {
+	mux := routes.New()
+	mux.Get("/api/:name", Hello)
+	http.Handle("/", mux)
+	http.ListenAndServe(":8080", nil)
+}