-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlersMethods.go
148 lines (138 loc) · 4.18 KB
/
handlersMethods.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"net/http"
"strings"
)
func getHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorHandler(w, http.StatusMethodNotAllowed)
return
}
if r.URL.Path == "/" {
// Display the homepage
renderTemplate(w, "Home Page", &res)
res = result{}
msg=Email{}
} else {
errorHandler(w, http.StatusNotFound)
}
}
func postHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
errorHandler(w, http.StatusMethodNotAllowed)
return
}
if r.URL.Path == "/ascii-art" {
// Parse the form data
if err := r.ParseForm(); err != nil {
setError(w, r, "Failed to parse form data.")
return
}
text := r.FormValue("text")
banner := r.FormValue("banner")
if text == "" || banner == "" { // Set error message if any field is empty
// setError(w, r, "Text or Banner cannot be empty")
errorHandler(w, http.StatusBadRequest)
return
}
if len(text) > 700 { // Check if text exceeds 700 characters
// setError(w, r, "Please enter less than 700 characters.")
errorHandler(w, http.StatusBadRequest)
return
}
// Validate banner value
validBanners := map[string]bool{
"shadow": true,
"standard": true,
"thinkertoy": true,
}
if !validBanners[banner] {
errorHandler(w, http.StatusBadRequest) // Invalid banner value
return
}
// Generate ascii-art
artResult := artHandler(text, banner)
// Check if special characters are present
if artResult == "Special charactere is not allowed." {
// Set error message for non-printable characters
setError(w, r, "Please enter printable ASCII characters only.")
//errorHandler(w, http.StatusBadRequest)
return
} else {
// Process form values and generate ASCII art if valid
res = result{
Symbol: "\n" + artResult,
Err: "", // Clear error
Text: text, // Store the text
Banner: banner, // Store the selected banner
}
}
http.Redirect(w, r, "/", http.StatusFound)
} else {
// 404 Error for incorrect path
errorHandler(w, http.StatusNotFound)
}
}
// Function to handle requests to the /Js/ path
func jsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorHandler(w, http.StatusMethodNotAllowed)
return
}
// Check the path
if strings.HasPrefix(r.URL.Path, "/assets/js/") {
// If the request is directly to /Js/, return a forbidden error
if r.URL.Path == "/assets/js/" {
errorHandler(w, http.StatusForbidden)
return
}
/*if r.URL.Path == "/Js/script.js" {
errorHandler(w, http.StatusForbidden)
return
}*/
// If the request is for a specific file, pass the request to http.FileServer
http.StripPrefix("/assets/js/", http.FileServer(http.Dir("assets/js"))).ServeHTTP(w, r)
} else {
// If the path is incorrect, return a 404 error
errorHandler(w, http.StatusNotFound)
}
}
func cssHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorHandler(w, http.StatusMethodNotAllowed)
return
}
// Check the path
if strings.HasPrefix(r.URL.Path, "/assets/css/") {
// If the request is directly to /assets/css/, return a forbidden error
if r.URL.Path == "/assets/css/" {
errorHandler(w, http.StatusForbidden)
return
}
// If the request is for a specific file, pass the request to http.FileServer
http.StripPrefix("/assets/css/", http.FileServer(http.Dir("assets/css"))).ServeHTTP(w, r)
} else {
// If the path is incorrect, return a 404 error
errorHandler(w, http.StatusNotFound)
}
}
// Function to handle requests to the /assets/images/ path
func imgHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorHandler(w, http.StatusMethodNotAllowed)
return
}
// Check the path
if strings.HasPrefix(r.URL.Path, "/assets/images/") {
// If the request is directly to /assets/images/, return a forbidden error
if r.URL.Path == "/assets/images/" {
errorHandler(w, http.StatusForbidden)
return
}
// If the request is for a specific file, pass the request to http.FileServer
http.StripPrefix("/assets/images/", http.FileServer(http.Dir("assets/images"))).ServeHTTP(w, r)
} else {
// If the path is incorrect, return a 404 error
errorHandler(w, http.StatusNotFound)
}
}