Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/webserver/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
</head>
<body><div class="c">
<h1>TinyGo HTTP Server</h1>
<p>access: {{ACCESS}}</p>

<ul class="links">
<li><a href="/hello">/hello</a></li>
<li><a href="/6">/6 (Tetris)</a></li>
Expand Down
51 changes: 3 additions & 48 deletions examples/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ var indexHTML string
//go:embed sixlines.html
var sixlinesHTML string

// indexBefore and indexAfter are the two halves of index.html split at the
// access counter insertion point. Populated by init().
var indexBefore, indexAfter string

const accessMarker = "{{ACCESS}}"

func init() {
for i := 0; i <= len(indexHTML)-len(accessMarker); i++ {
if indexHTML[i:i+len(accessMarker)] == accessMarker {
indexBefore = indexHTML[:i]
indexAfter = indexHTML[i+len(accessMarker):]
return
}
}
indexBefore = indexHTML
}

var (
ssid string
password string
Expand All @@ -49,10 +32,7 @@ func main() {
// wait a bit for serial
time.Sleep(2 * time.Second)

link := link.Esplink{
// link needs a 48k arena pool to handle the blob allocations for WiFi.
ArenaPoolSize: 48 * 1024,
}
link := link.Esplink{}
netdev.UseNetdev(&link)

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

func root(w http.ResponseWriter, r *http.Request) {
access := 1

cookie, err := r.Cookie("access")
if err != nil {
if err == http.ErrNoCookie {
cookie = &http.Cookie{
Name: "access",
Value: "1",
}
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
} else {
v, err := strconv.ParseInt(cookie.Value, 10, 0)
if err != nil {
http.Error(w, "invalid cookie.Value : "+cookie.Value, http.StatusBadRequest)
return
}
cookie.Value = strconv.Itoa(int(v) + 1)
access = int(v) + 1
}
http.SetCookie(w, cookie)
w.WriteHeader(http.StatusOK)

io.WriteString(w, indexBefore)
io.WriteString(w, strconv.Itoa(access))
io.WriteString(w, indexAfter)
io.WriteString(w, indexHTML)
}

func sixlines(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, sixlinesHTML)
}

Expand Down
Loading