Skip to content

Commit caa49ff

Browse files
committed
examples: improve webserver example to use less memory
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent f593c74 commit caa49ff

2 files changed

Lines changed: 3 additions & 50 deletions

File tree

examples/webserver/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
</head>
1616
<body><div class="c">
1717
<h1>TinyGo HTTP Server</h1>
18-
<p>access: {{ACCESS}}</p>
19-
2018
<ul class="links">
2119
<li><a href="/hello">/hello</a></li>
2220
<li><a href="/6">/6 (Tetris)</a></li>

examples/webserver/webserver.go

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,6 @@ var indexHTML string
2222
//go:embed sixlines.html
2323
var sixlinesHTML string
2424

25-
// indexBefore and indexAfter are the two halves of index.html split at the
26-
// access counter insertion point. Populated by init().
27-
var indexBefore, indexAfter string
28-
29-
const accessMarker = "{{ACCESS}}"
30-
31-
func init() {
32-
for i := 0; i <= len(indexHTML)-len(accessMarker); i++ {
33-
if indexHTML[i:i+len(accessMarker)] == accessMarker {
34-
indexBefore = indexHTML[:i]
35-
indexAfter = indexHTML[i+len(accessMarker):]
36-
return
37-
}
38-
}
39-
indexBefore = indexHTML
40-
}
41-
4225
var (
4326
ssid string
4427
password string
@@ -49,10 +32,7 @@ func main() {
4932
// wait a bit for serial
5033
time.Sleep(2 * time.Second)
5134

52-
link := link.Esplink{
53-
// link needs a 48k arena pool to handle the blob allocations for WiFi.
54-
ArenaPoolSize: 48 * 1024,
55-
}
35+
link := link.Esplink{}
5636
netdev.UseNetdev(&link)
5737

5838
println("Connecting to WiFi...")
@@ -88,37 +68,12 @@ func logRequest(h http.HandlerFunc) http.Handler {
8868
}
8969

9070
func root(w http.ResponseWriter, r *http.Request) {
91-
access := 1
92-
93-
cookie, err := r.Cookie("access")
94-
if err != nil {
95-
if err == http.ErrNoCookie {
96-
cookie = &http.Cookie{
97-
Name: "access",
98-
Value: "1",
99-
}
100-
} else {
101-
http.Error(w, err.Error(), http.StatusBadRequest)
102-
return
103-
}
104-
} else {
105-
v, err := strconv.ParseInt(cookie.Value, 10, 0)
106-
if err != nil {
107-
http.Error(w, "invalid cookie.Value : "+cookie.Value, http.StatusBadRequest)
108-
return
109-
}
110-
cookie.Value = strconv.Itoa(int(v) + 1)
111-
access = int(v) + 1
112-
}
113-
http.SetCookie(w, cookie)
11471
w.WriteHeader(http.StatusOK)
115-
116-
io.WriteString(w, indexBefore)
117-
io.WriteString(w, strconv.Itoa(access))
118-
io.WriteString(w, indexAfter)
72+
io.WriteString(w, indexHTML)
11973
}
12074

12175
func sixlines(w http.ResponseWriter, r *http.Request) {
76+
w.WriteHeader(http.StatusOK)
12277
io.WriteString(w, sixlinesHTML)
12378
}
12479

0 commit comments

Comments
 (0)