-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (106 loc) · 3.68 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2017 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"net/http"
logsPb "go.chromium.org/luci/logdog/api/endpoints/coordinator/logs/v1"
"go.chromium.org/luci/logdog/appengine/coordinator/flex"
"go.chromium.org/luci/logdog/appengine/coordinator/flex/logs"
"go.chromium.org/luci/logdog/server/config"
"go.chromium.org/luci/appengine/gaeauth/server"
flexMW "go.chromium.org/luci/appengine/gaemiddleware/flex"
commonAuth "go.chromium.org/luci/auth"
"go.chromium.org/luci/common/data/rand/mathrand"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/logging/gologger"
"go.chromium.org/luci/grpc/discovery"
"go.chromium.org/luci/grpc/prpc"
"go.chromium.org/luci/server/auth"
"go.chromium.org/luci/server/router"
)
// Run installs and executes this site.
func main() {
mathrand.SeedRandomly()
// Setup process global Context.
c := context.Background()
c = gologger.StdConfig.Use(c) // Log to STDERR.
// Cache for configs.
configStore := config.Store{}
// TODO(dnj): We currently instantiate global instances of several services,
// with the current service configuration paramers (e.g., name of BigTable
// table, etc.).
//
// We should monitor config and kill a Flex instance if it's been observed to
// change. It would respawn, reload the new config, and then be good to go
// until the next change.
//
// As things stand, this configuration basically never changes, so this is
// not terribly important. However, it's worth noting that we should do this,
// and that here is probably the right place to kick off such a goroutine.
// Standard HTTP endpoints using flex LogDog services singleton.
r := router.NewWithRootContext(c)
flexMW.ReadOnlyFlex.InstallHandlers(r)
// Setup the global services, such as auth, luci-config.
c = flexMW.WithGlobal(c)
c = config.WithStore(c, &configStore)
gsvc, err := flex.NewGlobalServices(c)
if err != nil {
logging.WithError(err).Errorf(c, "Failed to setup Flex services.")
panic(err)
}
defer gsvc.Close()
baseMW := flexMW.ReadOnlyFlex.Base().Extend(
config.Middleware(&configStore),
gsvc.Base,
)
// Set up PRPC server.
svr := &prpc.Server{
AccessControl: accessControl,
}
logsServer := logs.New()
logsPb.RegisterLogsServer(svr, logsServer)
discovery.Enable(svr)
svr.InstallHandlers(r, baseMW)
// Setup HTTP endpoints.
// We support OpenID (cookie) auth for browsers and OAuth2 for everything else.
httpMW := baseMW.Extend(
auth.Authenticate(
server.CookieAuth,
&auth.GoogleOAuth2Method{Scopes: []string{commonAuth.OAuthScopeEmail}}))
r.GET("/logs/*path", httpMW, logs.GetHandler)
// Run forever.
logging.Infof(c, "Listening on port 8080...")
if err := http.ListenAndServe(":8080", r); err != nil {
logging.WithError(err).Errorf(c, "Failed HTTP listen.")
panic(err)
}
}
func accessControl(c context.Context, origin string) bool {
cfg, err := config.Config(c)
if err != nil {
logging.WithError(err).Errorf(c, "Failed to get config for access control check.")
return false
}
ccfg := cfg.GetCoordinator()
if ccfg == nil {
return false
}
for _, o := range ccfg.RpcAllowOrigins {
if o == origin {
return true
}
}
return false
}