PracowniaProgramowania/backend/serverTest2.go

37 lines
924 B
Go
Raw Normal View History

2018-11-14 00:19:38 +01:00
package main
import (
"html/template"
"log"
"net/http"
"time"
)
type PageVariables struct {
Date string
Time string
}
func main() {
http.HandleFunc("/", HomePage)
log.Fatal(http.ListenAndServe(":8081", nil))
}
func HomePage(w http.ResponseWriter, r *http.Request) {
now := time.Now() // find the time right now
HomePageVars := PageVariables{ //store the date and time in a struct
Date: now.Format("02-01-2006"),
Time: now.Format("15:04:05"),
}
t, err := template.ParseFiles("homepage.html") //parse the html file homepage.html
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, HomePageVars) //execute the template and pass it the HomePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}