mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-23 20:27:42 +00:00
66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/christianselig/apollo-backend/internal/api"
|
||
|
"github.com/christianselig/apollo-backend/internal/cmdutil"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"github.com/spf13/cobra"
|
||
|
)
|
||
|
|
||
|
func APICmd(ctx context.Context) *cobra.Command {
|
||
|
var port int
|
||
|
|
||
|
cmd := &cobra.Command{
|
||
|
Use: "api",
|
||
|
Args: cobra.ExactArgs(0),
|
||
|
Short: "Runs the RESTful API.",
|
||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||
|
port = 400
|
||
|
if os.Getenv("PORT") != "" {
|
||
|
port, _ = strconv.Atoi(os.Getenv("PORT"))
|
||
|
}
|
||
|
|
||
|
logger := cmdutil.NewLogrusLogger(false)
|
||
|
|
||
|
statsd, err := cmdutil.NewStatsdClient()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer statsd.Close()
|
||
|
|
||
|
db, err := cmdutil.NewDatabasePool(ctx)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer db.Close()
|
||
|
|
||
|
redis, err := cmdutil.NewRedisClient(ctx)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer redis.Close()
|
||
|
|
||
|
api := api.NewAPI(ctx, logger, statsd, db)
|
||
|
srv := api.Server(port)
|
||
|
|
||
|
go srv.ListenAndServe()
|
||
|
|
||
|
logger.WithFields(logrus.Fields{
|
||
|
"port": port,
|
||
|
}).Info("started api")
|
||
|
|
||
|
<-ctx.Done()
|
||
|
|
||
|
srv.Shutdown(ctx)
|
||
|
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
|
||
|
return cmd
|
||
|
}
|