35 lines
782 B
Go
35 lines
782 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var index_template *template.Template
|
||
|
|
||
|
type indexData struct {
|
||
|
Time string
|
||
|
}
|
||
|
|
||
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
||
|
//if index_template == nil {
|
||
|
// tmpl, err := template.ParseFiles("templates/index.html")
|
||
|
// if err != nil {
|
||
|
// http.Error(w, "Couldn't parse template file", http.StatusInternalServerError)
|
||
|
// return
|
||
|
// }
|
||
|
// index_template = tmpl
|
||
|
//}
|
||
|
|
||
|
tmpl, err := template.ParseFiles("templates/index.html")
|
||
|
if err != nil {
|
||
|
http.Error(w, "Couldn't parse template file", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
err = tmpl.Execute(w, indexData{Time: time.Now().Format(time.Stamp)})
|
||
|
if err != nil {
|
||
|
http.Error(w, "Failed to execute template", http.StatusInternalServerError)
|
||
|
}
|
||
|
}
|