Add -serve flag

This commit is contained in:
adnano 2020-09-28 19:54:48 -04:00
parent 27608b600e
commit 82a5490ff5
4 changed files with 57 additions and 15 deletions

2
go.mod
View file

@ -1,3 +1,5 @@
module kiln module kiln
go 1.15 go 1.15
require git.sr.ht/~adnano/gmi v0.0.0-20200928222852-855eff6d8802

8
go.sum Normal file
View file

@ -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=

View file

@ -82,6 +82,7 @@ func (s *Site) Manipulate(dir *Directory) error {
path := filepath.Join(dir.Path, "index.gmi") path := filepath.Join(dir.Path, "index.gmi")
builder := &strings.Builder{} builder := &strings.Builder{}
tmpl := s.Templates.Lookup("index.gmi") tmpl := s.Templates.Lookup("index.gmi")
if tmpl != nil {
if err := tmpl.Execute(builder, dir); err != nil { if err := tmpl.Execute(builder, dir); err != nil {
return err return err
} }
@ -97,6 +98,7 @@ func (s *Site) Manipulate(dir *Directory) error {
} }
dir.Index = page dir.Index = page
} }
}
// Manipulate pages // Manipulate pages
for i := range dir.Pages { for i := range dir.Pages {

34
main.go
View file

@ -1,16 +1,34 @@
package main package main
import ( import (
"flag"
"log" "log"
"time"
"git.sr.ht/~adnano/gmi"
) )
var (
serveSite bool
)
func init() {
flag.BoolVar(&serveSite, "serve", false, "serve the site")
}
func main() { func main() {
if err := run(); err != nil { flag.Parse()
if err := build(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if serveSite {
serve()
}
} }
func run() error { // build the site
func build() error {
site, err := LoadSite("src") site, err := LoadSite("src")
if err != nil { if err != nil {
return err return err
@ -23,3 +41,15 @@ func run() error {
} }
return nil 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()
}