You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.2 KiB

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.ExitOnError)
postPath := fs.String("post-dir", "posts", "Path to posts directory")
port := fs.Int("port", 8080, "Port to serve on")
fs.String("config", "", "Config file")
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
}
b, err := post.NewBlog(*postPath)
if err != nil {
log.Error().Err(err).Msg("could not init blog")
return
}
r := router.New(b)
go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), 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
}