Skip to content

Commit 6b24ec4

Browse files
committed
modified project with additional features
1 parent 74a74f5 commit 6b24ec4

File tree

5 files changed

+197
-12
lines changed

5 files changed

+197
-12
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/itsfarhan/Simple-go-app
1+
module go-web-app
22

3-
go 1.20
3+
go 1.23.0

main.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1+
// main.go
12
package main
23

34
import (
4-
"fmt"
5+
"html/template"
6+
"log"
57
"net/http"
68
)
79

8-
// handler function to serve the home page
9-
func homePage(w http.ResponseWriter, r *http.Request) {
10-
fmt.Fprintf(w, "Hello, World!")
10+
// Parse the templates once to avoid re-parsing on every request
11+
var templates = template.Must(template.ParseGlob("templates/*.html"))
12+
13+
// RenderTemplate renders a specific template
14+
func RenderTemplate(w http.ResponseWriter, tmpl string) {
15+
err := templates.ExecuteTemplate(w, tmpl+".html", nil)
16+
if err != nil {
17+
http.Error(w, "Unable to load template", http.StatusInternalServerError)
18+
log.Println("Template rendering error:", err)
19+
}
20+
}
21+
22+
func homeHandler(w http.ResponseWriter, r *http.Request) {
23+
RenderTemplate(w, "home")
24+
}
25+
26+
func aboutHandler(w http.ResponseWriter, r *http.Request) {
27+
RenderTemplate(w, "about")
1128
}
1229

1330
func main() {
14-
// Route for the home page
15-
http.HandleFunc("/", homePage)
31+
http.HandleFunc("/", homeHandler) // Route for home page
32+
http.HandleFunc("/about", aboutHandler) // Route for about page
1633

17-
// Start the server on port 8080
18-
fmt.Println("Server starting on port 8080...")
19-
if err := http.ListenAndServe(":8080", nil); err != nil {
20-
fmt.Println("Error starting server:", err)
34+
log.Println("Server starting on :8080...")
35+
err := http.ListenAndServe(":8081", nil)
36+
if err != nil {
37+
log.Fatal("Server failed to start:", err)
2138
}
2239
}

main_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// main_test.go
2+
package main
3+
4+
import (
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
)
10+
11+
// TestHomeHandler tests the homeHandler function
12+
func TestHomeHandler(t *testing.T) {
13+
req, err := http.NewRequest("GET", "/", nil)
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
18+
rr := httptest.NewRecorder()
19+
handler := http.HandlerFunc(homeHandler)
20+
21+
handler.ServeHTTP(rr, req)
22+
23+
if status := rr.Code; status != http.StatusOK {
24+
t.Errorf("homeHandler returned wrong status code: got %v want %v", status, http.StatusOK)
25+
}
26+
27+
expected := "<title>Home</title>"
28+
if !strings.Contains(rr.Body.String(), expected) {
29+
t.Errorf("homeHandler returned unexpected body: got %v want to contain %v", rr.Body.String(), expected)
30+
}
31+
}
32+
33+
// TestAboutHandler tests the aboutHandler function
34+
func TestAboutHandler(t *testing.T) {
35+
req, err := http.NewRequest("GET", "/about", nil)
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
rr := httptest.NewRecorder()
41+
handler := http.HandlerFunc(aboutHandler)
42+
43+
handler.ServeHTTP(rr, req)
44+
45+
if status := rr.Code; status != http.StatusOK {
46+
t.Errorf("aboutHandler returned wrong status code: got %v want %v", status, http.StatusOK)
47+
}
48+
49+
expected := "<title>About</title>"
50+
if !strings.Contains(rr.Body.String(), expected) {
51+
t.Errorf("aboutHandler returned unexpected body: got %v want to contain %v", rr.Body.String(), expected)
52+
}
53+
}

templates/about.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>About</title>
6+
<!-- Add some modern styles -->
7+
<style>
8+
body {
9+
font-family: 'Arial', sans-serif;
10+
background-color: #f4f4f4;
11+
color: #333;
12+
margin: 0;
13+
padding: 0;
14+
}
15+
16+
.container {
17+
max-width: 800px;
18+
margin: 50px auto;
19+
padding: 20px;
20+
background-color: #fff;
21+
border-radius: 8px;
22+
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
23+
}
24+
25+
h1 {
26+
color: #333;
27+
text-align: center;
28+
}
29+
30+
p {
31+
font-size: 1.2em;
32+
text-align: center;
33+
}
34+
35+
a {
36+
display: inline-block;
37+
margin-top: 20px;
38+
padding: 10px 20px;
39+
background-color: #28a745;
40+
color: white;
41+
text-decoration: none;
42+
border-radius: 5px;
43+
transition: background-color 0.3s ease;
44+
}
45+
46+
a:hover {
47+
background-color: #218838;
48+
}
49+
</style>
50+
</head>
51+
<body>
52+
<div class="container">
53+
<h1>About Page</h1>
54+
<p>This is a simple Go web application.</p>
55+
<p><a href="/">Back to Home</a></p>
56+
</div>
57+
</body>
58+
</html>

templates/home.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Home</title>
6+
<!-- Add some modern styles -->
7+
<style>
8+
body {
9+
font-family: 'Arial', sans-serif;
10+
background-color: #f4f4f4;
11+
color: #333;
12+
margin: 0;
13+
padding: 0;
14+
}
15+
16+
.container {
17+
max-width: 800px;
18+
margin: 50px auto;
19+
padding: 20px;
20+
background-color: #fff;
21+
border-radius: 8px;
22+
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
23+
}
24+
25+
h1 {
26+
color: #333;
27+
text-align: center;
28+
}
29+
30+
p {
31+
font-size: 1.2em;
32+
text-align: center;
33+
}
34+
35+
a {
36+
display: inline-block;
37+
margin-top: 20px;
38+
padding: 10px 20px;
39+
background-color: #007BFF;
40+
color: white;
41+
text-decoration: none;
42+
border-radius: 5px;
43+
transition: background-color 0.3s ease;
44+
}
45+
46+
a:hover {
47+
background-color: #0056b3;
48+
}
49+
</style>
50+
</head>
51+
<body>
52+
<div class="container">
53+
<h1>Welcome to the Home Page!</h1>
54+
<p><a href="/about">Go to About Page</a></p>
55+
</div>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)