Skip to content

Commit 0e0a592

Browse files
committed
all: delay template loading to server boot time
1 parent 936eb45 commit 0e0a592

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"context"
99
"encoding/json"
10+
"html/template"
1011
"log"
1112
"net/http"
1213
)
@@ -21,7 +22,15 @@ type server struct {
2122
visitCh chan visit
2223
}
2324

25+
var (
26+
xTmpl *template.Template
27+
statsTmpl *template.Template
28+
)
29+
2430
func newServer(ctx context.Context) *server {
31+
xTmpl = template.Must(template.ParseFiles("public/x.html"))
32+
statsTmpl = template.Must(template.ParseFiles("public/stats.html"))
33+
2534
db, err := newStore(conf.Store)
2635
if err != nil {
2736
log.Fatalf("cannot establish connection to database, err: %v", err)

short.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13-
"html/template"
1413
"log"
1514
"net"
1615
"net/http"
@@ -25,9 +24,9 @@ type op string
2524
const (
2625
// opCreate represents a create operation for short link
2726
opCreate op = "create"
28-
// opDelete represents a create operation for short link
27+
// opDelete represents a delete operation for short link
2928
opDelete = "delete"
30-
// opUpdate represents a create operation for short link
29+
// opUpdate represents a update operation for short link
3130
opUpdate = "update"
3231
// opFetch represents a fetch operation for short link
3332
opFetch = "fetch"
@@ -149,8 +148,6 @@ func (s *server) readIP(r *http.Request) string {
149148
return ip
150149
}
151150

152-
var statsTmpl = template.Must(template.ParseFiles("public/stats.html"))
153-
154151
type arecords struct {
155152
Records []arecord
156153
}

x.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ package main
77
import (
88
"bytes"
99
"fmt"
10-
"html/template"
1110
"net/http"
1211
"strings"
1312
)
1413

15-
var xTmpl = template.Must(template.ParseFiles("public/x.html"))
16-
1714
type x struct {
1815
ImportRoot string
1916
VCS string

0 commit comments

Comments
 (0)