-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (68 loc) · 1.84 KB
/
main.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
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"net"
"os"
"google.golang.org/grpc"
"github.com/iochti/auth-service/models"
pb "github.com/iochti/auth-service/proto"
"github.com/namsral/flag"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"google.golang.org/grpc/credentials"
)
var conf *oauth2.Config
var ghubCreds models.Credentials
// RedirectURL used during oauth
var RedirectURL string
// Inits the service state & conf
func init() {
flag.StringVar(&RedirectURL, "redirect_url", "http://127.0.0.1:3000/auth", "Redirect URL used during oauth")
flag.Parse()
// Gets Apps secrets & id for github
ghubCreds.Init()
fmt.Println(ghubCreds)
conf = &oauth2.Config{
ClientID: ghubCreds.Cid,
ClientSecret: ghubCreds.Csecret,
RedirectURL: RedirectURL,
Scopes: []string{"user:email"},
Endpoint: github.Endpoint,
}
}
func dieIf(err error) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "Error: %s. Try --help for help.\n", err)
os.Exit(-1)
}
func main() {
addr := flag.String("srv", ":5000", "TCP address to listen on (in host:port form)")
certFile := flag.String("cert", "", "Path to PEM-encoded certificate")
keyFile := flag.String("key", "", "Path to PEM-encoded secret key")
flag.Parse()
if flag.NArg() != 0 {
dieIf(fmt.Errorf("expecting zero arguments but got %d", flag.NArg()))
}
svc := &AuthSvc{
Conf: conf,
}
var server *grpc.Server
if *keyFile == "" && *certFile == "" {
server = grpc.NewServer()
} else if *certFile == "" {
dieIf(fmt.Errorf("key specified with no cert"))
} else if *keyFile == "" {
dieIf(fmt.Errorf("cert specified with no key"))
} else {
pair, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
dieIf(err)
creds := grpc.Creds(pair)
server = grpc.NewServer(creds)
}
lis, err := net.Listen("tcp", *addr)
dieIf(err)
pb.RegisterAuthSvcServer(server, svc)
server.Serve(lis)
}