-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (97 loc) · 3.08 KB
/
main.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"ascii-art-web/ascii"
)
// PageVariables holds data to pass to the HTML template
type PageVariables struct {
Text string
Banner string
AsciiArt string
}
func main() {
http.HandleFunc("/", mainPage)
// Serve static files from the "static" folder
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
// Start the server
fmt.Println("Server is running at http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}
func mainPage(w http.ResponseWriter, r *http.Request) {
vars := PageVariables{}
if r.Method == http.MethodGet {
if r.URL.Path != "/" && r.URL.Path != "/index.html" {
// Serve a custom 404 error page
w.WriteHeader(http.StatusNotFound)
http.ServeFile(w, r, "templates/404.html")
return
}
}
if r.Method == http.MethodPost {
// Handle POST data
if err := r.ParseForm(); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Error parsing form: %v", err)
return
}
// handle file export
if r.FormValue("download") == "Download ASCII Art" {
// User requested a download
content := []byte(r.FormValue("ascii_art"))
log.Println(vars.AsciiArt)
// Set headers for file download
w.Header().Set("Content-Disposition", "attachment; filename=ascii_art.txt")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(content)))
w.Header().Set("Content-Type", "text/plain")
_, err := w.Write(content)
if err != nil {
http.Error(w, "Failed to export ASCII art", http.StatusInternalServerError)
log.Printf("Error writing ASCII art to response: %v", err)
}
return
}
// handle ascii art generation
vars.Text = r.FormValue("text")
vars.Banner = r.FormValue("banner")
// Validate text
if vars.Text == "" {
http.Error(w, "Bad Request 400: 'text' cannot be empty", http.StatusBadRequest)
return
}
// Comment this out to test internal server error handling
// Validate banner
if vars.Banner != "standard" && vars.Banner != "shadow" && vars.Banner != "thinkertoy" {
http.Error(w, "Bad Request 400: Invalid 'banner' value", http.StatusBadRequest)
return
}
// Log received values for debugging
log.Printf("Received text: %q", vars.Text)
log.Printf("Selected banner: %q", vars.Banner)
// Generate ASCII art from the input text
var err error
vars.AsciiArt, err = ascii.GenerateAsciiArt(vars.Text, vars.Banner)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Serve the main template for the homepage
t, err := template.ParseFiles("templates/index.html")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Error parsing template: %v", err)
return
}
// Set status code to OK (200)
w.WriteHeader(http.StatusOK)
if err := t.Execute(w, vars); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Error executing template: %v", err)
}
}