-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
64 lines (55 loc) · 1.33 KB
/
server.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
package main
import (
"encoding/base64"
"flag"
"fmt"
pb "github.com/leisurelicht/grpc-demo/protobuf"
grpc "google.golang.org/grpc"
"io"
"net"
)
type authServer struct{}
func (*authServer) AuthLogin(stream pb.AUTH_AuthLoginServer) error {
for {
req, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
fmt.Printf("\n收到的用户名:%s, 密码:%s.", req.Username, req.Password)
c := make(chan string)
go str2base64(c, req.Username, req.Password)
for n := range c {
resp := &pb.Response{
Result: string(n),
}
stream.Send(resp)
}
}
return nil
}
func newAuthServer() pb.AUTHServer {
return &authServer{}
}
func str2base64(c chan string, username string, password string) {
result := base64.StdEncoding.EncodeToString([]byte(username + password))
fmt.Printf("\nBase64编码后的结果为: %s.", result)
fmt.Printf("\n--------------------")
c <- result
close(c)
}
func main() {
port := flag.Int("p", 12345, "服务运行端口")
flag.Parse()
fmt.Printf("认证服务启动, 运行端口为: %d", *port)
fmt.Printf("\n--------------------")
conn, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
fmt.Printf("%v\n", err)
return
}
grpcServer := grpc.NewServer()
pb.RegisterAUTHServer(grpcServer, newAuthServer())
grpcServer.Serve(conn)
}