-
Notifications
You must be signed in to change notification settings - Fork 0
/
nethttp.go
30 lines (25 loc) · 830 Bytes
/
nethttp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Package handlercomp implements several popular web frameworks based on net/http
// to compare their performance.
package handlercomp
import (
"net/http"
)
// NewHTTPServer creates a new http.Server instance using DefaultServerMux as the handler.
//
// addr: address the server should run on (Ex: ":8080").
func NewHTTPServer(addr string) *http.Server {
// Use default server mutex as the handler.
mux := http.NewServeMux()
// Register route.
mux.HandleFunc(http.MethodGet+" /user/{id}", httpHandler)
// Return a new http.Server.
return &http.Server{
Addr: addr,
Handler: mux,
}
}
// httpHandler handles responses for the net/http server.
func httpHandler(w http.ResponseWriter, r *http.Request) {
resString := responseBody(r.PathValue("id"), r.URL.Query().Get("name"))
_, _ = w.Write([]byte(resString))
}