diff --git a/main.go b/main.go new file mode 100644 index 0000000..6433f3c --- /dev/null +++ b/main.go @@ -0,0 +1,131 @@ +package main + +import ( + "fmt" + "html/template" + "log" + "net/http" + "path/filepath" +) + +func main() { + http.HandleFunc("/", mainHandler) + http.HandleFunc("/assets/", assetHandler) + + fmt.Println("Starting server on :3000 ...") + log.Fatal(http.ListenAndServe(":3000", nil)) +} + +func mainHandler(w http.ResponseWriter, r *http.Request) { + // Sanitize the path to prevent directory traversal attacks + relPath := r.URL.Path + if relPath == "/" { + relPath = "/index" + } + + // Check method + switch r.Method { + case http.MethodGet: + getHandler(relPath, w, r) + case http.MethodPost: + postHandler(relPath, w, r) + default: + errorPage(405, w, r) + + } + +} + +func getHandler(relPath string, w http.ResponseWriter, r *http.Request) { + tmplPath := filepath.Join("templates", filepath.Clean(relPath)+".html") + + // Parse the template + tmpl, err := template.ParseFiles(tmplPath) + if err != nil { + errorPage(404, w, r) + return + } + + // Execute the template + err = tmpl.Execute(w, nil) + if err != nil { + http.Error(w, "Error rendering template", http.StatusInternalServerError) + } +} + +func postHandler(relPath string, w http.ResponseWriter, r *http.Request) { + switch relPath { + case "/index": + indexPostHandler(w, r) + default: + errorPage(405, w, r) + } +} + +func indexPostHandler(w http.ResponseWriter, r *http.Request) { + // Parse the form data + err := r.ParseForm() + if err != nil { + errorPage(500, w, r) + return + } + + // Get the form data + message := r.FormValue("message") + + // Print the message to the console + log.Printf("Message: %s", message) + + // Create a map to hold the data + requestData := map[string]interface{}{ + "message": message, + } + + // Return a page with message + tmpl, err := template.ParseFiles("templates/index.html") + if err != nil { + errorPage(500, w, r) + return + } + + err = tmpl.Execute(w, requestData) + +} + +func errorPage(errorCode int, w http.ResponseWriter, r *http.Request) { + w.WriteHeader(errorCode) + tmpl, err := template.ParseFiles("templates/error.html") + if err != nil { + log.Printf("Error loading error page template: %v", err) + http.Error(w, "Error loading error page", http.StatusInternalServerError) + return + } + + errorMsgs := map[int]string{ + 404: "Page not found for " + r.URL.Path, + 405: "Method not allowed", + 500: "Internal server error", + } + + // Check if errorcode isn't in the map + if _, ok := errorMsgs[errorCode]; !ok { + errorCode = 500 + } + + // Correcting the initialization of the errorData map + errorData := map[string]interface{}{ + "short": errorCode, + "long": errorMsgs[errorCode], + } + + err = tmpl.Execute(w, errorData) + if err != nil { + log.Printf("Error executing error page template: %v", err) + http.Error(w, "Error rendering error page", http.StatusInternalServerError) + } +} + +func assetHandler(w http.ResponseWriter, r *http.Request) { + // Serve static files from templates/assets + http.ServeFile(w, r, filepath.Join("templates", filepath.Clean(r.URL.Path))) +} diff --git a/templates/about.html b/templates/about.html new file mode 100644 index 0000000..15acde2 --- /dev/null +++ b/templates/about.html @@ -0,0 +1,13 @@ + + + + + + Go HTML Template + + + + +

About page

+ + \ No newline at end of file diff --git a/templates/assets/css/error.css b/templates/assets/css/error.css new file mode 100644 index 0000000..7983fd7 --- /dev/null +++ b/templates/assets/css/error.css @@ -0,0 +1,5 @@ +html { + background-color: black; + color: white; + text-align: center; +} \ No newline at end of file diff --git a/templates/assets/css/index.css b/templates/assets/css/index.css new file mode 100644 index 0000000..5eccafa --- /dev/null +++ b/templates/assets/css/index.css @@ -0,0 +1,4 @@ +html { + background-color: black; + color: white; +} \ No newline at end of file diff --git a/templates/assets/img/favicon.png b/templates/assets/img/favicon.png new file mode 100644 index 0000000..b325464 Binary files /dev/null and b/templates/assets/img/favicon.png differ diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..ef8e6a7 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,14 @@ + + + + + + Error {{.short}} + + + + +

Error {{.short}}

+

{{.long}}

+ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6e0048d --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + + + + + Go HTML Template + + + + +

Hello, World!

+ + {{if .message}} +

{{.message}}

+ {{end}} +
+ + +
+ + \ No newline at end of file