udemy-go-web-1/cmd/hello-world/main.go

38 lines
805 B
Go
Raw Normal View History

2024-06-26 15:30:39 +00:00
package main
import (
"fmt"
2024-06-26 16:10:56 +00:00
"html/template"
2024-06-26 15:30:39 +00:00
"net/http"
)
2024-06-26 15:41:50 +00:00
const portNumber = ":8080"
// Home is the about page handler
func Home(w http.ResponseWriter, r *http.Request) {
2024-06-26 16:10:56 +00:00
renderTemplate(w, "home.page.tmpl")
2024-06-26 15:41:50 +00:00
}
// About is the about page handler
func About(w http.ResponseWriter, r *http.Request) {
2024-06-26 16:10:56 +00:00
renderTemplate(w, "about.page.tmpl")
2024-06-26 15:41:50 +00:00
}
2024-06-26 16:10:56 +00:00
func renderTemplate(w http.ResponseWriter, tmpl string) {
parsedTemplate, _ := template.ParseFiles("../..//templates/" + tmpl)
err := parsedTemplate.Execute(w, nil)
2024-06-26 15:49:08 +00:00
if err != nil {
2024-06-26 16:10:56 +00:00
fmt.Println("error parsing template:", err)
2024-06-26 15:49:08 +00:00
}
}
2024-06-26 15:41:50 +00:00
// main is the main application function
2024-06-26 15:30:39 +00:00
func main() {
2024-06-26 15:41:50 +00:00
http.HandleFunc("/", Home)
http.HandleFunc("/about", About)
fmt.Printf("Starting application on port %s\n", portNumber)
_ = http.ListenAndServe(portNumber, nil)
2024-06-26 15:30:39 +00:00
}