-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
40 lines (33 loc) · 1.02 KB
/
main.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
// main.go
package main
import (
"html/template"
"log"
"net/http"
)
// Parse the templates once to avoid re-parsing on every request
var templates = template.Must(template.ParseGlob("templates/*.html"))
// RenderTemplate renders a specific template
func RenderTemplate(w http.ResponseWriter, tmpl string) {
err := templates.ExecuteTemplate(w, tmpl+".html", nil)
if err != nil {
http.Error(w, "Unable to load template", http.StatusInternalServerError)
log.Println("Template rendering error:", err)
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
RenderTemplate(w, "home")
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
RenderTemplate(w, "about")
}
func main() {
http.HandleFunc("/", homeHandler) // Route for home page
http.HandleFunc("/about", aboutHandler) // Route for about page
// Corrected log message to reflect the correct port
log.Println("Server starting on :8081...")
err := http.ListenAndServe(":8081", nil)
if err != nil {
log.Fatal("Server failed to start:", err)
}
}