-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (100 loc) · 2.57 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"embed"
"errors"
"fmt"
"image"
"image/color"
"image/png"
"net/http"
"os"
"github.com/DemmyDemon/line-go-up/eodhd"
"github.com/DemmyDemon/line-go-up/labelimage"
"github.com/golang/freetype/truetype"
)
const (
refPoint = 237.000
ISIN = "SE0001192618"
)
var (
BadColor = color.RGBA{128, 60, 60, 255}
GoodColor = color.RGBA{60, 128, 60, 255}
)
var (
//go:embed res
res embed.FS
)
func main() {
font, err := ReadFont(res, "res/White Rabbit.ttf")
if err != nil {
fmt.Printf("Unable to load font: %s\n", err)
}
http.HandleFunc("/Handelsbanken_Multi_Asset_50.png",
func(w http.ResponseWriter, r *http.Request) {
getImage(w, r, font)
})
err = http.ListenAndServe(":2468", nil)
if err != nil {
if errors.Is(err, http.ErrServerClosed) {
fmt.Println("Server closed")
} else {
fmt.Printf("Server error: %s\n", err)
os.Exit(1)
}
}
}
func ReadFont(fs embed.FS, fontPath string) (*truetype.Font, error) {
fontBytes, err := fs.ReadFile(fontPath)
if err != nil {
return nil, fmt.Errorf("reading font: %w", err)
}
f, err := truetype.Parse(fontBytes)
if err != nil {
return nil, fmt.Errorf("parsing font: %w", err)
}
return f, nil
}
func getImage(w http.ResponseWriter, r *http.Request, font *truetype.Font) {
results, err := eodhd.Search(ISIN, os.Getenv("API_TOKEN"))
if err != nil {
fmt.Printf("Error fetching oedhd data: %s\n", err)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if len(results) == 0 {
fmt.Println("No data found, somehow")
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Malformed data"))
return
}
result := results[0]
offsetPercent, good := getOffsetPercent(result.PreviousClose)
textColor := BadColor
if good {
textColor = GoodColor
}
lines := []string{
result.PreviousCloseDate,
fmt.Sprintf("%s%0.3f", result.Currency, result.PreviousClose),
offsetPercent,
}
img := labelimage.CreateWithFont(image.Rect(0, 0, 192, 64), font, textColor, lines, true, true)
w.WriteHeader(http.StatusOK)
err = png.Encode(w, img)
if err != nil {
fmt.Printf("Error writing PNG: %s\n", err)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Add("Content-Type", "image/png")
}
func getOffsetPercent(currentValue float64) (string, bool) {
diff := currentValue - refPoint
offset := (diff / refPoint) * 100
return fmt.Sprintf("%+0.3f%%", offset), offset > 0
}