From 1c742809a168529fa1d545f33a6b13f6f0174754 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Thu, 31 Aug 2023 20:27:43 -0400 Subject: [PATCH] Serve web files in go --- main.go | 37 ++++++++++++++++++++++++++++++++-- req.json | 29 -------------------------- croppie.css => web/croppie.css | 0 croppie.js => web/croppie.js | 0 index.html => web/index.html | 4 ++-- index.js => web/index.js | 0 6 files changed, 37 insertions(+), 33 deletions(-) delete mode 100644 req.json rename croppie.css => web/croppie.css (100%) rename croppie.js => web/croppie.js (100%) rename index.html => web/index.html (98%) rename index.js => web/index.js (100%) diff --git a/main.go b/main.go index bf42e95..6acc19e 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "crypto/rand" + "embed" "encoding/json" "flag" "fmt" "io" + "io/fs" "net/http" "os" "path" @@ -18,16 +20,47 @@ import ( ) var ( - imagesDir string - collageDir string + imagesDir string + collageDir string + localAssets bool ) +//go:embed web/* +var webFS embed.FS + func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") + flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets for testing") + flag.Parse() nameGen := NewNameGen() imagesDirFs := os.DirFS(imagesDir) + imagesURLPath := "images" + collagesPath := "collages" + + addFileServer := func(path, dir string) { + + httpFileServer := http.FileServer(http.Dir(dir)) + http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer)) + } + + addFileServer(imagesURLPath, imagesDir) + addFileServer(collagesPath, collageDir) + + if localAssets { + httpFileServer := http.FileServer(http.Dir("web")) + http.Handle("/", httpFileServer) + } else { + fs, err := fs.Sub(webFS, "web") + + if err != nil { + panic(err) + } + + httpFileServer := http.FileServer(http.FS(fs)) + http.Handle("/", httpFileServer) + } http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) { collageReq := collage.Request{} diff --git a/req.json b/req.json deleted file mode 100644 index e7f3a57..0000000 --- a/req.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "background_image": "", - "aspect": { "width": 4224, "height": 3264 }, - "dimension": { "width": 1187, "height": 848 }, - "photos": [ - { - "image": "img1.jpg", - "crop": { - "start": { "x": 419, "y": 667 }, - "end": { "x": 2707, "y": 3389 } - }, - "frame": { - "start": { "x": 0, "y": 0 }, - "end": { "x": 712, "y": 848 } - } - }, - { - "image": "img2.jpg", - "crop": { - "start": { "x": 331, "y": 44 }, - "end": { "x": 1132, "y": 1468 } - }, - "frame": { - "start": { "x": 712, "y": 0 }, - "end": { "x": 1187, "y": 848 } - } - } - ] -} diff --git a/croppie.css b/web/croppie.css similarity index 100% rename from croppie.css rename to web/croppie.css diff --git a/croppie.js b/web/croppie.js similarity index 100% rename from croppie.js rename to web/croppie.js diff --git a/index.html b/web/index.html similarity index 98% rename from index.html rename to web/index.html index c601b02..d719977 100644 --- a/index.html +++ b/web/index.html @@ -73,10 +73,10 @@
-
+
-
+
diff --git a/index.js b/web/index.js similarity index 100% rename from index.js rename to web/index.js