main
  1//go:build mage
  2// +build mage
  3
  4package main
  5
  6import (
  7	"context"
  8	"path/filepath"
  9
 10	"github.com/magefile/mage/mg"
 11	"github.com/magefile/mage/sh"
 12	"github.com/xlgmokha/x/pkg/x"
 13)
 14
 15// Default target to run when none is specified
 16// If not set, running mage will list available targets
 17var Default = Servers
 18
 19// Run the Identity Provider
 20func Idp() error {
 21	env := map[string]string{
 22		"SCHEME": "http",
 23		"PORT":   "8282",
 24		"HOST":   "idp.example.com:8080",
 25	}
 26	return sh.RunWithV(env, "ruby", "./bin/idp")
 27}
 28
 29// Run the UI (a.k.a Service Provider)
 30func UI() error {
 31	env := map[string]string{
 32		"SCHEME":   "http",
 33		"PORT":     "8283",
 34		"HOST":     "ui.example.com:8080",
 35		"IDP_HOST": "idp.example.com:8080",
 36	}
 37	return sh.RunWithV(env, "ruby", "./bin/ui")
 38}
 39
 40// Run the API Gateway
 41func Gateway() error {
 42	env := map[string]string{
 43		"BIND_ADDR": ":8080",
 44	}
 45	return sh.RunWithV(env, "go", "run", "./cmd/gtwy/main.go")
 46}
 47
 48// Run the REST API
 49func Api() error {
 50	env := map[string]string{
 51		"SCHEME": "http",
 52		"PORT":   "8284",
 53		"HOST":   "localhost:8284",
 54	}
 55	return sh.RunWithV(env, "ruby", "./bin/api")
 56}
 57
 58// Run the Authzd Service
 59func Authzd() error {
 60	env := map[string]string{
 61		"BIND_ADDR": ":50051",
 62	}
 63	return sh.RunWithV(env, "go", "run", "./cmd/authzd/main.go")
 64}
 65
 66// Start NATS server
 67func Nats() error {
 68	return sh.RunV(
 69		"nats-server",
 70		"--addr=127.0.0.1",
 71		"--port=4222",
 72		"--http_port=8222",
 73		"--pid=tmp/pids/nats.pid",
 74		"--jetstream",
 75		"--store_dir=tmp/nats/store",
 76	)
 77}
 78
 79// Generate gRPC from protocal buffers
 80func Protos() error {
 81	for _, file := range x.Must(filepath.Glob("./protos/*.proto")) {
 82		if err := sh.RunV(
 83			"protoc",
 84			"--proto_path=./protos",
 85			"--go_out=pkg/rpc",
 86			"--go_opt=paths=source_relative",
 87			"--go-grpc_out=pkg/rpc",
 88			"--go-grpc_opt=paths=source_relative",
 89			"--twirp_out=pkg/rpc",
 90			"--ruby_out=lib/authx/rpc",
 91			"--twirp_ruby_out=lib/authx/rpc",
 92			file,
 93		); err != nil {
 94			return err
 95		}
 96	}
 97
 98	return nil
 99}
100
101// Run All the servers
102func Servers(ctx context.Context) {
103	mg.CtxDeps(ctx, (Step{}).Server, Nats, Idp, UI, Api, Authzd, Gateway)
104}
105
106// Run the end to end tests
107func Test(ctx context.Context) error {
108	mg.CtxDeps(ctx, func() error {
109		return sh.RunV("go", "clean", "-testcache")
110	})
111	return sh.RunV("go", "test", "-shuffle=on", "-v", "./...")
112}