gpm/internal/cli/server.go

48 lines
1.0 KiB
Go

package cli
import (
"errors"
"fmt"
"net/http"
"go.jolheiser.com/gpm/internal/database"
"go.jolheiser.com/gpm/internal/router"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var Server = cli.Command{
Name: "server",
Aliases: []string{"web"},
Usage: "Start the gpm server",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "Port to run the gpm server on",
Value: 3333,
EnvVars: []string{"GPM_PORT"},
Destination: &portFlag,
},
},
Action: doServer,
}
func doServer(ctx *cli.Context) error {
if tokenFlag == "" {
return errors.New("gpm server requires --token")
}
db, err := database.Load(databaseFlag)
if err != nil {
log.Fatal().Msgf("could not load database at %q: %v", databaseFlag, err)
}
log.Info().Msgf("Running gpm server at http://localhost:%d", portFlag)
if err := http.ListenAndServe(fmt.Sprintf(":%d", portFlag), router.New(tokenFlag, ctx.App.Version, db)); err != nil {
return err
}
return nil
}