diff options
-rw-r--r-- | web/atward/indexHtml/default.nix | 54 | ||||
-rw-r--r-- | web/atward/src/main.rs | 48 | ||||
-rw-r--r-- | web/atward/src/tests.rs | 76 |
3 files changed, 5 insertions, 173 deletions
diff --git a/web/atward/indexHtml/default.nix b/web/atward/indexHtml/default.nix index 3af808b89831..801faf57dcf2 100644 --- a/web/atward/indexHtml/default.nix +++ b/web/atward/indexHtml/default.nix @@ -35,65 +35,13 @@ depot.web.tvl.template { <kbd>cl</kbd>), atward will redirect to the appropriate `tvl.fyi` domain. - ### Configuration - - Some behaviour of atward can be configured by adding query - parameters to the search string: - - * <kbd>cs=true</kbd> - use Sourcegraph instead of cgit to view code - - - In some browsers (like Firefox) users can not edit query - parameters for search engines. As an alternative configuration can - be supplied via cookies with the same names as the configuration - parameters. - - The form below can set this configuration: - <form class="cheddar-callout cheddar-todo"> - <input type="checkbox" - id="cs-setting" - name="cs-setting" - onchange="saveSetting(this, 'cs');"> - <label for="cs-setting">Use Sourcegraph instead of cgit</label> - </form> - - <noscript> - <p class="cheddar-callout cheddar-warning"> - The form above only works with Javascript enabled. Only a few - lines of Javascript are used, and they are licensed under a - free-software license (MIT). - </p> - </noscript> - ### Source code atward's source code lives at [//web/atward](https://at.tvl.fyi/?q=%2F%2Fweb%2Fatward). ''; - extraHead = '' - <script> - /* Initialise the state of all settings. */ - function loadSettings() { - loadSetting(document.getElementById('cs-setting'), 'cs'); - } - - /* Initialise the state of a setting from a cookie. */ - function loadSetting(checkbox, name) { - if (document.cookie.split(';').some(function(cookie) { - return cookie.indexOf(`''${name}=true`) >= 0; - })) { - checkbox.checked = true; - } - } - /* Persist the state of a checkbox in a cookie */ - function saveSetting(checkbox, name) { - console.log(`setting atward parameter '''''${name}' to ''${checkbox.checked.toString()}`); - document.cookie = `''${name}=''${checkbox.checked.toString()};`; - } - - document.addEventListener('DOMContentLoaded', loadSettings); - </script> + extraHead = '' <link rel="search" type="application/opensearchdescription+xml" title="TVL Search" href="https://at.tvl.fyi/opensearch.xml"> ''; } diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index eb2603a226c6..13aeff0e27d8 100644 --- a/web/atward/src/main.rs +++ b/web/atward/src/main.rs @@ -5,7 +5,6 @@ //! browsers and attempts to send users to useful locations based on //! their search query (falling back to another search engine). use regex::Regex; -use rouille::input::cookies; use rouille::{Request, Response}; #[cfg(test)] @@ -31,43 +30,14 @@ struct Handler { struct Query { /// Query string itself. query: String, - - /// Should Sourcegraph be used instead of cgit? - cs: bool, -} - -/// Helper function for setting a parameter based on a query -/// parameter. -fn query_setting(req: &Request, config: &mut bool, param: &str) { - match req.get_param(param) { - Some(s) if s == "true" => *config = true, - Some(s) if s == "false" => *config = false, - _ => {} - } } impl Query { fn from_request(req: &Request) -> Option<Query> { - // First extract the actual search query ... - let mut query = match req.get_param("q") { - Some(query) => Query { query, cs: false }, + match req.get_param("q") { + Some(query) => Some(Query { query }), None => return None, - }; - - // ... then apply settings to it. Settings in query parameters - // take precedence over cookies. - for cookie in cookies(req) { - match cookie { - ("cs", "true") => { - query.cs = true; - } - _ => {} - } } - - query_setting(req, &mut query.cs, "cs"); - - Some(query) } } @@ -76,7 +46,6 @@ impl From<&str> for Query { fn from(query: &str) -> Query { Query { query: query.to_string(), - cs: false, } } } @@ -90,10 +59,6 @@ fn cgit_url(path: &str) -> String { } } -/// Create a URL to a path in Sourcegraph. -fn sourcegraph_path_url(path: &str) -> String { - format!("https://cs.tvl.fyi/depot/-/tree/{}", path) -} /// Definition of all supported query handlers in atward. fn handlers() -> Vec<Handler> { vec![ @@ -126,16 +91,11 @@ fn handlers() -> Vec<Handler> { // TODO(tazjin): Add support for specifying lines in a query parameter Handler { pattern: Regex::new("^//(?P<path>[a-zA-Z].*)?$").unwrap(), - target: |query, captures| { + target: |_, captures| { // Pass an empty string if the path is missing, to // redirect to the depot root. let path = captures.name("path").map(|m| m.as_str()).unwrap_or(""); - - if query.cs { - Some(sourcegraph_path_url(path)) - } else { - Some(cgit_url(path)) - } + Some(cgit_url(path)) }, }, ] diff --git a/web/atward/src/tests.rs b/web/atward/src/tests.rs index a23f96ee9a74..eb205fdf9810 100644 --- a/web/atward/src/tests.rs +++ b/web/atward/src/tests.rs @@ -44,38 +44,12 @@ fn depot_path_cgit_query() { } #[test] -fn depot_path_sourcegraph_query() { - assert_eq!( - dispatch( - &handlers(), - &Query { - query: "//web/atward/default.nix".to_string(), - cs: true, - } - ), - Some("https://cs.tvl.fyi/depot/-/tree/web/atward/default.nix".to_string()), - ); - - assert_eq!( - dispatch( - &handlers(), - &Query { - query: "/not/a/depot/path".to_string(), - cs: true, - } - ), - None - ); -} - -#[test] fn depot_root_cgit_query() { assert_eq!( dispatch( &handlers(), &Query { query: "//".to_string(), - cs: false, } ), Some("https://code.tvl.fyi/tree/".to_string()), @@ -112,7 +86,6 @@ fn request_to_query() { .expect("request should parse to a query"), Query { query: "b/42".to_string(), - cs: false, }, ); @@ -123,55 +96,6 @@ fn request_to_query() { } #[test] -fn settings_from_cookie() { - assert_eq!( - Query::from_request(&Request::fake_http( - "GET", - "/?q=b%2F42", - vec![("Cookie".to_string(), "cs=true;".to_string())], - vec![] - )) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: true, - }, - ); -} - -#[test] -fn settings_from_query_parameter() { - assert_eq!( - Query::from_request(&Request::fake_http( - "GET", - "/?q=b%2F42&cs=true", - vec![], - vec![] - )) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: true, - }, - ); - - // Query parameter should override cookie - assert_eq!( - Query::from_request(&Request::fake_http( - "GET", - "/?q=b%2F42&cs=false", - vec![("Cookie".to_string(), "cs=true;".to_string())], - vec![] - )) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: false, - }, - ); -} - -#[test] fn depot_revision_query() { assert_eq!( dispatch(&handlers(), &"r/3002".into()), |