Add run commands

This commit is contained in:
Michał 2024-04-27 15:01:54 +01:00
parent ac0a8df205
commit 4318b3ed47
5 changed files with 80 additions and 19 deletions

26
api/api.go Normal file
View file

@ -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))
}

26
cmd/cmd.go Normal file
View file

@ -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)
}
}

27
cmd/run.go Normal file
View file

@ -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,
})
}

View file

@ -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"))
}

View file

@ -5,5 +5,5 @@ import (
) )
func main() { func main() {
cmd.Serve() cmd.Parse()
} }