Skip to content

Commit

Permalink
feat: Serves files after generation
Browse files Browse the repository at this point in the history
  • Loading branch information
codingconcepts committed Apr 26, 2024
1 parent 719364a commit b3a4c03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/codingconcepts/dg/internal/pkg/model"
"github.com/codingconcepts/dg/internal/pkg/source"
"github.com/codingconcepts/dg/internal/pkg/ui"
"github.com/codingconcepts/dg/internal/pkg/web"
"github.com/samber/lo"
)

Expand All @@ -31,6 +32,7 @@ func main() {
createImports := flag.String("i", "", "write import statements to file")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
versionFlag := flag.Bool("version", false, "display the current version number")
port := flag.Int("p", 0, "port to serve files from (omit to generate without serving)")
flag.Parse()

if *cpuprofile != "" {
Expand Down Expand Up @@ -78,6 +80,12 @@ func main() {
log.Fatalf("error writing import statements: %v", err)
}
}

if *port == 0 {
return
}

log.Fatal(web.Serve(*outputDir, *port))
}

func loadConfig(filename string, tt ui.TimerFunc) (model.Config, error) {
Expand Down
20 changes: 20 additions & 0 deletions internal/pkg/web/file_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package web

import (
"fmt"
"log"
"net/http"
)

// Serve files from the output csv directory on a given port.
//
// Note: This is a blocking call.
func Serve(dir string, port int) error {
fs := http.FileServer(http.Dir(dir))
http.Handle("/", fs)

addr := fmt.Sprintf(":%d", port)

log.Printf("Serving files on %s", addr)
return http.ListenAndServe(addr, nil)
}

0 comments on commit b3a4c03

Please sign in to comment.