2021-09-03 04:31:27 +00:00
|
|
|
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})
|
|
|
|
|
2022-07-07 17:27:57 +00:00
|
|
|
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")
|
2021-09-03 04:31:27 +00:00
|
|
|
fs.String("config", "", "Config file")
|
2022-07-07 17:27:57 +00:00
|
|
|
debugFlag := fs.Bool("debug", false, "Debug mode")
|
2021-09-03 04:31:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-07 17:27:57 +00:00
|
|
|
if *debugFlag {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
} else {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := post.NewBlog(*postDirFlag)
|
2021-09-03 04:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not init blog")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := router.New(b)
|
|
|
|
go func() {
|
2022-07-07 17:27:57 +00:00
|
|
|
log.Info().Msgf("listening at http://localhost:%d", *portFlag)
|
|
|
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", *portFlag), r); err != nil {
|
2021-09-03 04:31:27 +00:00
|
|
|
log.Error().Err(err).Msg("could not open server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt, os.Kill)
|
|
|
|
<-ch
|
|
|
|
}
|