code generation CLI tool for catalystgo projects
go install github.com/catalystgo/cli/cmd/catalystgo@latest
docker pull catalystgo/cli:latest
Command | Short | Description |
---|---|---|
init |
i | Initialize the project files |
implement |
impl | Generate the gRPC code for the project |
version |
ver | Print the version of the tool |
help |
Print the help message |
-
Initialize the project
catalystgo init github.com/catalystgo/awesome-app
-
Create proto file
mkdir -p api/user touch api/user/user.proto
-
Insert proto file content
syntax = "proto3"; package user_pb; import "google/api/annotations.proto"; import "google/api/http.proto"; import "protoc-gen-openapiv2/options/annotations.proto"; import "protoc-gen-openapiv2/options/openapiv2.proto"; option go_package = "github.com/catalystgo/awesome-app/pkg/user"; service UserService { rpc Authenticate(AuthenticateRequest) returns (AuthenticateResponse) { option (google.api.http) = { post: "/user/authenticate" body: "*" }; option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = { description: "Authenticate a user" }; } } message AuthenticateRequest { string username = 1; string password = 2; } message AuthenticateResponse { string token = 1; }
-
Generate the gRPC code
task generate
-
Modify the content of
internal/api/user/authenticate.go
topackage user import ( "context" desc "github.com/catalystgo/awesome-app/pkg/user" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) func (i *Implementation) Authenticate(ctx context.Context, req *desc.AuthenticateRequest) (*desc.AuthenticateResponse, error) { if req.GetUsername() == "admin" && req.GetPassword() == "admin" { return &desc.AuthenticateResponse{Token: "admin-token"}, nil } return nil, status.Error(codes.Unauthenticated, "invalid credentials") }
-
Modify the content of
cmd/awesome-app/main.go
to bepackage main import ( "context" "github.com/catalystgo/catalystgo" "github.com/catalystgo/logger/logger" "github.com/catalystgo/awesome-app/internal/api/user" ) func main() { ctx := context.Background() app, err := catalystgo.New() if err != nil { logger.Fatalf(ctx, "create app: %v", err) } srv := user.NewUserService() if err := app.Run(srv); err != nil { logger.Fatalf(ctx, "run app: %v", err) } }
-
Run the app
task run
-
Test the app
# using http curl -s -X POST -d '{"username": "admin", "password": "admin"}' http://localhost:7000/user/authenticate | jq # using grpc grpcurl -plaintext -d '{"username": "admin", "password": "admin"}' localhost:8000 user_pb.UserService/Authenticate | jq
Congratz!!! You have built a golang app in seconds π₯³