mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2024-12-28 02:16:07 +00:00
Add run commands
This commit is contained in:
parent
ac0a8df205
commit
4318b3ed47
26
api/api.go
Normal file
26
api/api.go
Normal 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
26
cmd/cmd.go
Normal 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
27
cmd/run.go
Normal 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,
|
||||
})
|
||||
}
|
|
@ -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"))
|
||||
}
|
Loading…
Reference in a new issue