This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
forked from yeo/ec2.shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (120 loc) · 4.02 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"errors"
"fmt"
"html/template"
"io"
"net/http"
"os"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
// Add global methods if data is a map
if viewContext, isMap := data.(map[string]interface{}); isMap {
viewContext["reverse"] = c.Echo().Reverse
}
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
debug := os.Getenv("DEBUG") == "1"
s := NewSpotPriceCrawler()
s.Run()
p := &PriceFinder{
SpotPriceFinder: s,
}
p.Load()
// Echo instance
e := echo.New()
t := &Template{
templates: template.Must(template.ParseGlob("views/*.html")),
}
e.Renderer = t
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/static", "static")
// Routes
e.GET("/", GetPriceHandler(debug, p))
// Start server
listen_on := os.Getenv("BIND_TO")
if listen_on == "" {
listen_on = "127.0.0.1:6001"
}
e.Logger.Fatal(e.Start(listen_on))
}
func GetPriceHandler(debug bool, p *PriceFinder) func(echo.Context) error {
header := "%-15s %-12s %4s vCPUs %-20s %-18s %-10s %-10s %-10s\n"
pattern := "%-15s %-12s %4d vCPUs %-20s %-18s %-10.4f %-10.3f %-10s\n"
ts := time.Now()
return func(c echo.Context) error {
c.Response().Header().Set("Cache-Control", "public, max-age=300, stale-while-revalidate=60, stale-if-error=10800")
if debug {
ts = time.Now()
}
prices := p.PriceListFromRequest(c)
if prices == nil {
return errors.New("Invalid region")
}
if IsJson(c) {
friendlyPrices := &FriendlyPriceResponse{
Prices: make([]*FriendlyPrice, len(prices)),
}
for i, v := range prices {
friendlyPrices.Prices[i] = &FriendlyPrice{
InstanceType: v.Attribute.EC2InstanceType,
Memory: v.Attribute.EC2Memory,
VCPUS: v.Attribute.EC2VCPU,
Storage: v.Attribute.EC2Storage,
Network: v.Attribute.EC2NetworkPerformance,
Cost: v.Price,
MonthlyPrice: v.MonthlyPrice(),
SpotPrice: v.FormatSpotPrice(),
}
}
return c.JSON(http.StatusOK, friendlyPrices)
}
if IsText(c) {
// When loading by shell we can pass these param
priceText := ""
//priceText += "┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐\n"
priceText += fmt.Sprintf(header,
"Instance Type",
"Memory",
"",
"Storage",
"Network",
"Price",
"Monthly",
"Spot Price")
for _, price := range prices {
m := price.Attribute
//priceText += "├──────────────────────────────────────────────────────────────────────────────────────────────────────┤\n"
priceText += fmt.Sprintf(pattern,
m.EC2InstanceType,
m.EC2Memory,
m.EC2VCPU,
m.EC2Storage,
m.EC2NetworkPerformance,
price.Price,
price.MonthlyPrice(),
price.FormatSpotPrice())
}
//priceText += "└──────────────────────────────────────────────────────────────────────────────────────────────────────┘\n"
return c.String(http.StatusOK, priceText)
}
currentRegion := "us-east-1"
if region := c.QueryParam("region"); region != "" {
currentRegion = region
}
return c.Render(http.StatusOK, "index.html", map[string]interface{}{
"ts": ts,
"priceData": prices,
"currentRegion": currentRegion,
})
}
}