Skip to content

Commit

Permalink
U main.go: add -extract arg for builtin static
Browse files Browse the repository at this point in the history
  • Loading branch information
yurenchen000 committed Nov 9, 2024
1 parent 773e686 commit d6be8cd
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions server-go/cloud-clip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,57 @@ func mkdir_uploads() {
}
}

func extract_static(dest_dir string) error {
// mkdir
if _, err := os.Stat(dest_dir); os.IsNotExist(err) {
err := os.MkdirAll(dest_dir, 0755)
if err != nil {
log.Fatalf("Failed to create extract directory: %v", err)
}
log.Println("++ Extract directory Created")
} else {
fmt.Println("== Extract directory Exists")
}

// extract
err := fs.WalkDir(embed_static_fs, "static", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

relPath := strings.TrimPrefix(path, "static")
destPath := filepath.Join(dest_dir, relPath)

if d.IsDir() {
return os.MkdirAll(destPath, 0755)
}

data, err := embed_static_fs.ReadFile(path)
if err != nil {
return err
}

return os.WriteFile(destPath, data, 0644)
})

if err != nil {
log.Fatalf("Failed to extract static files: %v", err)
} else {
fmt.Println("== builtin Static files extracted to", dest_dir)
}

return nil
}

//go:embed static
var embed_static_fs embed.FS

// specify external static folder
var flg_external_static = flag.String("static", "", "Path to external static files, example ./static")

// extract builtin static for user custmize
var flg_extract = flag.String("extract", "", "Path to extract builtin static, example ./static_out")

func main() {
load_history()
mkdir_uploads() // mkdir -p uplodas
Expand All @@ -392,6 +437,11 @@ func main() {

flag.Parse()

if *flg_extract != "" {
extract_static(*flg_extract)
return
}

if *flg_external_static == "" {
// use builtin static
fmt.Println("== serve from builtin static")
Expand Down

0 comments on commit d6be8cd

Please sign in to comment.