Skip to content

Commit b3a4c03

Browse files
feat: Serves files after generation
1 parent 719364a commit b3a4c03

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

dg.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/codingconcepts/dg/internal/pkg/model"
1717
"github.com/codingconcepts/dg/internal/pkg/source"
1818
"github.com/codingconcepts/dg/internal/pkg/ui"
19+
"github.com/codingconcepts/dg/internal/pkg/web"
1920
"github.com/samber/lo"
2021
)
2122

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

3638
if *cpuprofile != "" {
@@ -78,6 +80,12 @@ func main() {
7880
log.Fatalf("error writing import statements: %v", err)
7981
}
8082
}
83+
84+
if *port == 0 {
85+
return
86+
}
87+
88+
log.Fatal(web.Serve(*outputDir, *port))
8189
}
8290

8391
func loadConfig(filename string, tt ui.TimerFunc) (model.Config, error) {

internal/pkg/web/file_server.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package web
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
)
8+
9+
// Serve files from the output csv directory on a given port.
10+
//
11+
// Note: This is a blocking call.
12+
func Serve(dir string, port int) error {
13+
fs := http.FileServer(http.Dir(dir))
14+
http.Handle("/", fs)
15+
16+
addr := fmt.Sprintf(":%d", port)
17+
18+
log.Printf("Serving files on %s", addr)
19+
return http.ListenAndServe(addr, nil)
20+
}

0 commit comments

Comments
 (0)