Skip to content

Commit 247ead4

Browse files
committed
refactored to mux router mode
1 parent 6dea37c commit 247ead4

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

main.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"net/http"
66

7+
"github.com/gorilla/mux"
78
_ "github.com/lib/pq"
89
)
910

@@ -15,16 +16,20 @@ import (
1516

1617
// }
1718

18-
type server struct{}
19-
20-
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
19+
func home(w http.ResponseWriter, r *http.Request) {
2120
w.Header().Set("Content-Type", "application/json")
22-
w.WriteHeader(http.StatusOK)
23-
w.Write([]byte(`{"message": "hello world"}`))
21+
switch r.Method {
22+
case "GET":
23+
w.WriteHeader(http.StatusOK)
24+
w.Write([]byte(`{"message": "get called"}`))
25+
case "POST":
26+
w.WriteHeader(http.StatusCreated)
27+
w.Write([]byte(`{"message": "post called"}`))
28+
}
2429
}
2530

2631
func main() {
27-
s := &server{}
28-
http.Handle("/", s)
29-
log.Fatal(http.ListenAndServe(":8080", nil))
32+
r := mux.NewRouter()
33+
r.HandleFunc("/", home)
34+
log.Fatal(http.ListenAndServe(":8080", r))
3035
}

0 commit comments

Comments
 (0)