diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-01-14 18:22:11 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2025-01-14 18:22:19 -0500 |
| commit | 2fbc950b7da60087930c8ec3cd667ef70c889f2c (patch) | |
| tree | bf69ab8af9214838aed0a689eb16cb5a7a143ccb /cmd/nomadicdemo/main.go | |
| parent | 432b1128b43d7ad34b89866f6ce8ab93b6c2e7d3 (diff) | |
| download | x-2fbc950b7da60087930c8ec3cd667ef70c889f2c.tar.xz x-2fbc950b7da60087930c8ec3cd667ef70c889f2c.zip | |
uber fix
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd/nomadicdemo/main.go')
| -rw-r--r-- | cmd/nomadicdemo/main.go | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/cmd/nomadicdemo/main.go b/cmd/nomadicdemo/main.go new file mode 100644 index 0000000..cd56018 --- /dev/null +++ b/cmd/nomadicdemo/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "flag" + "fmt" + "log" + "log/slog" + "net/http" + "time" + + "github.com/a-h/templ" + "within.website/x/htmx" + "within.website/x/internal" + "within.website/x/web" + "within.website/x/xess" +) + +//go:generate go run github.com/a-h/templ/cmd/templ@latest generate + +var ( + apiURL = flag.String("api-url", "https://waifuwave.fly.dev/generate", "API backend URL") + bind = flag.String("bind", ":3924", "TCP address to bind to") +) + +func main() { + internal.HandleStartup() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + _ = ctx + + mux := http.NewServeMux() + htmx.Mount(mux) + xess.Mount(mux) + + mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { + prompt := r.FormValue("prompt") + negPrompt := r.FormValue("negative_prompt") + + var imageURL string + var howLong time.Duration + + if prompt != "" && negPrompt != "" { + t0 := time.Now() + output, err := getImage(r.Context(), *apiURL, prompt, negPrompt) + if err != nil { + slog.Error("can't make image", "err", err) + templ.Handler( + xess.Simple("Scale to 0 demo", ohNoes(err.Error())), + templ.WithStatus(http.StatusInternalServerError), + ).ServeHTTP(w, r) + return + } + howLong = time.Now().Sub(t0) + + imageURL = output.URL + } + + if prompt == "" { + prompt = "1girl, solo, flower, long hair, outdoors, green hair, running, smiling, parted lips, blue eyes, cute, sakura blossoms, spring, sun, blue sky, depth of field, cat ears, kimono, bow, onsen, pool, warm lighting, safe" + } + + if negPrompt == "" { + negPrompt = "crop, bad hands, worst hands, worst quality" + } + + templ.Handler( + xess.Simple("Scale to 0 demo", index(prompt, negPrompt, imageURL, howLong)), + ).ServeHTTP(w, r) + }) + + slog.Info("listening", "bind", *bind) + log.Fatal(http.ListenAndServe(*bind, mux)) +} + +func getImage(ctx context.Context, apiURL, prompt, negPrompt string) (*Output, error) { + buf := bytes.Buffer{} + + if err := json.NewEncoder(&buf).Encode(Input{negPrompt, prompt}); err != nil { + return nil, fmt.Errorf("can't encode: %w", err) + } + + resp, err := http.Post(apiURL, "application/json", &buf) + if err != nil { + return nil, fmt.Errorf("can't request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, web.NewError(http.StatusOK, resp) + } + + var result Output + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("can't read result: %w", err) + } + + return &result, nil +} + +type Input struct { + NegativePrompt string `json:"negative_prompt"` + Prompt string `json:"prompt"` +} + +type Output struct { + Fname []string `json:"fname"` + URL string `json:"url"` +} |
