github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/webapp/interop_handler.go (about) 1 // Copyright 2022 The WPT Dashboard Project. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package webapp 6 7 import ( 8 "fmt" 9 "net/http" 10 11 "github.com/gorilla/mux" 12 "github.com/web-platform-tests/wpt.fyi/shared" 13 ) 14 15 type interopData struct { 16 Embedded bool 17 Year string 18 } 19 20 // Set of years that are valid for Interop 20XX. 21 var validYears = map[string]bool{"2021": true, "2022": true, "2023": true, "2024": true} 22 23 // Year that any invalid year will redirect to. 24 // TODO(danielrsmith): Change this redirect for next year's interop page. 25 const defaultRedirectYear = "2024" 26 27 // interopHandler handles GET requests to /interop-20XX and /compat20XX 28 func interopHandler(w http.ResponseWriter, r *http.Request) { 29 name := mux.Vars(r)["name"] 30 year := mux.Vars(r)["year"] 31 32 // /compat20XX redirects to /interop-20XX 33 needsRedirect := name == "compat" 34 if _, ok := validYears[year]; !ok { 35 year = defaultRedirectYear 36 needsRedirect = true 37 } 38 39 if needsRedirect { 40 destination := *(r.URL) 41 42 destination.Path = fmt.Sprintf("interop-%s", year) 43 http.Redirect(w, r, destination.String(), http.StatusTemporaryRedirect) 44 return 45 } 46 47 if r.Method != http.MethodGet { 48 http.Error(w, "Only GET is supported.", http.StatusMethodNotAllowed) 49 return 50 } 51 52 q := r.URL.Query() 53 embedded, err := shared.ParseBooleanParam(q, "embedded") 54 if err != nil { 55 http.Error(w, err.Error(), http.StatusBadRequest) 56 return 57 } 58 59 data := interopData{ 60 Embedded: embedded != nil && *embedded, 61 Year: year, 62 } 63 RenderTemplate(w, r, "interop.html", data) 64 }