Reorg code, add basic styling to pages

This commit is contained in:
Muyao CHEN 2024-06-26 21:56:58 +02:00
parent 92dc9975c7
commit b207b9a293
5 changed files with 53 additions and 23 deletions

15
handlers.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"net/http"
)
// Home is the about page handler
func Home(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "home.page.tmpl")
}
// About is the about page handler
func About(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "about.page.tmpl")
}

19
main.go
View File

@ -2,30 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"net/http" "net/http"
) )
const portNumber = ":8080" const portNumber = ":8080"
// Home is the about page handler
func Home(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "home.page.tmpl")
}
// About is the about page handler
func About(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "about.page.tmpl")
}
func renderTemplate(w http.ResponseWriter, tmpl string) {
parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl)
err := parsedTemplate.Execute(w, nil)
if err != nil {
fmt.Println("error parsing template:", err)
}
}
// main is the main application function // main is the main application function
func main() { func main() {
http.HandleFunc("/", Home) http.HandleFunc("/", Home)

16
render.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"html/template"
"net/http"
)
// renderTemplate renders a template file
func renderTemplate(w http.ResponseWriter, tmpl string) {
parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl)
err := parsedTemplate.Execute(w, nil)
if err != nil {
fmt.Println("error parsing template:", err)
}
}

View File

@ -4,12 +4,21 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title></title> <title>About page</title>
<link href="css/style.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head> </head>
<body> <body>
<h1>This is the about page</h1> <div class="container">
<div class="row">
<div class="col">
<h1>This is the about page</h1>
<p>This is the content</p>
</div>
</div>
</div>
</body> </body>
</html> </html>

View File

@ -4,12 +4,21 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title></title> <title>Home page</title>
<link href="css/style.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head> </head>
<body> <body>
<h1>This is the home page</h1> <div class="container">
<div class="row">
<div class="col">
<h1>This is the home page</h1>
<p>This is the content</p>
</div>
</div>
</div>
</body> </body>
</html> </html>