1 Star 0 Fork 0

回忆LE/tour

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
appengine.go 1.86 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"io"
"log"
"net/http"
"os"
_ "github.com/Go-zh/tools/playground"
)
func gaeMain() {
prepContent = gaePrepContent
socketAddr = gaeSocketAddr
if err := initTour(".", "HTTPTransport"); err != nil {
log.Fatal(err)
}
http.Handle("/", hstsHandler(rootHandler))
http.Handle("/lesson/", hstsHandler(lessonHandler))
registerStatic(".")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// gaePrepContent returns a Reader that produces the content from the given
// Reader, but strips the prefix "#appengine: " from each line. It also drops
// any non-blank like that follows a series of 1 or more lines with the prefix.
func gaePrepContent(in io.Reader) io.Reader {
var prefix = []byte("#appengine: ")
out, w := io.Pipe()
go func() {
r := bufio.NewReader(in)
drop := false
for {
b, err := r.ReadBytes('\n')
if err != nil && err != io.EOF {
w.CloseWithError(err)
return
}
if bytes.HasPrefix(b, prefix) {
b = b[len(prefix):]
drop = true
} else if drop {
if len(b) > 1 {
b = nil
}
drop = false
}
if len(b) > 0 {
w.Write(b)
}
if err == io.EOF {
w.Close()
return
}
}
}()
return out
}
// gaeSocketAddr returns the WebSocket handler address.
// The App Engine version does not provide a WebSocket handler.
func gaeSocketAddr() string { return "" }
// hstsHandler wraps an http.HandlerFunc such that it sets the HSTS header.
func hstsHandler(fn http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload")
fn(w, r)
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LoveA/tour.git
[email protected]:LoveA/tour.git
LoveA
tour
tour
master

搜索帮助