From ee6c1c82cb28540412864e9b6932d933ebf43cf3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 8 May 2022 10:19:48 -0700 Subject: [PATCH] Cleanup --- go.mod | 1 + go.sum | 2 ++ main.go | 32 +++++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f369bba..276e859 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.16 require ( github.com/FooSoft/goldsmith v0.0.0-20220410034610-83530f1b2fe4 github.com/FooSoft/goldsmith-components v0.0.0-20220410175545-c35ca9cfd9d4 + github.com/toqueteos/webbrowser v1.2.0 github.com/yuin/goldmark v1.4.12 ) diff --git a/go.sum b/go.sum index 3ff9187..b79f5f8 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/tdewolff/minify/v2 v2.9.27/go.mod h1:L/bwPtsU/Xx30MxCndlClCMMiLbqROgkR4vZT+QIGXA= github.com/tdewolff/parse/v2 v2.5.26/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho= github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= +github.com/toqueteos/webbrowser v1.2.0 h1:tVP/gpK69Fx+qMJKsLE7TD8LuGWPnEV71wBN9rrstGQ= +github.com/toqueteos/webbrowser v1.2.0/go.mod h1:XWoZq4cyp9WeUeak7w7LXRUQf1F1ATJMir8RTqb4ayM= github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= github.com/yuin/goldmark v1.4.12 h1:6hffw6vALvEDqJ19dOJvJKOoAOKe4NDaTqvd2sktGN0= github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= diff --git a/main.go b/main.go index c0124a3..7eb12ff 100644 --- a/main.go +++ b/main.go @@ -2,22 +2,29 @@ package main import ( "flag" + "fmt" "io/ioutil" "log" "os" + "os/signal" "path/filepath" + "syscall" "github.com/FooSoft/goldsmith" "github.com/FooSoft/goldsmith-components/devserver" "github.com/FooSoft/goldsmith-components/plugins/livejs" "github.com/FooSoft/goldsmith-components/plugins/markdown" + "github.com/toqueteos/webbrowser" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer/html" ) -type builder struct{} +type builder struct { + port int + browsing bool +} func (self *builder) Build(contentDir, buildDir, cacheDir string) { log.Print("building...") @@ -37,6 +44,11 @@ func (self *builder) Build(contentDir, buildDir, cacheDir string) { for _, err := range errs { log.Print(err) } + + if !self.browsing { + webbrowser.Open(fmt.Sprintf("http://127.0.0.1:%d", self.port)) + self.browsing = true + } } func main() { @@ -63,6 +75,20 @@ func main() { contentDir = filepath.Dir(requestPath) } - b := new(builder) - devserver.DevServe(b, *port, contentDir, buildDir, "") + go func() { + b := &builder{port: *port} + devserver.DevServe(b, *port, contentDir, buildDir, "") + }() + + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + for { + <-sigs + log.Println("terminating...") + break + } + if err := os.RemoveAll(buildDir); err != nil { + log.Fatal(err) + } }