/
ui.go
44 lines (40 loc) · 1006 Bytes
/
ui.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
package app
import (
"html/template"
"io/ioutil"
"log"
"net/http"
"strings"
)
type Page struct {
Title string
App *Impl
Body []byte
}
func loadPage(title string) (*Page, error) {
body, err := ioutil.ReadFile(indexHTML)
if err != nil {
return nil, err
}
return &Page{Title: title, App: appImpl, Body: body}, nil
}
// Forward rpc requests to the rpc server and only handle http requests here.
func indexHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[:]
log.Printf("Handling request URL: %v", title)
if strings.Contains(strings.ToLower(title), "rpc") {
rpcServer.ServeHTTP(w, r)
} else {
/*p, err := loadPage(title)
if err != nil {
log.Printf("Error loading page: %v", err)
p = &Page{Title: title, App: appImpl, Body: make([]byte, 0)}
}*/
p := &Page{Title: title, App: appImpl, Body: make([]byte, 0)}
t, err := template.ParseFiles(indexHTML)
if err != nil {
log.Fatalf("Error parsing template file; err: %v", err)
}
t.Execute(w, p)
}
}