add csv reading, routing
This commit is contained in:
@@ -6,37 +6,27 @@ import (
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
"github.com/rs/zerolog"
|
||||
"gogs.home.aneur.in/go/ultrashorty/internal"
|
||||
)
|
||||
|
||||
type CLI struct {
|
||||
File string `arg:"" name:"file" short:"f" help:"Path to CSV redirects file." type:"path"`
|
||||
Host string `name:"host" help:"HTTP listen host." default:"${host}"`
|
||||
Port string `name:"port" short:"p" help:"HTTP listen port." default:"${port}"`
|
||||
}
|
||||
|
||||
type HttpServer struct {
|
||||
CLI *CLI
|
||||
Log zerolog.Logger
|
||||
}
|
||||
|
||||
func (srv *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
statusCode := 404
|
||||
srv.Log.Trace().Int("result", statusCode).Msg(req.URL.Path)
|
||||
w.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func main() {
|
||||
vars := kong.Vars{
|
||||
"host": "localhost",
|
||||
"port": "4000",
|
||||
}
|
||||
|
||||
cli := &CLI{}
|
||||
cli := &internal.CLI{}
|
||||
kong.Parse(cli, vars)
|
||||
|
||||
srv := &HttpServer{
|
||||
CLI: cli,
|
||||
Log: zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger(),
|
||||
redirects, err := internal.ReadCsvRedirects(cli.File)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
srv := &internal.HttpServer{
|
||||
CLI: cli,
|
||||
Log: zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger(),
|
||||
Redirects: redirects,
|
||||
}
|
||||
|
||||
addr := net.JoinHostPort(cli.Host, cli.Port)
|
||||
@@ -46,6 +36,6 @@ func main() {
|
||||
}()
|
||||
|
||||
srv.Log.Info().Msgf("Listening at %s", addr)
|
||||
err := <-errc
|
||||
err = <-errc
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user