generated from crossplane/provider-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,008 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
LOCAL_BIN := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/.bin | ||
|
||
GOIMPORTS_VERSION=v0.8.0 | ||
SQLBOILER_VERSION=v4.14.2 | ||
MIGRATE_VERSION=v4.15.2 | ||
URLENC_VERSION=v1.1.1 | ||
GRPCURL_VERSION=v1.8.7 | ||
|
||
export PATH := ${LOCAL_BIN}:$(PATH) | ||
|
||
### General | ||
.PHONY: all | ||
all: clean tools build test | ||
|
||
.PHONY: tools | ||
tools: go-imports tidy grpcurl-install | ||
|
||
### Golang | ||
.PHONY: go-imports | ||
go-imports: | ||
GOBIN=$(LOCAL_BIN) go install golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION) | ||
|
||
.PHONY: grpcurl-install | ||
grpcurl-install: | ||
GOBIN=$(LOCAL_BIN) go install github.com/fullstorydev/grpcurl/cmd/grpcurl@$(GRPCURL_VERSION) | ||
|
||
.PHONY: build | ||
build: | ||
go build -o $(LOCAL_BIN)/$(BINARY_NAME) -a -race ./cmd/$(BINARY_NAME) | ||
|
||
.PHONY: build-fast | ||
build-fast: | ||
go build -o $(LOCAL_BIN)/$(BINARY_NAME) ./cmd/$(BINARY_NAME) | ||
|
||
.PHONY: run | ||
run: | ||
go run main.go | ||
|
||
.PHONY: test | ||
test: | ||
go test -failfast -v -vet=off -race ./... | ||
|
||
.PHONY: update | ||
update: tidy | ||
go get -u ./... | ||
|
||
.PHONY: tidy | ||
tidy: | ||
go mod tidy | ||
|
||
.PHONY: imports | ||
imports: | ||
$(LOCAL_BIN)/goimports -l -w . | ||
|
||
.PHONY: fmt | ||
fmt: | ||
go fmt ./... | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf $(LOCAL_BIN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module github.com/ksaritek/crossplane-provider-grpc/grpc-server | ||
|
||
go 1.21.2 | ||
|
||
require ( | ||
google.golang.org/grpc v1.58.2 | ||
google.golang.org/protobuf v1.31.0 | ||
) | ||
|
||
require ( | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
golang.org/x/net v0.12.0 // indirect | ||
golang.org/x/sys v0.10.0 // indirect | ||
golang.org/x/text v0.11.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= | ||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= | ||
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= | ||
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= | ||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= | ||
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= | ||
google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= | ||
google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= | ||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | ||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= | ||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= | ||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
"sync" | ||
|
||
"github.com/ksaritek/crossplane-provider-grpc/grpc-server/proto/gen/go/userapi" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/reflection" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type server struct { | ||
userapi.UnimplementedUserServiceServer | ||
cache sync.Map // concurrent map to store User entities in-memory | ||
} | ||
|
||
func (s *server) CreateUser(ctx context.Context, user *userapi.User) (*userapi.UserResponse, error) { | ||
log.Printf("CreateUser request for ID: %s\n", user.Id) | ||
|
||
_, found := s.cache.Load(user.Id) | ||
if found { | ||
err := status.Errorf( | ||
codes.AlreadyExists, | ||
"User with ID '%s' already exists.", | ||
user.Id, | ||
) | ||
log.Println(err) | ||
return nil, err | ||
} | ||
s.cache.Store(user.Id, user) | ||
|
||
log.Printf("User with ID: %s created successfully\n", user.Id) | ||
return &userapi.UserResponse{ | ||
Status: "Success", | ||
Message: "User created successfully", | ||
User: user, | ||
}, nil | ||
} | ||
|
||
func (s *server) GetUser(ctx context.Context, req *userapi.GetRequest) (*userapi.User, error) { | ||
log.Printf("GetUser request for ID: %s\n", req.Id) | ||
|
||
value, found := s.cache.Load(req.Id) | ||
if !found { | ||
err := status.Errorf( | ||
codes.NotFound, | ||
"User with ID '%s' not found.", | ||
req.Id, | ||
) | ||
log.Println(err) | ||
return nil, err | ||
} | ||
log.Printf("User with ID: %s retrieved successfully\n", req.Id) | ||
return value.(*userapi.User), nil | ||
} | ||
|
||
func (s *server) UpdateUser(ctx context.Context, user *userapi.User) (*userapi.UserResponse, error) { | ||
log.Printf("UpdateUser request for ID: %s\n", user.Id) | ||
|
||
_, found := s.cache.Load(user.Id) | ||
if !found { | ||
err := status.Errorf( | ||
codes.NotFound, | ||
"User with ID '%s' not found.", | ||
user.Id, | ||
) | ||
log.Println(err) | ||
return nil, err | ||
} | ||
s.cache.Store(user.Id, user) | ||
|
||
log.Printf("User with ID: %s updated successfully\n", user.Id) | ||
return &userapi.UserResponse{ | ||
Status: "Success", | ||
Message: "User updated successfully", | ||
User: user, | ||
}, nil | ||
} | ||
|
||
func (s *server) DeleteUser(ctx context.Context, req *userapi.GetRequest) (*userapi.UserResponse, error) { | ||
log.Printf("DeleteUser request for ID: %s\n", req.Id) | ||
|
||
_, found := s.cache.Load(req.Id) | ||
if !found { | ||
err := status.Errorf( | ||
codes.NotFound, | ||
"User with ID '%s' not found.", | ||
req.Id, | ||
) | ||
log.Println(err) | ||
return nil, err | ||
} | ||
s.cache.Delete(req.Id) | ||
|
||
log.Printf("User with ID: %s deleted successfully\n", req.Id) | ||
return &userapi.UserResponse{ | ||
Status: "Success", | ||
Message: "User deleted successfully", | ||
}, nil | ||
} | ||
|
||
func main() { | ||
lis, err := net.Listen("tcp", ":50051") | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
grpcServer := grpc.NewServer() | ||
userapi.RegisterUserServiceServer(grpcServer, &server{}) | ||
reflection.Register(grpcServer) | ||
|
||
log.Println("Server is running on port 50051") | ||
if err := grpcServer.Serve(lis); err != nil { | ||
log.Fatalf("failed to serve: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
LOCAL_BIN := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/.bin | ||
|
||
format: | ||
buf format -w | ||
|
||
clean: | ||
rm -rf gen | ||
|
||
generate: format clean | ||
buf generate | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generating Protobuf | ||
|
||
This directory contains the required proto and buf files. | ||
|
||
## Requirements | ||
|
||
- [buf](https://buf.build/docs/installation/) | ||
|
||
```shell | ||
$ brew install bufbuild/buf/buf | ||
``` | ||
|
||
## How to generate? | ||
|
||
```shell | ||
# In the /proto directory | ||
$ make generate | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: v1 | ||
managed: | ||
enabled: true | ||
go_package_prefix: | ||
default: https://github.com/ksaritek/crossplane-provider-grpc/grpc-server/proto/gen/go | ||
plugins: | ||
- plugin: buf.build/protocolbuffers/go:v1.30.0 | ||
out: gen/go | ||
opt: | ||
- paths=source_relative | ||
- plugin: buf.build/grpc/go:v1.3.0 | ||
out: gen/go | ||
opt: | ||
- paths=source_relative | ||
- require_unimplemented_servers=true | ||
- plugin: buf.build/community/pseudomuto-doc:v1.5.1 | ||
out: gen/go | ||
opt: | ||
- markdown,README.md,source_relative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
version: v1 | ||
breaking: | ||
use: | ||
- FILE | ||
lint: | ||
use: | ||
- DEFAULT | ||
except: | ||
# we want to enable our Go packages to have a pb prefix to make goimports more | ||
# intelligently handle fixing up imports and hopefully getting it right. | ||
- PACKAGE_DIRECTORY_MATCH | ||
|
||
# if we ever need a v2 we can have a second version with the .v2 version in the package name | ||
- PACKAGE_VERSION_SUFFIX | ||
service_suffix: Service | ||
allow_comment_ignores: true |
Oops, something went wrong.