diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..26b806b --- /dev/null +++ b/api/api.go @@ -0,0 +1,26 @@ +package api + +import ( + "github.com/Fluffy-Bean/TastyBites/front" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +type Config struct { + Host string + Logging bool +} + +func Serve(c Config) { + r := echo.New() + + if c.Logging { + r.Use(middleware.Logger()) + } + r.Use(middleware.Recover()) + + r.StaticFS("/", front.DistDir) + + r.HideBanner = true + r.Logger.Fatal(r.Start(c.Host)) +} diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..ef379c1 --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "fmt" + "os" +) + +func Parse() { + if len(os.Args) < 2 { + fmt.Println("Use -h or --help for usage") + os.Exit(1) + } + + switch os.Args[1] { + case "run": + run(os.Args[2:]) + case "-h": + case "--help": + fmt.Println("Available commands are:") + fmt.Println(" run: starts the server") + fmt.Println("\nTo specific usages, run commandName -h") + default: + fmt.Println("Use -h or --help for usage") + os.Exit(1) + } +} diff --git a/cmd/run.go b/cmd/run.go new file mode 100644 index 0000000..3d9c102 --- /dev/null +++ b/cmd/run.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "flag" + "fmt" + "os" + + "github.com/Fluffy-Bean/TastyBites/api" +) + +func run(flags []string) { + cmd := flag.NewFlagSet("run", flag.ExitOnError) + + host := cmd.String("host", "127.0.0.1:8080", "Host address, such as 0.0.0.0:8080") + logging := cmd.Bool("log", false, "Enable logging for the application") + + err := cmd.Parse(flags) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + api.Serve(api.Config{ + Host: *host, + Logging: *logging, + }) +} diff --git a/cmd/server.go b/cmd/server.go deleted file mode 100644 index 315f8c2..0000000 --- a/cmd/server.go +++ /dev/null @@ -1,18 +0,0 @@ -package cmd - -import ( - "github.com/Fluffy-Bean/TastyBites/front" - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" -) - -func Serve() { - r := echo.New() - - r.Use(middleware.Logger()) - r.Use(middleware.Recover()) - - r.StaticFS("/", front.DistDir) - - r.Logger.Fatal(r.Start(":8080")) -} diff --git a/main.go b/main.go index b583a20..f83b60a 100644 --- a/main.go +++ b/main.go @@ -5,5 +5,5 @@ import ( ) func main() { - cmd.Serve() + cmd.Parse() }