Skip to content

Commit

Permalink
Add an implementation of GetAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
korfuri committed Jul 14, 2017
1 parent f59cd72 commit 517fb81
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 46 deletions.
107 changes: 81 additions & 26 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,82 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"golang.org/x/net/context"
"io/ioutil"
"log"
"net"
"net/http"
"path/filepath"

"golang.org/x/net/context"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"github.com/korfuri/goref"
pb "github.com/korfuri/goref/cmd/serve/proto"
gorefpb "github.com/korfuri/goref/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
elastic "gopkg.in/olivere/elastic.v5"
)

const (
gatewayListenAddr = ":6061"
grpcListenAddr = "127.0.0.1:6062"
grpcListenAddr = "127.0.0.1:6062"
)

var (
elasticUrl = flag.String("elastic_url", "http://localhost:9200",
"URL of the ElasticSearch cluster.")
elasticUsername = flag.String("elastic_user", "elastic",
"Username to authenticate with ElasticSearch.")
elasticPassword = flag.String("elastic_password", "changeme",
"Password to authenticate with ElasticSearch.")
elasticIndex = flag.String("elastic_index", "goref",
"Name of the index to use in ElasticSearch.")
)

// server implements pb.GorefServer
type server struct{
type server struct {
corpora []goref.Corpus
client *elastic.Client
}

func (s server) GetAnnotations(ctx context.Context, req *pb.GetAnnotationsRequest) (*pb.GetAnnotationsResponse, error) {
return nil, nil
}
func (s server) GetFile(ctx context.Context, req *pb.GetFileRequest) (*pb.GetFileResponse, error) {
fmt.Printf("path: %s\n", req.Path)
fpath := req.Path
_, err := s.findCorpus(fpath)
if err != nil {
return nil, err
}

termQuery := elastic.NewTermQuery("to.position.filename.keyword", fpath)
action := s.client.Search().
Index(*elasticIndex).
Query(termQuery).
//Sort("user.keyword", true).
From(0).Size(1000).
Pretty(false)
searchResult, err := action.Do(ctx)
if err != nil {
return nil, err
}

res := &pb.GetAnnotationsResponse{
Path: fpath,
}
for _, hit := range searchResult.Hits.Hits {
var r gorefpb.Ref
json.Unmarshal(*hit.Source, &r)
res.Annotation = append(res.Annotation, &r)
}

return res, nil
}

func (s server) findCorpus(fpath string) (goref.Corpus, error) {
if filepath.Ext(fpath) != ".go" {
return nil, fmt.Errorf("Not found: invalid extension")
return goref.Corpus(""), fmt.Errorf("Not found: invalid extension")
}
var corpus goref.Corpus
for _, c := range s.corpora {
Expand All @@ -44,33 +86,39 @@ func (s server) GetFile(ctx context.Context, req *pb.GetFileRequest) (*pb.GetFil
}
}
if corpus == "" {
return nil, fmt.Errorf("Not found under any corpus")
return goref.Corpus(""), fmt.Errorf("Not found under any corpus")
}
return corpus, nil
}

func (s server) GetFile(ctx context.Context, req *pb.GetFileRequest) (*pb.GetFileResponse, error) {
fpath := req.Path
corpus, err := s.findCorpus(fpath)
if err != nil {
return nil, err
}
if f, err := ioutil.ReadFile(corpus.Abs(fpath)); err == nil {
return &pb.GetFileResponse{
Path: fpath,
Path: fpath,
Contents: string(f),
}, nil
} else {
return nil, fmt.Errorf("Internal server error")
}


}

func runGRPC(grpcReady chan struct{}) error {
func runGRPC(s *server, grpcReady chan struct{}) error {
lis, err := net.Listen("tcp", grpcListenAddr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGorefServer(s, &server{
corpora: goref.DefaultCorpora(),
})
srv := grpc.NewServer()
pb.RegisterGorefServer(srv, s)
// Register reflection service on gRPC server.
reflection.Register(s)
reflection.Register(srv)
grpcReady <- struct{}{}
if err := s.Serve(lis); err != nil {
if err := srv.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
return nil
Expand All @@ -81,7 +129,7 @@ func runGateway(grpcReady <-chan struct{}) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

<- grpcReady
<-grpcReady

mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
Expand All @@ -93,12 +141,19 @@ func runGateway(grpcReady <-chan struct{}) error {
return http.ListenAndServe(gatewayListenAddr, mux)
}

func Do() {
func main() {
flag.Parse()
grpcReady := make(chan struct{})
ec, err := elastic.NewClient(
elastic.SetURL(*elasticUrl),
elastic.SetBasicAuth(*elasticUsername, *elasticPassword))
if err != nil {
panic(err)
}
go runGateway(grpcReady)
runGRPC(grpcReady)
}

func main() {
Do()
s := &server{
corpora: goref.DefaultCorpora(),
client: ec,
}
runGRPC(s, grpcReady)
}
13 changes: 13 additions & 0 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"go/token"

pb "github.com/korfuri/goref/proto"
)

// A Position is similar to token.Position in that it gives an
Expand Down Expand Up @@ -84,3 +86,14 @@ func (p Position) MarshalJSON() ([]byte, error) {
return json.Marshal(withend(p))
}
}

// ToProto marshals a Position as a pb.Position
func (p Position) ToProto() *pb.Position {
return &pb.Position{
Filename: p.File,
StartLine: int32(p.PosL),
StartCol: int32(p.PosC),
EndLine: int32(p.EndL),
EndCol: int32(p.EndC),
}
}
36 changes: 16 additions & 20 deletions ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package goref
import (
"encoding/json"
"fmt"

pb "github.com/korfuri/goref/proto"
)

// A Ref is a reference to an identifier whose definition lives in
Expand Down Expand Up @@ -42,29 +44,23 @@ func (r *Ref) String() string {

// MarshalJSON implements encoding/json.Marshaler interface
func (r Ref) MarshalJSON() ([]byte, error) {
type location struct {
Position Position `json:"position"`
Pkg string `json:"package"`
Ident string `json:"ident"`
}
type refForJSON struct {
From location `json:"from"`
To location `json:"to"`
Typ RefType `json:"type"`
Version int64 `json:"version"`
}
return json.Marshal(refForJSON{
return json.Marshal(r.ToProto())
}

// ToProto marshals a Ref as a pb.Ref
func (r Ref) ToProto() *pb.Ref {
return &pb.Ref{
Version: r.FromPackage.Version,
From: location{
Position: r.FromPosition,
Pkg: r.FromPackage.Path,
From: &pb.Location{
Position: r.FromPosition.ToProto(),
Package: r.FromPackage.Path,
Ident: r.FromIdent,
},
To: location{
Position: r.ToPosition,
Pkg: r.ToPackage.Path,
To: &pb.Location{
Position: r.ToPosition.ToProto(),
Package: r.ToPackage.Path,
Ident: r.ToIdent,
},
Typ: r.RefType,
})
Type: pb.Type(r.RefType),
}
}

0 comments on commit 517fb81

Please sign in to comment.