-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: forwarded message handler for the login code
- Loading branch information
Showing
11 changed files
with
134 additions
and
12 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
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
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,55 @@ | ||
package source_telegram | ||
|
||
import ( | ||
"context" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
type Service interface { | ||
Login(ctx context.Context, code int64, replicaIdx uint) (err error) | ||
} | ||
|
||
type service struct { | ||
uri string | ||
} | ||
|
||
func NewService(uri string) Service { | ||
return service{ | ||
uri: uri, | ||
} | ||
} | ||
|
||
func (svc service) Login(ctx context.Context, code int64, replicaIdx uint) (err error) { | ||
req := &LoginRequest{ | ||
Code: uint32(code), | ||
ReplicaIndex: uint32(replicaIdx), | ||
} | ||
creds := grpc.WithTransportCredentials(insecure.NewCredentials()) | ||
var success bool | ||
for { | ||
success, err = svc.loginOnce(ctx, req, creds) | ||
if err != nil { | ||
break | ||
} | ||
if success { | ||
break | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (svc service) loginOnce(ctx context.Context, req *LoginRequest, creds grpc.DialOption) (success bool, err error) { | ||
var conn *grpc.ClientConn | ||
conn, err = grpc.NewClient(svc.uri, creds) | ||
var resp *LoginResponse | ||
if err == nil { | ||
defer conn.Close() | ||
client := NewServiceClient(conn) | ||
resp, err = client.Login(ctx, req) | ||
} | ||
if err == nil { | ||
success = resp.Success | ||
} | ||
return | ||
} |
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 @@ | ||
syntax = "proto3"; | ||
|
||
package awakari.source.telegram; | ||
|
||
option go_package = "./api/grpc/source-telegram"; | ||
|
||
service Service { | ||
rpc Login(LoginRequest) returns (LoginResponse); | ||
} | ||
|
||
message LoginRequest { | ||
uint32 code = 1; | ||
uint32 replicaIndex = 2; | ||
} | ||
|
||
message LoginResponse { | ||
bool success = 1; | ||
} |
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,30 @@ | ||
package source_telegram | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
) | ||
|
||
type serviceLogging struct { | ||
svc Service | ||
log *slog.Logger | ||
} | ||
|
||
func NewServiceLogging(svc Service, log *slog.Logger) Service { | ||
return serviceLogging{ | ||
svc: svc, | ||
log: log, | ||
} | ||
} | ||
|
||
func (sl serviceLogging) Login(ctx context.Context, code int64, replicaIdx uint) (err error) { | ||
err = sl.svc.Login(ctx, code, replicaIdx) | ||
switch err { | ||
case nil: | ||
sl.log.Debug(fmt.Sprintf("api.grpc.source-telegram.Login(%d. %d): ok", code, replicaIdx)) | ||
default: | ||
sl.log.Error(fmt.Sprintf("api.grpc.source-telegram.Login(%d, %d): %s", code, replicaIdx, err)) | ||
} | ||
return | ||
} |
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
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
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
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
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
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