-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
69 lines (57 loc) · 1.66 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
65
66
67
68
69
package main
import (
"fmt"
"net"
"golang.org/x/net/context"
"google.golang.org/grpc"
apProto "github.com/autograde/antiplagiarism/proto"
)
type apServer struct {
env envVariables
}
func (s *apServer) CheckPlagiarism(ctx context.Context, req *apProto.ApRequest) (*apProto.ApResponse, error) {
fmt.Printf("Received request.\n")
// If request is coming from the client example, just return.
if req.GithubToken == "testtoken" {
fmt.Printf("Sending response.\n")
return &apProto.ApResponse{Success: true, Err: ""}, nil
}
var names []string
var languages []int
for i := range req.Labs {
names = append(names, req.Labs[i].Name)
languages = append(languages, int(req.Labs[i].Language))
}
args := commandLineArgs{
studentRepos: req.StudentRepos,
labNames: names,
labLanguages: languages,
githubOrg: req.GithubOrg,
githubToken: req.GithubToken,
endpoint: "",
}
success := buildAndRunCommands(&args, &s.env)
fmt.Printf("Sending response.\n")
if !success {
return &apProto.ApResponse{Success: false, Err: "Check the server command prompt for the error."}, nil
}
return &apProto.ApResponse{Success: true, Err: ""}, nil
}
func startServer(args *commandLineArgs, env *envVariables) {
listener, err := net.Listen("tcp", *endpoint)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Listener started on %v\n", *endpoint)
}
server := new(apServer)
server.env = *env
// TODO: Add transport security
grpcServer := grpc.NewServer()
apProto.RegisterApServer(grpcServer, server)
fmt.Printf("Preparing to serve incoming requests.\n")
err = grpcServer.Serve(listener)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
}