diff --git a/app.yaml b/app.yaml index 51946a9..f30d7aa 100644 --- a/app.yaml +++ b/app.yaml @@ -1,6 +1,4 @@ -runtime: go -api_version: go1 - +runtime: go125 handlers: - url: /.* - script: _go_app + script: auto diff --git a/appengine.go b/appengine.go deleted file mode 100644 index 6cb4098..0000000 --- a/appengine.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2017 Google Inc. All Rights Reserved. -// -// 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. - -//+build appengine - -package main - -import ( - "io/ioutil" - "log" - "net/http" - - "google.golang.org/appengine" -) - -func main() { - vanity, err := ioutil.ReadFile("./vanity.yaml") - if err != nil { - log.Fatal(err) - } - h, err := newHandler(vanity) - if err != nil { - log.Fatal(err) - } - http.Handle("/", h) - appengine.Main() -} - -func defaultHost(r *http.Request) string { - return appengine.DefaultVersionHostname(appengine.NewContext(r)) -} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8fca8d4 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/thisissoon/govanityurls + +go 1.12 + +require gopkg.in/yaml.v2 v2.2.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bd555a3 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/handler.go b/handler.go index 8b7738d..d2064a9 100644 --- a/handler.go +++ b/handler.go @@ -68,10 +68,12 @@ func newHandler(config []byte) (*handler, error) { display: e.Display, vcs: e.VCS, } + isGithub := strings.HasPrefix(e.Repo, "https://github.com/") + isGitlab := strings.HasPrefix(e.Repo, "https://gitlab.com/") switch { case e.Display != "": // Already filled in. - case strings.HasPrefix(e.Repo, "https://github.com/"): + case isGithub, isGitlab: pc.display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", e.Repo, e.Repo, e.Repo) case strings.HasPrefix(e.Repo, "https://bitbucket.org"): pc.display = fmt.Sprintf("%v %v/src/default{/dir} %v/src/default{/dir}/{file}#{file}-{line}", e.Repo, e.Repo, e.Repo) @@ -82,7 +84,7 @@ func newHandler(config []byte) (*handler, error) { if e.VCS != "bzr" && e.VCS != "git" && e.VCS != "hg" && e.VCS != "svn" { return nil, fmt.Errorf("configuration for %v: unknown VCS %s", path, e.VCS) } - case strings.HasPrefix(e.Repo, "https://github.com/"): + case isGithub, isGitlab: pc.vcs = "git" default: return nil, fmt.Errorf("configuration for %v: cannot infer VCS from %s", path, e.Repo) diff --git a/handler_test.go b/handler_test.go index b3e9a69..64e2171 100644 --- a/handler_test.go +++ b/handler_test.go @@ -64,6 +64,16 @@ func TestHandler(t *testing.T) { goImport: "example.com/gopdf hg https://bitbucket.org/zombiezen/gopdf", goSource: "example.com/gopdf https://bitbucket.org/zombiezen/gopdf https://bitbucket.org/zombiezen/gopdf/src/default{/dir} https://bitbucket.org/zombiezen/gopdf/src/default{/dir}/{file}#{file}-{line}", }, + { + name: "display Gitlab inference", + config: "host: example.com\n" + + "paths:\n" + + " /portmidi:\n" + + " repo: https://gitlab.com/rakyll/portmidi\n", + path: "/portmidi", + goImport: "example.com/portmidi git https://gitlab.com/rakyll/portmidi", + goSource: "example.com/portmidi https://gitlab.com/rakyll/portmidi https://gitlab.com/rakyll/portmidi/tree/master{/dir} https://gitlab.com/rakyll/portmidi/blob/master{/dir}/{file}#L{line}", + }, { name: "Bitbucket Git", config: "host: example.com\n" + @@ -237,10 +247,10 @@ func TestPathConfigSetFind(t *testing.T) { want: "/y", }, { - paths: []string{"/example/helloworld", "/", "/y", "/foo"}, - query: "/x/y/", - want: "/", - subpath: "x/y/", + paths: []string{"/example/helloworld", "/", "/y", "/foo"}, + query: "/x/y/", + want: "/", + subpath: "x/y/", }, { paths: []string{"/example/helloworld", "/y", "/foo"}, diff --git a/main.go b/main.go index 6d06a81..3b6fab7 100644 --- a/main.go +++ b/main.go @@ -12,11 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -//+build !appengine - package main import ( + "fmt" "io/ioutil" "log" "net/http" @@ -42,7 +41,13 @@ func main() { log.Fatal(err) } http.Handle("/", h) - if err := http.ListenAndServe(":8080", nil); err != nil { + + port := os.Getenv("PORT") + if port == "" { + port = "8080" + log.Printf("Defaulting to port %s", port) + } + if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil { log.Fatal(err) } }