This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry picked 3aed625 New header design: * collapse header on mobile devices * add AMP logo * introduce hamburger button add working example of amp-install-serviceworker (#156) * add amp-install-serviceworker sample * add comments on sw-precache * add comment on how the SW works in this demo and how to confirm the result * change to cache all image, video, html * fix to point to original ABE page * fix lint of gulpfile.js * fix to cache only amp-install-serviceworker related files * add comments on benefits of SW and changed only to cache amp-install-serviceworker * nit * modify comments and changed path to sw.js in relative path * change gulpfile to organize the process to generate sw.js * change gulpfile.js to directory put sw.js in dist dir improve amp-social-share sample * add amp facebook sample * remove experimental flag * add samples for all providers Add amp-fx-flying-carpet sample (#169) Change cursor to pointer on mouseover (#179) Improve amp-accordion sample (#184) * demonstrate better show/hide * show simple with single show button Fixes #182,#183. Added ad to amp-fx-flying-carpet (#186) optimize images add AMP URL API embed add AMP URL API Sample Added sticky-ad example (#165) Use a new ad (#187) Remove the non-working link to create a fork Fix typo in NewsArticle sample disable directory file listings Fixes #82 New drawer menu using amp-sidebar Cherry picked 3aed625 New header design: * collapse header on mobile devices * add AMP logo * introduce hamburger button
- Loading branch information
1 parent
617d981
commit 278f3c6
Showing
51 changed files
with
1,045 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
conf.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright Google Inc. | ||
// | ||
// 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. | ||
|
||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"google.golang.org/appengine/log" | ||
"google.golang.org/appengine/urlfetch" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
APIHeader = "X-Goog-Api-Key" | ||
|
||
// APIURLLimit is the limit of the number of URLs in API request. | ||
// Currently it is set as 10 as default. | ||
APIURLLimit = 10 | ||
|
||
// APIEndpoint is AMP URL API (batchGet) endpoint. | ||
APIEndpoint = "https://acceleratedmobilepageurl.googleapis.com/v1/ampUrls:batchGet" | ||
) | ||
|
||
// APIHeader is required HTTP HEADER for AMP URL API. | ||
// X-Goog-Api-Key header for authorization. | ||
var UrlApiKey = getConfig().UrlApiKey | ||
|
||
// TaskIDs is tentative struct to hold stringIDs, i.e. original URLs, for a single task. | ||
type TaskIDs struct { | ||
IDs []string | ||
} | ||
|
||
// RequestBody is a struct for AMP URL API request body. On calling AMP URL batchGet API, | ||
// its HTTP request body is marshaled instance of this struct. | ||
type RequestBody struct { | ||
URLs []string `json:"urls"` | ||
} | ||
|
||
// ResponseBody is a struct for AMP URL API response body. | ||
type ResponseBody struct { | ||
AmpURLs []URLData `json:"ampUrls,omitempty"` | ||
Errors []URLError `json:"urlErrors,omitempty"` | ||
} | ||
|
||
// URLData holds AMP URL and cached AMP URL of requested original URL. | ||
type URLData struct { | ||
OriginalURL string `json:"originalUrl,omitempty"` | ||
AmpURL string `json:"ampUrl,omitempty"` | ||
CachedAmpURL string `json:"cdnAmpUrl,omitempty"` | ||
} | ||
|
||
// URLError holds error information that API returned. | ||
type URLError struct { | ||
ErrorCode string `json:"errorCode,omitempty"` | ||
ErrorMessage string `json:"errorMessage,omitempty"` | ||
OriginalURL string `json:"originalUrl,omitempty"` | ||
} | ||
|
||
// NewRequestBody creates a instance for request body of single API call. | ||
func NewRequestBody(urls []string) ([]byte, error) { | ||
b := RequestBody{ | ||
URLs: urls, | ||
} | ||
return json.Marshal(b) | ||
} | ||
|
||
// AmpURLAPIRequest generates actual http.Request for API call to get | ||
// cached AMP URLs of urls. | ||
func AmpURLAPIRequest(requestBody RequestBody) (*http.Request, error) { | ||
b, err := json.Marshal(requestBody) | ||
body := bytes.NewBuffer(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := http.NewRequest("POST", APIEndpoint, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add(APIHeader, UrlApiKey) | ||
return req, nil | ||
} | ||
|
||
// Creates a request and sends it to the AMP URL API. | ||
func Amplify(ctx context.Context, requestBody RequestBody) ([]byte, error) { | ||
log.Infof(ctx, "API Key %v", UrlApiKey) | ||
req, err := AmpURLAPIRequest(requestBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := urlfetch.Client(ctx) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2016, Google, Inc. | ||
# 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. | ||
|
||
runtime: go | ||
api_version: go1 | ||
threadsafe: yes | ||
|
||
handlers: | ||
- url: /view | ||
static_dir: view | ||
secure: always | ||
- url: /img | ||
static_dir: img | ||
secure: always | ||
- url: /third_party | ||
static_dir: third_party | ||
secure: always | ||
- url: /.* | ||
script: _go_app | ||
secure: always | ||
|
||
skip_files: | ||
- ^(.*/)?.*\.DS_Store$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Google Inc. | ||
// | ||
// 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. | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type Config struct { | ||
UrlApiKey string `json:"url-api-key"` | ||
} | ||
|
||
// Returns the app config | ||
func getConfig() Config { | ||
file, err := os.Open("conf.json") | ||
if err != nil { | ||
panic("Could not read config file " + err.Error()) | ||
} | ||
decoder := json.NewDecoder(file) | ||
config := Config{} | ||
err = decoder.Decode(&config) | ||
if err != nil { | ||
panic("Could not read config" + config.UrlApiKey) | ||
} | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Salted__�����]�侴X���.�`m�أ<��ؘ?�Y\TԢ��'��̫�؍J>yNw�}C�:\��w�xW�� | ||
|
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright Google Inc. | ||
// | ||
// 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. | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"google.golang.org/appengine" | ||
"net/http" | ||
) | ||
|
||
// init the handlers | ||
func init() { | ||
http.HandleFunc("/url", apiHandler) | ||
} | ||
|
||
// URL API handler routing calls the actual AMP URL API | ||
// which doesn't support CORS access. | ||
func apiHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Host != "amp-by-example-api.appspot.com" { | ||
w.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
ctx := appengine.NewContext(r) | ||
decoder := json.NewDecoder(r.Body) | ||
var requestBody RequestBody | ||
err := decoder.Decode(&requestBody) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Could not parse json request: " + err.Error())) | ||
} | ||
res, err := Amplify(ctx, requestBody) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Could not amplify: " + err.Error())) | ||
} | ||
w.Write(res) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.