/
handler.go
126 lines (111 loc) · 3.91 KB
/
handler.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
123
124
125
126
// Copyright 2015 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 frontend implements HTTP server that handles requests to default
// module.
package frontend
import (
"context"
"net/http"
"strings"
"github.com/golang/protobuf/proto"
"go.chromium.org/gae/service/info"
"github.com/TriggerMail/luci-go/appengine/gaeauth/server"
"github.com/TriggerMail/luci-go/appengine/gaemiddleware/standard"
"github.com/TriggerMail/luci-go/examples/appengine/helloworld_standard/proto"
"github.com/TriggerMail/luci-go/grpc/discovery"
"github.com/TriggerMail/luci-go/grpc/prpc"
"github.com/TriggerMail/luci-go/server/auth"
"github.com/TriggerMail/luci-go/server/router"
"github.com/TriggerMail/luci-go/server/templates"
)
// templateBundle is used to render HTML templates. It provides a base args
// passed to all templates.
var templateBundle = &templates.Bundle{
Loader: templates.FileSystemLoader("templates"),
DebugMode: info.IsDevAppServer,
DefaultArgs: func(c context.Context, e *templates.Extra) (templates.Args, error) {
loginURL, err := auth.LoginURL(c, e.Request.URL.RequestURI())
if err != nil {
return nil, err
}
logoutURL, err := auth.LogoutURL(c, e.Request.URL.RequestURI())
if err != nil {
return nil, err
}
isAdmin, err := auth.IsMember(c, "administrators")
if err != nil {
return nil, err
}
return templates.Args{
"AppVersion": strings.Split(info.VersionID(c), ".")[0],
"IsAnonymous": auth.CurrentIdentity(c) == "anonymous:anonymous",
"IsAdmin": isAdmin,
"User": auth.CurrentUser(c),
"LoginURL": loginURL,
"LogoutURL": logoutURL,
}, nil
},
}
// pageBase returns the middleware chain for page handlers.
func pageBase() router.MiddlewareChain {
return standard.Base().Extend(
templates.WithTemplates(templateBundle),
auth.Authenticate(server.UsersAPIAuthMethod{}),
)
}
// prpcBase returns the middleware chain for pRPC API handlers.
func prpcBase() router.MiddlewareChain {
// OAuth 2.0 with email scope is registered as a default authenticator
// by importing "github.com/TriggerMail/luci-go/appengine/gaeauth/server".
// No need to setup an authenticator here.
//
// For authorization checks, we use per-service decorators; see
// service registration code.
return standard.Base()
}
//// Routes.
func checkAPIAccess(c context.Context, methodName string, req proto.Message) (context.Context, error) {
// Implement authorization check here, for example:
//
// import "github.com/golang/protobuf/proto"
// import "google.golang.org/grpc/codes"
// import "github.com/TriggerMail/luci-go/grpc/grpcutil"
//
// hasAccess, err := auth.IsMember(c, "my-users")
// if err != nil {
// return nil, grpcutil.Errf(codes.Internal, "%s", err)
// }
// if !hasAccess {
// return nil, grpcutil.Errf(codes.PermissionDenied, "%s is not allowed to call APIs", auth.CurrentIdentity(c))
// }
return c, nil
}
func init() {
r := router.New()
standard.InstallHandlers(r)
r.GET("/", pageBase(), indexPage)
r.GET("/test/*something", pageBase(), indexPage) // to test redirect on login
var api prpc.Server
helloworld.RegisterGreeterServer(&api, &helloworld.DecoratedGreeter{
Service: &greeterService{},
Prelude: checkAPIAccess,
})
discovery.Enable(&api)
api.InstallHandlers(r, prpcBase())
http.DefaultServeMux.Handle("/", r)
}
//// Handlers.
func indexPage(c *router.Context) {
templates.MustRender(c.Context, c.Writer, "pages/index.html", nil)
}