ソースを参照

httprouter demo

Frederic G. MARAND 5 年 前
コミット
2b9a3fdb3a
4 ファイル変更41 行追加0 行削除
  1. 1 0
      httprouter/.gitignore
  2. 8 0
      httprouter/go.mod
  3. 4 0
      httprouter/go.sum
  4. 28 0
      httprouter/httprouter.go

+ 1 - 0
httprouter/.gitignore

@@ -0,0 +1 @@
+httprouter

+ 8 - 0
httprouter/go.mod

@@ -0,0 +1,8 @@
+module code.osinet.fr/fgm/tooling/httprouter
+
+go 1.13
+
+require (
+	github.com/julienschmidt/httprouter v1.2.0
+	github.com/sanity-io/litter v1.1.0
+)

+ 4 - 0
httprouter/go.sum

@@ -0,0 +1,4 @@
+github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
+github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
+github.com/sanity-io/litter v1.1.0 h1:BllcKWa3VbZmOZbDCoszYLk7zCsKHz5Beossi8SUcTc=
+github.com/sanity-io/litter v1.1.0/go.mod h1:CJ0VCw2q4qKU7LaQr3n7UOSHzgEMgcGco7N/SkZQPjw=

+ 28 - 0
httprouter/httprouter.go

@@ -0,0 +1,28 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"net/http"
+
+	"github.com/julienschmidt/httprouter"
+	"github.com/sanity-io/litter"
+)
+
+func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
+	fmt.Fprint(w, "Welcome!\n")
+}
+
+func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	c := httprouter.ParamsFromContext(r.Context()) // nil ?
+	fmt.Fprintln(w, litter.Sdump(c))
+	fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
+}
+
+func main() {
+	router := httprouter.New()
+	router.GET("/", Index)
+	router.GET("/hello/:name", Hello)
+
+	log.Fatal(http.ListenAndServe(":8080", router))
+}