-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (52 loc) · 1.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/dapr/go-sdk/service/common"
daprd "github.com/dapr/go-sdk/service/grpc"
"github.com/hello-slide/token-manager/token"
)
var Key []byte = []byte(os.Getenv("PUBLIC_KEY"))
func createHandler(ctx context.Context, in *common.InvocationEvent) (*common.Content, error) {
if in.ContentType != "text/plain" {
return nil, fmt.Errorf("The content-type must be `text/plain.`")
}
resultToken, err := token.Create(string(in.Data), Key)
if err != nil {
return nil, err
}
return &common.Content{
Data: []byte(resultToken),
ContentType: "text/plain",
}, nil
}
func verifyHandler(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
if in.ContentType != "text/plain" {
return nil, fmt.Errorf("The content-type must be `text/plain.`")
}
resultToken, err := token.Verify(string(in.Data), Key)
if err != nil {
return nil, err
}
return &common.Content{
Data: []byte(resultToken),
ContentType: "text/plain",
}, nil
}
func main() {
s, err := daprd.NewService(":3000")
if err != nil {
log.Fatalf("failed to start the server: %v", err)
}
if err := s.AddServiceInvocationHandler("create", createHandler); err != nil {
log.Fatalf("error adding invocation handler: %v", err)
}
if err := s.AddServiceInvocationHandler("verify", verifyHandler); err != nil {
log.Fatalf("error adding invocation handler: %v", err)
}
if err := s.Start(); err != nil {
log.Fatalf("server error: %v", err)
}
}