Go net/http server snippet

## 最小

```go
package main

import (
	"net/http"
	"log"
	"fmt"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello")
}

func main() {
    http.HandleFunc("/", hello)
 
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}
```

## 最小 (mux)

```go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello")
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", hello)

	if err := http.ListenAndServe(":8080", mux); err != nil {
		log.Fatal(err)
	}
}

```

## gorilla/mux

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello")
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", hello)

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatal(err)
	}
}

```

## gorilla/mux Method指定

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello")
}

func main() {
	r := mux.NewRouter()
	r.Methods("GET").Path("/").HandlerFunc(hello)

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatal(err)
	}
}

```

## query

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello\n")
	q := r.URL.Query()
	if name, ok := q["name"]; ok {
		fmt.Fprintf(w, name[0])
	}
}

func main() {
	r := mux.NewRouter()
	r.Methods("GET").Path("/").HandlerFunc(hello)

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatal(err)
	}
}

```

## gorilla/mux path parameter

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func greeting(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	fmt.Fprintf(w, vars["greeting"])
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/{greeting}", greeting)

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatal(err)
	}
}

```

## POST form

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func postHello(w http.ResponseWriter, r *http.Request) {
	name := r.FormValue("name")
	fmt.Fprintf(w, "hello, %v", name)
}

func main() {
	r := mux.NewRouter()
	r.Methods("POST").Path("/").HandlerFunc(postHello)

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatal(err)
	}
}
```

## POST json

```go
package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

type Hello struct {
    Name string `json="name"`
}

func parseJsonRequest(w http.ResponseWriter, r *http.Request, data interface{}) (int, error) {
    //Validate request
    if r.Method != "POST" {
        return http.StatusBadRequest, fmt.Errorf("Bad method")
    }

    if r.Header.Get("Content-Type") != "application/json" {
        return http.StatusBadRequest, fmt.Errorf("Bad content type")
    }
	
	decoder := json.NewDecoder(r.Body)
    if err := decoder.Decode(&data); err != nil {
        return http.StatusInternalServerError, err      
    }
    return http.StatusOK, nil
}

func postHello(w http.ResponseWriter, r *http.Request) {
    data := Hello{}
    status, err := parseJsonRequest(w, r, &data)
    if err != nil {
        w.WriteHeader(status)
        return
    }
    w.WriteHeader(status)

    encoder := json.NewEncoder(w)
    encoder.Encode(data)
}

func main() {
    r := mux.NewRouter()
    r.Methods("POST").Path("/").HandlerFunc(postHello)

    if err := http.ListenAndServe(":9999", r); err != nil {
        log.Fatal(err)
    }
}


```

## Handler (ServeHTTP)

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

type Greeting struct {
	message string
}

func (g *Greeting) ServeHTTP (w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, g.message)
}

func main() {
	hello := Greeting{message: "hello"}
	r := mux.NewRouter()
	r.Methods("GET").Path("/").Handler(&hello)

	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatal(err)
	}
}
```