2020-02-20 05:46:44 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-02-28 04:20:14 +00:00
|
|
|
"errors"
|
2020-02-20 05:46:44 +00:00
|
|
|
"fmt"
|
2020-10-07 04:11:36 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
"go.jolheiser.com/gpm/cmd/flags"
|
|
|
|
"go.jolheiser.com/gpm/database"
|
2020-10-07 04:11:36 +00:00
|
|
|
"go.jolheiser.com/gpm/router"
|
|
|
|
|
2020-02-20 05:46:44 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-20 19:09:56 +00:00
|
|
|
"go.jolheiser.com/beaver"
|
2020-02-20 05:46:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Server = cli.Command{
|
|
|
|
Name: "server",
|
|
|
|
Usage: "Start the gpm server",
|
|
|
|
Flags: []cli.Flag{
|
2021-02-28 04:20:14 +00:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "port",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "Port to run the gpm server on",
|
|
|
|
Value: 3333,
|
|
|
|
EnvVars: []string{"GPM_PORT"},
|
|
|
|
Destination: &flags.Port,
|
2020-02-20 05:46:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: doServer,
|
|
|
|
}
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
func doServer(_ *cli.Context) error {
|
|
|
|
if flags.Token == "" {
|
|
|
|
return errors.New("gpm server requires --token")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := database.Load(flags.Database)
|
2020-10-07 04:11:36 +00:00
|
|
|
if err != nil {
|
2021-02-28 04:20:14 +00:00
|
|
|
beaver.Fatalf("could not load database at %s: %v", flags.Database, err)
|
2020-10-07 04:11:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
beaver.Infof("Running gpm server at http://localhost:%d", flags.Port)
|
|
|
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", flags.Port), router.New(flags.Token, db)); err != nil {
|
2020-02-20 05:46:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|