-
Notifications
You must be signed in to change notification settings - Fork 4
/
plotly05.go
66 lines (56 loc) · 1.5 KB
/
plotly05.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
54
55
56
57
58
59
60
61
62
63
64
65
66
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Padesátá první část
// Tvorba grafů v jazyce Go: kreslení ve webovém klientu
// https://www.root.cz/clanky/tvorba-grafu-v-jazyce-go-kresleni-ve-webovem-klientu/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z padesáté první části:
// https://github.com/tisnik/go-root/blob/master/article_51/README.md
package main
import (
"encoding/json"
"log"
"math"
"net/http"
)
type Points struct {
X []float64 `json:"x"`
Y []float64 `json:"y"`
}
const npoints = 100
const periods = 3
func makePoints(npoints uint) Points {
var points Points
points.X = make([]float64, npoints)
points.Y = make([]float64, npoints)
for i := uint(0); i < npoints; i++ {
t := float64(i) * periods * 2.0 * math.Pi / float64(npoints)
points.X[i] = t
// limita
if t == 0.0 {
points.Y[i] = 1.0
} else {
points.Y[i] = math.Sin(t) / t
}
}
return points
}
func dataHandler(writer http.ResponseWriter, request *http.Request) {
points := makePoints(npoints)
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
json.NewEncoder(writer).Encode(points)
}
func startHttpServer(address string) {
log.Printf("Starting server on address %s", address)
http.Handle("/", http.FileServer(http.Dir("plotly05/")))
http.HandleFunc("/data", dataHandler)
http.ListenAndServe(address, nil)
}
func main() {
startHttpServer(":8080")
}