diff --git a/cmd/app/main.go b/cmd/app/main.go new file mode 100644 index 0000000..5da6887 --- /dev/null +++ b/cmd/app/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" +) + +func main() { + // Create a new router + r := chi.NewRouter() + + // Add some middleware for logging and recover + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + + // Register the hello world handler at the root path + r.Get("/", helloHandler) + + // Serve static HTML files from the "static" directory + // (e.g., /static/index.html will be accessible at /static/index.html) + FileServer(r, "/static", http.Dir("./static")) + + // Start the HTTP server + http.ListenAndServe(":8080", r) +} + +// helloHandler responds with a simple "Hello, World!" message. +func helloHandler(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Hello, World!")) +} + +// FileServer sets up a http.FileServer handler for serving static files. +func FileServer(r chi.Router, publicPath string, root http.FileSystem) { + if publicPath == "" { + publicPath = "/" + } + // Ensure the path ends with a slash + if publicPath[len(publicPath)-1] != '/' { + publicPath += "/" + } + fs := http.StripPrefix(publicPath, http.FileServer(root)) + r.Get(publicPath+"*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fs.ServeHTTP(w, r) + })) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e6bc79d --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module draw-tools + +go 1.22 + +require github.com/go-chi/chi/v5 v5.0.12 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bfc9174 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= +github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=