|
| 1 | +// Copyright 2020 Google LLC |
| 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 | +package routes |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/google/exposure-notifications-verification-server/pkg/cache" |
| 23 | + "github.com/google/exposure-notifications-verification-server/pkg/config" |
| 24 | + "github.com/google/exposure-notifications-verification-server/pkg/controller" |
| 25 | + "github.com/google/exposure-notifications-verification-server/pkg/controller/associated" |
| 26 | + "github.com/google/exposure-notifications-verification-server/pkg/controller/middleware" |
| 27 | + "github.com/google/exposure-notifications-verification-server/pkg/controller/redirect" |
| 28 | + "github.com/google/exposure-notifications-verification-server/pkg/database" |
| 29 | + "github.com/google/exposure-notifications-verification-server/pkg/render" |
| 30 | + |
| 31 | + "github.com/google/exposure-notifications-server/pkg/logging" |
| 32 | + |
| 33 | + "github.com/gorilla/mux" |
| 34 | +) |
| 35 | + |
| 36 | +// ENXRedirect defines routes for the redirector service for ENX. |
| 37 | +func ENXRedirect( |
| 38 | + ctx context.Context, |
| 39 | + cfg *config.RedirectConfig, |
| 40 | + db *database.Database, |
| 41 | + cacher cache.Cacher, |
| 42 | +) (http.Handler, error) { |
| 43 | + // Create the router |
| 44 | + r := mux.NewRouter() |
| 45 | + |
| 46 | + // Common observability context |
| 47 | + ctx, obs := middleware.WithObservability(ctx) |
| 48 | + r.Use(obs) |
| 49 | + |
| 50 | + // Create the renderer |
| 51 | + h, err := render.New(ctx, cfg.AssetsPath, cfg.DevMode) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("failed to create renderer: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Request ID injection |
| 57 | + populateRequestID := middleware.PopulateRequestID(h) |
| 58 | + r.Use(populateRequestID) |
| 59 | + |
| 60 | + // Logger injection |
| 61 | + populateLogger := middleware.PopulateLogger(logging.FromContext(ctx)) |
| 62 | + r.Use(populateLogger) |
| 63 | + |
| 64 | + // Install common security headers |
| 65 | + r.Use(middleware.SecureHeaders(cfg.DevMode, "html")) |
| 66 | + |
| 67 | + // Enable debug headers |
| 68 | + processDebug := middleware.ProcessDebug() |
| 69 | + r.Use(processDebug) |
| 70 | + |
| 71 | + // Handle health. |
| 72 | + r.Handle("/health", controller.HandleHealthz(ctx, nil, h)).Methods("GET") |
| 73 | + |
| 74 | + // iOS and Android include functionality to associate data between web-apps |
| 75 | + // and device apps. Things like handoff between websites and apps, or |
| 76 | + // shared credentials are the common usecases. The redirect server |
| 77 | + // publishes the metadata needed to share data between the two domains to |
| 78 | + // offer a more seemless experience between the website and apps. iOS and |
| 79 | + // Android publish specs as to what this format looks like, and both live |
| 80 | + // under the /.well-known directory on the server. |
| 81 | + // |
| 82 | + // Android Specs: |
| 83 | + // https://developer.android.com/training/app-links/verify-site-associations |
| 84 | + // iOS Specs: |
| 85 | + // https://developer.apple.com/documentation/safariservices/supporting_associated_domains |
| 86 | + // |
| 87 | + { |
| 88 | + wk := r.PathPrefix("/.well-known").Subrouter() |
| 89 | + |
| 90 | + // Enable the iOS and Android redirect handler. |
| 91 | + assocController, err := associated.New(ctx, cfg, db, cacher, h) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("failed to create associated links controller: %w", err) |
| 94 | + } |
| 95 | + wk.PathPrefix("/apple-app-site-association").Handler(assocController.HandleIos()).Methods("GET") |
| 96 | + wk.PathPrefix("/assetlinks.json").Handler(assocController.HandleAndroid()).Methods("GET") |
| 97 | + } |
| 98 | + |
| 99 | + // Handle redirects. |
| 100 | + redirectController, err := redirect.New(ctx, db, cfg, cacher, h) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("failed to create redirect controller: %w", err) |
| 103 | + } |
| 104 | + r.PathPrefix("/").Handler(redirectController.HandleIndex()).Methods("GET") |
| 105 | + |
| 106 | + // Wrap the main router in the mutating middleware method. This cannot be |
| 107 | + // inserted as middleware because gorilla processes the method before |
| 108 | + // middleware. |
| 109 | + mux := http.NewServeMux() |
| 110 | + mux.Handle("/", middleware.MutateMethod()(r)) |
| 111 | + return mux, nil |
| 112 | +} |
0 commit comments