Added base of proto handler
This commit is contained in:
parent
17c4226914
commit
90868e6168
1 changed files with 53 additions and 0 deletions
53
ProtoHandler/Handler.go
Normal file
53
ProtoHandler/Handler.go
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
package ProtoHandler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"lpm-cli/Lotus"
|
||||||
|
proto "lpm-cli/Proto/lpm/go"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createConnection() *grpc.ClientConn{
|
||||||
|
|
||||||
|
logger := Lotus.DetailedLogger("ProtoHandler", "createConnection")
|
||||||
|
|
||||||
|
//TODO Remove Hardcoded addr
|
||||||
|
conn, err := grpc.Dial("localhost:9090", grpc.WithInsecure())
|
||||||
|
if err != nil{
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func createRegistryClient() (proto.LpmRegistryClient, context.Context){
|
||||||
|
|
||||||
|
lrc := proto.NewLpmRegistryClient(createConnection())
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
|
||||||
|
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return lrc, ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func SearchPackages(platform string, architecture []string, query string) []PackageMetadata {
|
||||||
|
|
||||||
|
logger := Lotus.DetailedLogger("ProtoHandler", "sendSearchRequest")
|
||||||
|
|
||||||
|
client, ctx := createRegistryClient()
|
||||||
|
res, connErr := client.Search(ctx, &proto.SearchRequest{Platform: platform, Architecture: architecture, Query: query})
|
||||||
|
if connErr != nil{
|
||||||
|
logger.Fatal(connErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
packages := make([]PackageMetadata, len(res.GetPackages()))
|
||||||
|
|
||||||
|
for i, pkg := range res.GetPackages() {
|
||||||
|
packages[i].FillMetadata(pkg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return packages
|
||||||
|
}
|
||||||
Reference in a new issue