base.go 848 B

123456789101112131415161718192021222324252627282930313233343536
  1. package grpc
  2. import (
  3. "context"
  4. "google.golang.org/grpc/metadata"
  5. pb "sportfitness/base/api/grpc/base"
  6. "sportfitness/base/assembly/base/service"
  7. "strings"
  8. )
  9. type Api struct {
  10. pb.UnimplementedApiServer
  11. }
  12. func (Api) getRemoteIp(ctx context.Context) string {
  13. if md, ok := metadata.FromIncomingContext(ctx); ok {
  14. ip := md.Get("x-forwarded-for")
  15. if len(ip) > 0 {
  16. return ip[0]
  17. }
  18. }
  19. return ""
  20. }
  21. func (Api) getUserAgent(ctx context.Context) string {
  22. if md, ok := metadata.FromIncomingContext(ctx); ok {
  23. agents := md.Get("user-agent")
  24. return strings.Join(agents, "\n")
  25. }
  26. return ""
  27. }
  28. func (a Api) SignIn(ctx context.Context, q *pb.SignInRequest) (*pb.SignInReply, error) {
  29. token := service.User{}.SignInUserCodePassword(
  30. q.Name, q.Password, a.getRemoteIp(ctx), a.getUserAgent(ctx))
  31. return &pb.SignInReply{Token: token}, nil
  32. }