From 82a5490ff59b3a27f4d630ad4051baf098ad5b3d Mon Sep 17 00:00:00 2001 From: adnano Date: Mon, 28 Sep 2020 19:54:48 -0400 Subject: [PATCH] Add -serve flag --- go.mod | 2 ++ go.sum | 8 ++++++++ kiln.go | 28 +++++++++++++++------------- main.go | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 26d486e..a68415a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module kiln go 1.15 + +require git.sr.ht/~adnano/gmi v0.0.0-20200928222852-855eff6d8802 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b9326cb --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +git.sr.ht/~adnano/gmi v0.0.0-20200928222852-855eff6d8802 h1:VAuDKKZWyq3/UkBuIeLEQ1qjRqiRfACfErdvBNdYz9A= +git.sr.ht/~adnano/gmi v0.0.0-20200928222852-855eff6d8802/go.mod h1:4MWQDsleal4HRi/LuxxM6ymWJQikP3Gh7xZindVCHzg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/kiln.go b/kiln.go index a386ed8..5698786 100644 --- a/kiln.go +++ b/kiln.go @@ -82,20 +82,22 @@ func (s *Site) Manipulate(dir *Directory) error { path := filepath.Join(dir.Path, "index.gmi") builder := &strings.Builder{} tmpl := s.Templates.Lookup("index.gmi") - if err := tmpl.Execute(builder, dir); err != nil { - return err + if tmpl != nil { + if err := tmpl.Execute(builder, dir); err != nil { + return err + } + content := builder.String() + permalink := filepath.Dir(path) + if permalink == "." { + permalink = "" + } + page := &Page{ + Path: path, + Permalink: "/" + permalink, + Content: content, + } + dir.Index = page } - content := builder.String() - permalink := filepath.Dir(path) - if permalink == "." { - permalink = "" - } - page := &Page{ - Path: path, - Permalink: "/" + permalink, - Content: content, - } - dir.Index = page } // Manipulate pages diff --git a/main.go b/main.go index 249bb40..9bc8ce2 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,34 @@ package main import ( + "flag" "log" + "time" + + "git.sr.ht/~adnano/gmi" ) +var ( + serveSite bool +) + +func init() { + flag.BoolVar(&serveSite, "serve", false, "serve the site") +} + func main() { - if err := run(); err != nil { + flag.Parse() + if err := build(); err != nil { log.Fatal(err) } + + if serveSite { + serve() + } } -func run() error { +// build the site +func build() error { site, err := LoadSite("src") if err != nil { return err @@ -23,3 +41,15 @@ func run() error { } return nil } + +// serve the site +func serve() error { + server := &gmi.Server{} + cert, err := gmi.NewCertificate("localhost", time.Hour) + if err != nil { + return err + } + server.Certificate = cert + server.Handler = gmi.FileServer(gmi.Dir("dst")) + return server.ListenAndServe() +}