Files
public-api/pkg/cmds/api.go
Rhys Lloyd 21a764f298
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
fix comment
2026-02-24 14:26:25 -08:00

74 lines
1.6 KiB
Go

package cmds
import (
"net/http"
"git.itzana.me/strafesnet/public-api/pkg/api"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func NewApiCommand() *cli.Command {
return &cli.Command{
Name: "api",
Usage: "Run api service",
Action: runAPI,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Usage: "Listen port",
EnvVars: []string{"PORT"},
Value: 8080,
},
&cli.StringFlag{
Name: "dev-rpc-host",
Usage: "Host of dev rpc",
EnvVars: []string{"DEV_RPC_HOST"},
Value: "dev-service:8081",
},
&cli.StringFlag{
Name: "data-rpc-host",
Usage: "Host of data rpc",
EnvVars: []string{"DATA_RPC_HOST"},
Value: "data-service:9000",
},
&cli.StringFlag{
Name: "storage-host",
Usage: "Host of storage",
EnvVars: []string{"STORAGE_HOST"},
Value: "storage-service:9000",
},
},
}
}
func runAPI(ctx *cli.Context) error {
// Dev service client
devConn, err := grpc.Dial(ctx.String("dev-rpc-host"), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
// Data service client
dataConn, err := grpc.Dial(ctx.String("data-rpc-host"), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
// Storage service http client
storageUrl := ctx.String("storage-host")
httpClient := http.Client{
Timeout: 10,
}
return api.NewRouter(
api.WithContext(ctx),
api.WithPort(ctx.Int("port")),
api.WithDevClient(devConn),
api.WithDataClient(dataConn),
api.WithHttpClient(&httpClient),
api.WithStorageUrl(storageUrl),
)
}