Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
main: adopt a command based interface
main.go | 106 ++++++++++++----------------------------------------------
diff --git a/main.go b/main.go index fc17a73e34660e36ac3d025bdf5e876b1dbb94c9..ce1d93da9f29af3f94d6644c80f527cc3410730d 100644 --- a/main.go +++ b/main.go @@ -1,100 +1,38 @@ package main import ( + "flag" "fmt" "os" - "log" - "path/filepath" - "strings" - "net/http" + "git.eletrotupi.com/git/dinheiro/cmd" +) - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" - goRedis "github.com/go-redis/redis/v8" +const usage = `usage: dinheiro <action> [options...] - "git.eletrotupi.com/git/dinheiro/config" - "git.eletrotupi.com/git/dinheiro/redis" -) + server Start the server + keys Generate a Fernet Key to encrypt and sign cookies/sessions + help Display this message +` -func FileServer(router chi.Router, path string, root http.FileSystem) { - if strings.ContainsAny(path, "{}*") { - // TODO: Should we abort here? - log.Fatal("FileServer does not permit any URL parameters.") - os.Exit(1) +func init() { + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), usage) } - - if path != "/" && path[len(path)-1] != '/' { - router.Get(path, http.RedirectHandler(path + "/", 301).ServeHTTP) - path += "/" - } - - path += "*" - - router.Get(path, func(w http.ResponseWriter, r *http.Request) { - rctx := chi.RouteContext(r.Context()) - pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*") - fs := http.StripPrefix(pathPrefix, http.FileServer(root)) - fs.ServeHTTP(w, r) - }) } -func registerStaticRoutes(router chi.Router) { - // TODO: Deal with cache busting - workDir, _ := os.Getwd() - publicDir := http.Dir(filepath.Join(workDir, "public")) - - FileServer(router, "/public", publicDir) -} - -func registerRoutes(router chi.Router) { - registerStaticRoutes(router) - - router.Get("/", func(w http.ResponseWriter, r *http.Request) { - fileData, err := os.ReadFile("./templates/index.html") +func main() { + flag.Parse() - if err != nil { - log.Fatal("Couldn't open file") + switch action := flag.Arg(0); action { + case "server": + fmt.Println("Starting server...") + cmd.Server() + case "keys": + cmd.GenerateKey() + default: + flag.Usage() + if action != "help" { os.Exit(1) } - - w.Write(fileData) - }) -} - -func main() { - if len(os.Args) < 2 { - fmt.Printf("Usage: %s server\n", os.Args[0]) - os.Exit(1) } - - appConfig := config.LoadConfig() - - router := chi.NewRouter() - - // XXX: We should probably have a struct holding all this together - redisHost, ok := appConfig.Get("redis", "redis-host") - if !ok { - log.Fatalf("Invalid redis::redis-host config") - } - - ropts, err := goRedis.ParseURL(redisHost) - if err != nil { - log.Fatal("Invalid redis host") - } - - rc := goRedis.NewClient(ropts) - router.Use(config.Middleware(appConfig)) - router.Use(redis.Middleware(rc)) - router.Use(middleware.RealIP) - router.Use(middleware.Logger) - - registerRoutes(router) - - addr := ":8555" - if len(os.Args) > 2 { - addr = os.Args[2] - } - - log.Printf("Listening on %s", addr) - http.ListenAndServe(addr, router) }