-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.go
More file actions
117 lines (110 loc) · 3.03 KB
/
html.go
File metadata and controls
117 lines (110 loc) · 3.03 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"net/http"
)
const loginHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GoRESTAPI</title>
</head>
<body>
<form action="/login" method="post">
<label for="username">Username</label>
<input id="username" type="text" name="username">
<br>
<br>
<label for="password">Password</label>
<input id="password" type="password" name="password">
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`
const formHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GoRESTAPI</title>
</head>
<body>
<form action="/form" method="post">
<h1> Weather Forecast </h1>
<label for="latitude">Latitude</label>
<input id="latitude" type="text" name="latitude">
<label for="longitude">Longitude</label>
<input id="longitude" type="text" name="longitude">
<label for="days">Days (max 10)</label>
<input id="days" type="text" name="days">
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`
func generateExternalErrHTML(w http.ResponseWriter, addressAPIResponse AddressAPIResponse, weatherAPIResponse1, weatherAPIReponse2 WeatherAPIResponse) {
responseHTML := fmt.Sprintf(`
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GoRESTAPI</title>
</head>
<body>
<h1>Error occured while calling external API</h1>
<table>
<tr>
<th>API Name</th>
<th>Status</th>
</tr>
<tr>
<td>%s</td>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
<td>%s</td>
</tr>
</table>
</body>
</html>
`, addressAPIResponse.apiName, addressAPIResponse.status, weatherAPIResponse1.apiName, weatherAPIResponse1.status, weatherAPIReponse2.apiName, weatherAPIReponse2.status)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(responseHTML))
}
func generateErrHTML(w http.ResponseWriter, code int, status, message string) {
responseHTML := fmt.Sprintf(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GoRESTAPI</title>
</head>
<body>
<h1> %d - %s </h1>
<h1> %s </h1>
</body>
</html>
`, code, status, message)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(code)
w.Write([]byte(responseHTML))
}