Skip to content

Commit

Permalink
use appengine go111 runtime now
Browse files Browse the repository at this point in the history
  • Loading branch information
zigo101 committed Mar 9, 2019
1 parent 4128688 commit a35db0e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 45 deletions.
25 changes: 25 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
5 changes: 2 additions & 3 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
runtime: go
api_version: go1
runtime: go111

handlers:
- url: /static
Expand All @@ -11,6 +10,6 @@ handlers:
secure: always
redirect_http_response_code: 301
- url: /.*
script: _go_app
script: auto
secure: always
redirect_http_response_code: 301
30 changes: 15 additions & 15 deletions go101.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (go101 *Go101) RenderArticlePage(w http.ResponseWriter, r *http.Request, fi
var buf bytes.Buffer
if err = t.Execute(&buf, pageParams); err == nil {
page = buf.Bytes()
} else {
} else {
page = []byte(err.Error())
}
} else if os.IsNotExist(err) {
Expand Down Expand Up @@ -223,13 +223,13 @@ func (go101 *Go101) RenderPrintPage(w http.ResponseWriter, r *http.Request, item
case "book101":
pageParams, err = buildBook101PrintParams()
}

if err == nil {
if pageParams == nil {
pageParams = map[string]interface{}{}
}
pageParams["IsLocalServer"] = isLocal

t := retrievePageTemplate(Template_PrintBook, !isLocal)
var buf bytes.Buffer
if err = t.Execute(&buf, pageParams); err == nil {
Expand All @@ -238,7 +238,7 @@ func (go101 *Go101) RenderPrintPage(w http.ResponseWriter, r *http.Request, item
}

if err != nil {
page = []byte(err.Error())
page = []byte(err.Error())
}

if !isLocal {
Expand All @@ -261,16 +261,16 @@ func (go101 *Go101) RenderPrintPage(w http.ResponseWriter, r *http.Request, item
}
w.Write(page)
}

func buildBook101PrintParams() (map[string]interface{}, error) {
article, err := retrieveArticleContent("101.html")
if err != nil {
return nil, err
}

// get all index article content by removing some lines
var builder strings.Builder
var builder bytes.Buffer // strings.Builder // GAE go111 has problems

content := string(article.Content)
i := strings.Index(content, IndexContentStart)
if i < 0 {
Expand All @@ -283,7 +283,7 @@ func buildBook101PrintParams() (map[string]interface{}, error) {
if i >= 0 {
content = content[:i]
}

for range [1000]struct{}{} {
i = strings.Index(content, LineToRemoveTag)
if i < 0 {
Expand All @@ -301,36 +301,36 @@ func buildBook101PrintParams() (map[string]interface{}, error) {
content = content[end:]
}
builder.WriteString(content)

// the index article
articles := make([]Article, 0, 100)
article.FilenameWithoutExt = "101"
article.Content = template.HTML(builder.String())
articles = append(articles, article)

// find all articles from links
content = string(article.Content)
for range [1000]struct{}{} {
i = strings.Index(content, Anchor)
if i < 0 {
break
}
content = content[i + len(Anchor):]
content = content[i+len(Anchor):]
i = strings.Index(content, _Anchor)
if i < 0 {
break
}

article, err := retrieveArticleContent(content[:i])
if err != nil {
log.Printf("retrieve article %s error: %s", content[:i], err)
} else {
articles = append(articles, article)
}
content = content[i + len(_Anchor):]

content = content[i+len(_Anchor):]
}

return map[string]interface{}{"Articles": articles}, nil
}

Expand Down
18 changes: 0 additions & 18 deletions main-appengine.go

This file was deleted.

18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !appengine

package main

import (
Expand All @@ -8,30 +6,36 @@ import (
"log"
"net"
"net/http"
"os"
"time"
)

var port = flag.Int("port", 55555, "server port")
var portFlag = flag.String("port", "55555", "server port")

func main() {
log.SetFlags(0)
flag.Parse()

l, err := net.Listen("tcp", fmt.Sprintf(":%v", *port))
port := *portFlag
if prt := os.Getenv("PORT"); prt != "" { // appengine std
port = prt
}

l, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
if err != nil {
log.Fatal(err)
}

err = openBrowser(fmt.Sprintf("http://localhost:%v", *port))
err = openBrowser(fmt.Sprintf("http://localhost:%v", port))
if err != nil {
log.Fatal(err)
}

go go101.Update()

log.Println("Server started:")
log.Printf(" http://localhost:%v (non-cached version)\n", *port)
log.Printf(" http://127.0.0.1:%v (cached version)\n", *port)
log.Printf(" http://localhost:%v (non-cached version)\n", port)
log.Printf(" http://127.0.0.1:%v (cached version)\n", port)
(&http.Server{
Handler: go101,
WriteTimeout: 10 * time.Second,
Expand Down
2 changes: 1 addition & 1 deletion static/code-prettify/2019-03-02/prettify.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion templates/print
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
});

$("pre.line-numbers").addClass("prettyprint linenums")
$("pre.no-line-numbers").addClass("prettyprint")
$("pre.disable-line-numbers111").addClass("prettyprint")
PR.prettyPrint();
}
</script>
Expand Down

0 comments on commit a35db0e

Please sign in to comment.