Skip to content

Commit 7efef47

Browse files
committed
first commit code
1 parent 67e60b2 commit 7efef47

23 files changed

+6563
-0
lines changed

.gitignore

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
*.cache
14+
15+
# Output of the go coverage tool, specifically when used with LiteIDE
16+
*.out
17+
*build
18+
19+
# Dependency directories (remove the comment below to include it)
20+
# vendor/
21+
22+
# Go workspace file
23+
go.work
24+
go.work.sum
25+
26+
# env file
27+
.env
28+
29+
# Other
30+
.cache/
31+
unpackage
32+
node_modules/
33+
.idea/
34+
.DS_Store
35+
.vscode
36+
*.suo
37+
*.ntvs*
38+
*.njsproj
39+
*.sln
40+
*.sw?
41+
.node
42+
!.vscode/extensions.json
43+
44+
logs
45+
*.log
46+
47+
# resources
48+
resources/gocaptcha/fonts/*
49+
resources/gocaptcha/master_images/*
50+
resources/gocaptcha/shape_images/*
51+
resources/gocaptcha/thumb_images/*
52+
resources/gocaptcha/tile_images/*

Makefile

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Makefile for go-captcha-service-sdk project
2+
3+
# Variables
4+
BINARY_NAME=go-captcha-service-sdk
5+
GO=go
6+
7+
# Default Target
8+
.PHONY: all
9+
all: build
10+
11+
# Install golang Dependencies
12+
.PHONY: deps-go
13+
deps-go:
14+
$(GO) mod tidy
15+
$(GO) mod download
16+
@if ! command -v protoc >/dev/null; then \
17+
echo "Installing protoc..."; \
18+
$(GO) install github.com/golang/protobuf/protoc-gen-go@latest; \
19+
fi
20+
@if ! command -v protobufjs >/dev/null; then \
21+
echo "Installing protobufjs..."; \
22+
npm install -g protobufjs \
23+
fi
24+
25+
# Generate gRPC code for golang
26+
.PHONY: proto-go
27+
proto-go:
28+
protoc --go_out=./golang --go-grpc_out=./golang ./gocaptcha-service-api.proto
29+
30+
# Run tests
31+
.PHONY: test
32+
test: proto
33+
$(GO) test -v ./...
34+
35+
# Coverage report
36+
.PHONY: cover
37+
cover: proto
38+
$(GO) test -cover -coverprofile=coverage.out ./...
39+
$(GO) tool cover -html=coverage.out -o coverage.html
40+
41+
# Format code
42+
fmt:
43+
$(GO) fmt ./...
44+
45+
# Help Information
46+
.PHONY: help
47+
help:
48+
@echo "Available targets:"
49+
@echo " deps-go : Install golang dependencies"
50+
@echo " proto-go : Generate Protobuf code for golang"
51+
@echo " test : Run tests"
52+
@echo " cover : Generate test coverage report"
53+
@echo " clean : Remove build artifacts"
54+
@echo " help : Show this help message"

__example/golang/grpcclient.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
"github.com/wenlng/go-captcha-service-sdk/golang/grpcapi"
11+
"github.com/wenlng/go-captcha-service-sdk/golang/sdlb"
12+
"github.com/wenlng/go-service-discovery/loadbalancer"
13+
"github.com/wenlng/go-service-discovery/servicediscovery"
14+
)
15+
16+
// setupGrpcClient .
17+
func setupGrpcClient() (grpcapi.Client, error) {
18+
sdlbInstance, err := sdlb.NewServiceDiscoveryLB(sdlb.ClientConfig{
19+
ServiceDiscoveryType: servicediscovery.ServiceDiscoveryTypeConsul,
20+
Addrs: "localhost:8500",
21+
LoadBalancerType: loadbalancer.LoadBalancerTypeRandom,
22+
ServiceName: "go-captcha-service",
23+
})
24+
25+
if err != nil {
26+
return nil, fmt.Errorf("failed to new sdlb: %v", err)
27+
}
28+
29+
return grpcapi.NewGRPCClient(grpcapi.ClientConfig{
30+
APIKey: "my-secret-key-123",
31+
}, sdlbInstance)
32+
}
33+
34+
func TestGrpcGetData(id string) {
35+
client, err := setupGrpcClient()
36+
if err != nil {
37+
fmt.Fprintf(os.Stderr, "Failed to new sdlb: %v\n", err)
38+
return
39+
}
40+
41+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
42+
defer cancel()
43+
44+
res, err := client.GetData(ctx, id)
45+
if err != nil {
46+
fmt.Fprintf(os.Stderr, "Failed to get data: %v\n", err)
47+
return
48+
}
49+
50+
resStr, _ := json.Marshal(res)
51+
fmt.Println(">>>>>>>>", string(resStr))
52+
}
53+
54+
func TestGrpcCheckData(id, captchaKey, value string) {
55+
client, err := setupGrpcClient()
56+
if err != nil {
57+
fmt.Fprintf(os.Stderr, "Failed to new sdlb: %v\n", err)
58+
return
59+
}
60+
61+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
62+
defer cancel()
63+
64+
res, err := client.CheckData(ctx, id, captchaKey, value)
65+
if err != nil {
66+
fmt.Fprintf(os.Stderr, "Failed to check data: %v\n", err)
67+
return
68+
}
69+
70+
resStr, _ := json.Marshal(res)
71+
fmt.Println(">>>>>>>>", string(resStr))
72+
}
73+
74+
func TestGrpcGetStatusInfo(captchaKey string) {
75+
client, err := setupGrpcClient()
76+
if err != nil {
77+
fmt.Fprintf(os.Stderr, "Failed to new sdlb: %v\n", err)
78+
return
79+
}
80+
81+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
82+
defer cancel()
83+
84+
res, err := client.GetStatusInfo(ctx, captchaKey)
85+
if err != nil {
86+
fmt.Fprintf(os.Stderr, "Failed to get status info: %v\n", err)
87+
return
88+
}
89+
90+
resStr, _ := json.Marshal(res)
91+
fmt.Println(">>>>>>>>", string(resStr))
92+
}
93+
94+
func TestGrpcDelStatusInfo(captchaKey string) {
95+
client, err := setupGrpcClient()
96+
if err != nil {
97+
fmt.Fprintf(os.Stderr, "Failed to new sdlb: %v\n", err)
98+
return
99+
}
100+
101+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
102+
defer cancel()
103+
104+
res, err := client.DelStatusInfo(ctx, captchaKey)
105+
if err != nil {
106+
fmt.Fprintf(os.Stderr, "Failed to del status info: %v\n", err)
107+
return
108+
}
109+
110+
resStr, _ := json.Marshal(res)
111+
fmt.Println(">>>>>>>>", string(resStr))
112+
}

__example/golang/grpcclient_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestGrpc(t *testing.T) {
6+
TestGrpcGetData("click_dark_ch")
7+
8+
//TestGrpcCheckData("click_dark_ch", "25011d90-1cc8-11f0-b41e-8c85907c8cf5", "10,25,63,57")
9+
10+
//TestGrpcGetStatusInfo("25011d90-1cc8-11f0-b41e-8c85907c8cf5")
11+
12+
//TestGrpcDelStatusInfo("bba172f4-1c73-11f0-95b9-8c85907c8cf5")
13+
}

0 commit comments

Comments
 (0)