package main import ( "flag" "fmt" "net/http" "os" "os/signal" "go.jolheiser.com/blog/post" "go.jolheiser.com/blog/router" "github.com/go-chi/chi/v5/middleware" "github.com/peterbourgon/ff/v3" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) func main() { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: &log.Logger, NoColor: true}) fs := flag.NewFlagSet("blog", flag.ContinueOnError) postDirFlag := fs.String("post-dir", "posts", "Path to posts directory") portFlag := fs.Int("port", 8080, "Port to serve on") fs.String("config", "", "Config file") debugFlag := fs.Bool("debug", false, "Debug mode") if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("BLOG"), ff.WithConfigFileFlag("config"), ff.WithAllowMissingConfigFile(true), ); err != nil { log.Error().Err(err).Msg("could not parse flags") return } if *debugFlag { zerolog.SetGlobalLevel(zerolog.DebugLevel) } else { zerolog.SetGlobalLevel(zerolog.InfoLevel) } b, err := post.NewBlog(*postDirFlag) if err != nil { log.Error().Err(err).Msg("could not init blog") return } r := router.New(b) go func() { log.Info().Msgf("listening at http://localhost:%d", *portFlag) if err := http.ListenAndServe(fmt.Sprintf(":%d", *portFlag), r); err != nil { log.Error().Err(err).Msg("could not open server") } }() ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt, os.Kill) <-ch }