Render Soy index in run-server.sh

When the Java server was updated to render the index HTML from Soy, the
run-server.sh development server had not been upgraded, and continued to
render the static index file. As a result, the static file was kept so
that the development server would continue working. Problematically,
however, this meant that the two index files had to be kept in sync.

With this change, the Go server used by run-server.sh loads and renders
the same Soy template as the Java server. Allowing the static index to
be removed. This adds the robfig/soy Go dependency for development only.

Bug: Issue 5919
Change-Id: I5a45b5f779d79d8aa2b2725e3570b89e3a7aa9ad
This commit is contained in:
Wyatt Allen
2018-02-26 09:42:46 -08:00
parent b59fe11a9d
commit c510c42853
5 changed files with 27 additions and 57 deletions

View File

@@ -68,6 +68,12 @@ Then add go to your path:
PATH=$PATH:/usr/local/go/bin
```
Install the go Soy template library:
```
go get "github.com/robfig/soy"
```
### Running the server
To test the local UI against gerrit-review.googlesource.com:

View File

@@ -15,7 +15,6 @@ polygerrit_bundle(
],
exclude = [
"bower_components/**",
"index.html",
"test/**",
"**/*_test.html",
],
@@ -158,7 +157,6 @@ polygerrit_bundle(
],
exclude = [
"bower_components/**",
"index.html",
"test/**",
"**/*_test.html",
],

View File

@@ -1,49 +0,0 @@
<!DOCTYPE html>
<!--
@license
Copyright (C) 2015 The Android Open Source Project
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.
-->
<html lang="en">
<meta charset="utf-8">
<meta name="description" content="Gerrit Code Review">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<!--
RobotoMono fonts are used in styles/fonts.css
@see https://github.com/w3c/preload/issues/32 regarding crossorigin
-->
<link rel="preload" href="/fonts/RobotoMono-Regular.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/fonts/RobotoMono-Regular.woff" as="font" type="font/woff" crossorigin>
<link rel="preload" href="/fonts/Roboto-Regular.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/fonts/Roboto-Regular.woff" as="font" type="font/woff" crossorigin>
<link rel="preload" href="/fonts/Roboto-Medium.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/fonts/Roboto-Medium.woff" as="font" type="font/woff" crossorigin>
<link rel="stylesheet" href="/styles/fonts.css">
<link rel="stylesheet" href="/styles/main.css">
<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<!--
- Content between webcomponents-lite and the load of the main app element
- run before polymer-resin is installed so may have security consequences.
- Contact your local security engineer if you have any questions, and
- CC them on any changes that load content before gr-app.html.
-
- github.com/Polymer/polymer-resin/blob/master/getting-started.md#integrating
-->
<link rel="preload" href="/elements/gr-app.js" as="script" crossorigin="anonymous">
<link rel="import" href="/elements/gr-app.html">
<body unresolved>
<gr-app id="app"></gr-app>

View File

@@ -65,7 +65,6 @@ def polygerrit_bundle(name, srcs, outs, app):
name = name + "_top_sources",
srcs = [
"favicon.ico",
"index.html",
],
)

View File

@@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"flag"
"github.com/robfig/soy"
"io"
"io/ioutil"
"log"
@@ -36,11 +37,17 @@ var (
prod = flag.Bool("prod", false, "Serve production assets")
scheme = flag.String("scheme", "https", "URL scheme")
plugins = flag.String("plugins", "", "comma seperated plugin paths to serve")
tofu, _ = soy.NewBundle().
AddTemplateFile("../resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy").
CompileToTofu()
)
func main() {
flag.Parse()
http.HandleFunc("/index.html", handleIndex)
if *prod {
http.Handle("/", http.FileServer(http.Dir("dist")))
} else {
@@ -63,6 +70,15 @@ func main() {
log.Fatal(http.ListenAndServe(*port, &server{}))
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
var obj = map[string]interface{}{
"canonicalPath": "",
"staticResourcePath": "",
}
w.Header().Set("Content-Type", "text/html")
tofu.Render(w, "com.google.gerrit.httpd.raw.Index", obj)
}
func handleRESTProxy(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, ".html") {
w.Header().Set("Content-Type", "text/html")
@@ -211,13 +227,13 @@ var (
func (_ *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s %s\n", r.Proto, r.Method, r.RemoteAddr, r.URL)
for _, prefix := range fePaths {
if strings.HasPrefix(r.URL.Path, prefix) {
r.URL.Path = "/"
log.Println("Redirecting to /")
if strings.HasPrefix(r.URL.Path, prefix) || r.URL.Path == "/" {
r.URL.Path = "/index.html"
log.Println("Redirecting to /index.html")
break
} else if match := issueNumRE.Find([]byte(r.URL.Path)); match != nil {
r.URL.Path = "/"
log.Println("Redirecting to /")
r.URL.Path = "/index.html"
log.Println("Redirecting to /index.html")
break
}
}