/
web_server.go
46 lines (39 loc) · 1.12 KB
/
web_server.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
package server
import (
"fmt"
"github.com/abondar24/GoBase/basic"
"github.com/abondar24/GoBase/lissajous"
"log"
"net/http"
"sync"
)
var count int
var mu sync.Mutex
func WebServer() {
http.Handle("/", http.FileServer(http.Dir("./../server/html")))
http.HandleFunc("/headers",simpleHandler)
http.HandleFunc("/count", counter)
http.HandleFunc("/lissajous", func(w http.ResponseWriter, r *http.Request) { lissajous.Lissajous(w) })
http.HandleFunc("/fractal", func(w http.ResponseWriter, r *http.Request) { basic.DrawFractal(w) })
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
//handler func
func simpleHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s %s %s/n", r.Method, r.URL, r.Proto)
for k, v := range r.Header {
fmt.Fprintf(w, "Header[%q]=%q\n", k, v)
}
fmt.Fprintf(w, "Host = %q\n", r.Host)
fmt.Fprintf(w, "RemoteAddr = %q\n", r.RemoteAddr)
if err := r.ParseForm(); err != nil {
log.Print(err)
}
for k, v := range r.Form {
fmt.Fprintf(w, "Form[%q] =%q\n", k, v)
}
}
func counter(w http.ResponseWriter, r *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count %d\n", count)
mu.Unlock()
}