38 lines
773 B
Go
38 lines
773 B
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"path/filepath"
|
|
"sync"
|
|
)
|
|
|
|
// templ represents a single template
|
|
type templateHandler struct {
|
|
once sync.Once
|
|
filename string
|
|
templ *template.Template
|
|
}
|
|
|
|
// ServeHTTP handles the HTTP server
|
|
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
t.once.Do(func() {
|
|
t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
|
|
})
|
|
t.templ.Execute(w, nil)
|
|
}
|
|
|
|
func NewTemplateHandler() *templateHandler {
|
|
return &templateHandler{filename: "chat.html"}
|
|
}
|
|
|
|
func main() {
|
|
http.Handle("/", &templateHandler{filename: "chat.html"})
|
|
|
|
// start the web server
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
log.Fatal("ListenAndServe:", err)
|
|
}
|
|
}
|