-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
53 lines (43 loc) · 1.08 KB
/
requests.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
)
func main() {
server()
}
func simpleRequest() {
response, _ := http.Get("https://google.com")
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("Done!\n", body[0])
}
func handler() {
http.HandleFunc("/welcome", func(response http.ResponseWriter, request *http.Request) {
fmt.Println("Welcome!", request.URL.RawPath)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
func server() {
server := http.Server{
Addr: "127.0.0.1:7000",
Handler: serverHandler(),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Println("Listening at " + server.Addr)
err := server.ListenAndServe()
log.Println("Error Log >>", err)
}
func serverHandler() http.HandlerFunc {
var b = []byte{1, 2, 43, 54, 254, 44, 255, 58}
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
request.Write(io.MultiWriter(io.Writer(b)))
log.Println("Request Log >>", request.Host, request.Method)
})
}