-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlersMethods.go
111 lines (103 loc) · 2.86 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
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{
Symbol: "", // Clear previous values
Banner: "",
Text: "",
Err: "",
}
} 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, "/Js/") {
// If the request is directly to /Js/, return a forbidden error
if r.URL.Path == "/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("/Js/", http.FileServer(http.Dir("Js"))).ServeHTTP(w, r)
} else {
// If the path is incorrect, return a 404 error
errorHandler(w, http.StatusNotFound)
}
}