go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/appengine/frontend/main.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Binary frontend implements HTTP server that handles requests to 'default'
    16  // module.
    17  package main
    18  
    19  import (
    20  	"net/http"
    21  
    22  	"go.chromium.org/luci/server"
    23  	"go.chromium.org/luci/server/auth"
    24  	"go.chromium.org/luci/server/encryptedcookies"
    25  	"go.chromium.org/luci/server/module"
    26  	"go.chromium.org/luci/server/router"
    27  
    28  	adminapi "go.chromium.org/luci/cipd/api/admin/v1"
    29  	pubapi "go.chromium.org/luci/cipd/api/cipd/v1"
    30  	"go.chromium.org/luci/cipd/appengine/impl"
    31  	"go.chromium.org/luci/cipd/appengine/impl/accesslog"
    32  	"go.chromium.org/luci/cipd/appengine/ui"
    33  
    34  	// Using datastore for user sessions.
    35  	_ "go.chromium.org/luci/server/encryptedcookies/session/datastore"
    36  	// Using transactional datastore TQ tasks.
    37  	_ "go.chromium.org/luci/server/tq/txn/datastore"
    38  )
    39  
    40  func main() {
    41  	// Extra modules used by the frontend server only.
    42  	extra := []module.Module{
    43  		encryptedcookies.NewModuleFromFlags(),
    44  	}
    45  
    46  	impl.Main(extra, func(srv *server.Server, svc *impl.Services) error {
    47  		// Register non-pRPC routes, such as the client bootstrap handler and routes
    48  		// to support minimal subset of legacy API required to let old CIPD clients
    49  		// fetch packages and self-update.
    50  		svc.PublicRepo.InstallHandlers(srv.Routes, router.NewMiddlewareChain(
    51  			auth.Authenticate(&auth.GoogleOAuth2Method{
    52  				Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
    53  			}),
    54  		))
    55  
    56  		// UI pages. When running locally, serve static files ourself as well.
    57  		ui.InstallHandlers(srv, svc, "templates")
    58  		if !srv.Options.Prod {
    59  			srv.Routes.Static("/static", nil, http.Dir("./static"))
    60  		}
    61  
    62  		// All RPC services.
    63  		adminapi.RegisterAdminServer(srv, svc.AdminAPI)
    64  		pubapi.RegisterStorageServer(srv, svc.PublicCAS)
    65  		pubapi.RegisterRepositoryServer(srv, svc.PublicRepo)
    66  
    67  		// Log RPC requests to BigQuery.
    68  		srv.RegisterUnaryServerInterceptors(accesslog.NewUnaryServerInterceptor(&srv.Options))
    69  
    70  		return nil
    71  	})
    72  }