Skip to content

Commit a77411e

Browse files
authored
template and handler (#16)
1 parent 3d565fd commit a77411e

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

html/index.html

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,56 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Simple HTTP Server</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
margin: 20px;
11+
}
12+
h1, h3 {
13+
text-align: center;
14+
}
15+
ul {
16+
list-style-type: none;
17+
padding: 0;
18+
}
19+
li {
20+
margin-bottom: 10px;
21+
}
22+
table {
23+
border-collapse: collapse;
24+
width: 100%;
25+
}
26+
th, td {
27+
border: 1px solid #ccc;
28+
padding: 8px;
29+
text-align: left;  
30+
31+
}
32+
th {
33+
background-color: #f2f2f2;
34+
}
35+
</style>
736
</head>
837
<body>
938
<h1>Welcome to Simple HTTP Server</h1>
1039
<h3>by Paresh Pawar</h3>
1140
<ul>
12-
<li>Origin IP: {{.Origin}}</li>
13-
<li>Request Method: {{.Type}}</li>
14-
<li>Destination: {{.Hostname}}</li>
41+
<li>Request: {{.Request.ReqTime}}</li>
42+
<li>Type: {{.Request.ReqType}}</li>
43+
<li>Host: {{.Request.Host}}</li>
44+
<li>Remote Address: {{.Request.RemoteAddr}}</li>
1545
</ul>
46+
<table>
47+
<tr>
48+
<th>Key</th>
49+
<th>Value</th>
50+
</tr>
51+
{{ range $key, $value := .Headers }}
52+
<tr>
53+
<td>{{ $key }}</td>
54+
<td>{{ $value }}</td>
55+
</tr>
56+
{{ end }}
57+
</table>
1658
</body>
1759
</html>

simple_http_server.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,42 @@ func check(err error) {
3030
}
3131
}
3232

33-
func htmlhandler(w http.ResponseWriter, r *http.Request){
33+
func htmlhandler(w http.ResponseWriter, r *http.Request) {
34+
timestamp := time.Now()
35+
if r.Method == "GET" {
36+
fmt.Print(string("\033[34m"))
37+
} else if r.Method == "POST" {
38+
fmt.Print(string("\033[33m"))
39+
} else if r.Method == "PUT" {
40+
fmt.Print(string("\033[35m"))
41+
} else if r.Method == "DELETE" {
42+
fmt.Print(string("\033[31m"))
43+
} else {
44+
fmt.Print(string("\033[36m"))
45+
}
46+
fmt.Printf("%s %s %s %s ===> from %s\n", timestamp.Local(), r.Method, r.URL, r.Proto, r.RemoteAddr)
3447

3548
file, err := os.ReadFile("html/index.html")
3649
check(err)
3750

3851
template, err := template.New("webpage").Parse(string(file))
3952
check(err)
4053

54+
type reqDataStruct struct{ ReqTime, ReqType, Host, Remote, RemoteAddr string }
55+
reqData := reqDataStruct{
56+
ReqTime: timestamp.String(),
57+
ReqType: r.Proto + " " + r.Method + " " + r.URL.Path,
58+
Host: r.Host,
59+
Remote: r.RemoteAddr,
60+
RemoteAddr: utils.GetMyOutboundIP().String(),
61+
}
62+
4163
data := struct {
42-
Origin string
43-
Type string
44-
Hostname string
64+
Request reqDataStruct
65+
Headers http.Header
4566
}{
46-
Origin: r.RemoteAddr,
47-
Type: r.Method,
48-
Hostname: r.Host,
67+
Request: reqData,
68+
Headers: r.Header,
4969
}
5070

5171
err = template.Execute(w, data)

0 commit comments

Comments
 (0)