You've already forked collage-maker
							
							Cleanup for 1.0 #18
							
								
								
									
										37
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								main.go
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"context"
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"embed"
 | 
			
		||||
@@ -12,6 +13,7 @@ import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"sync/atomic"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
@@ -30,14 +32,14 @@ var (
 | 
			
		||||
	imagesDirFs    fs.FS
 | 
			
		||||
	listenAddr     string
 | 
			
		||||
 | 
			
		||||
	// go:embed web/*
 | 
			
		||||
	//go:embed web
 | 
			
		||||
	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.BoolVar(&localAssets, "local-assets", false, "Serve local assets during development")
 | 
			
		||||
	flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
 | 
			
		||||
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
@@ -48,7 +50,6 @@ func main() {
 | 
			
		||||
	collagesPath := "collages"
 | 
			
		||||
 | 
			
		||||
	addFileServer := func(path, dir string) {
 | 
			
		||||
 | 
			
		||||
		httpFileServer := http.FileServer(http.Dir(dir))
 | 
			
		||||
		http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer))
 | 
			
		||||
	}
 | 
			
		||||
@@ -63,14 +64,29 @@ func main() {
 | 
			
		||||
			httpFileServer.ServeHTTP(w, r)
 | 
			
		||||
		})
 | 
			
		||||
	} else {
 | 
			
		||||
		fs, err := fs.Sub(webFS, "web")
 | 
			
		||||
 | 
			
		||||
		indexModTime := time.Now()
 | 
			
		||||
		indexHTML := func() io.ReadSeeker {
 | 
			
		||||
			indexHTMLContent, err := webFS.ReadFile("web/index.html")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				panic(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		httpFileServer := http.FileServer(http.FS(fs))
 | 
			
		||||
		http.Handle("/", httpFileServer)
 | 
			
		||||
			devOnlyRegex := regexp.MustCompile("\n[^\n]*<!-- DEVONLY[^\n]*")
 | 
			
		||||
			return bytes.NewReader(devOnlyRegex.ReplaceAllLiteral(indexHTMLContent, nil))
 | 
			
		||||
		}()
 | 
			
		||||
		httpFileServer := func() http.Handler {
 | 
			
		||||
			webrootFs, err := fs.Sub(webFS, "web")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				panic(err)
 | 
			
		||||
			}
 | 
			
		||||
			return http.FileServer(http.FS(webrootFs))
 | 
			
		||||
		}()
 | 
			
		||||
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
			if r.URL.Path == "/" {
 | 
			
		||||
				http.ServeContent(w, r, "index.html", indexModTime, indexHTML)
 | 
			
		||||
			} else {
 | 
			
		||||
				httpFileServer.ServeHTTP(w, r)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
@@ -105,7 +121,8 @@ func main() {
 | 
			
		||||
		if err := idle.Wait(30 * time.Minute); err != nil {
 | 
			
		||||
			slog.Error("Failed to wait for idler", "error", err)
 | 
			
		||||
		}
 | 
			
		||||
		ctx, _ := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
 | 
			
		||||
		ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
 | 
			
		||||
		defer cancel()
 | 
			
		||||
		if err := server.Shutdown(ctx); err != nil {
 | 
			
		||||
			slog.Error("http server Shutdown failed", "error", err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -152,5 +169,3 @@ func MakeCollage(req *collage.Request) (string, error) {
 | 
			
		||||
	}
 | 
			
		||||
	return collageFile, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// curl -D - --json @req.json http://localhost:8767/make-collage
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
    <head>
 | 
			
		||||
        <link rel="stylesheet" href="croppie.css" />
 | 
			
		||||
        <link rel="icon" href="data:;base64,iVBORw0KGgo=" />
 | 
			
		||||
        <script src="http://localhost:35729/livereload.js"></script>
 | 
			
		||||
        <!-- DEVONLY --> <script src="http://localhost:35729/livereload.js"></script>
 | 
			
		||||
        <script src="croppie.js" defer></script>
 | 
			
		||||
        <script src="index.js" defer></script>
 | 
			
		||||
        <link rel="stylesheet" href="index.css" />
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user