package main import ( "flag" "fmt" "gopkg.in/yaml.v3" "log" "net/http" "os" "time" "riedstra.dev/mitch/go-website/page" ) var VersionString = "" func VersionPrint() { fmt.Println(VersionString) os.Exit(0) } func loadConf(fn string) (*App, error) { fh, err := os.Open(fn) if err != nil { return nil, err } dec := yaml.NewDecoder(fh) app := &App{} err = dec.Decode(app) return app, err } func main() { fl := flag.NewFlagSet("Website", flag.ExitOnError) listen := fl.String("l", "0.0.0.0:8001", "Listening address") directory := fl.String("d", ".", "Directory to serve.") version := fl.Bool("v", false, "Print the version then exit") confFn := fl.String("c", "conf.yml", "Location for the config file") verbose := fl.Bool("V", false, "Be more verbose ( dump config, etc ) ") fl.StringVar(&page.TimeFormat, "T", page.TimeFormat, "Set the page time format, be careful with this") defaultIndexPath := "/reIndex" indexPath := fl.String("i", defaultIndexPath, "Path in which, when called will rebuild the index and clear the cache") _ = fl.Parse(os.Args[1:]) if *version { VersionPrint() } if err := os.Chdir(*directory); err != nil { log.Fatal(err) } app, err := loadConf(*confFn) if err != nil { log.Println(err) app = &App{} } if app.ReIndexPath == "" || *indexPath != defaultIndexPath { app.ReIndexPath = *indexPath } if app.StaticDirectory == "" { app.StaticDirectory = "static" } if app.FeedPrefix == "" { app.FeedPrefix = FeedPrefixDefault } if *verbose { b, _ := yaml.Marshal(app) os.Stderr.Write(b) } srv := &http.Server{ Handler: app, Addr: *listen, WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } log.Fatal(srv.ListenAndServe()) }