Enable non-AppEngine option and add tests (#3)
This commit is contained in:
parent
148449359e
commit
713c74bb24
5
.travis.yml
Normal file
5
.travis.yml
Normal file
@ -0,0 +1,5 @@
|
||||
sudo: false
|
||||
language: go
|
||||
go:
|
||||
- 1.6
|
||||
- 1.x
|
42
appengine.go
Normal file
42
appengine.go
Normal file
@ -0,0 +1,42 @@
|
||||
// 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 requestHost(r *http.Request) string {
|
||||
return appengine.DefaultVersionHostname(appengine.NewContext(r))
|
||||
}
|
87
handler.go
Normal file
87
handler.go
Normal file
@ -0,0 +1,87 @@
|
||||
// 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.
|
||||
|
||||
// govanityurls serves Go vanity URLs.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
host string
|
||||
m map[string]*struct {
|
||||
Repo string `yaml:"repo,omitempty"`
|
||||
Display string `yaml:"display,omitempty"`
|
||||
}
|
||||
}
|
||||
|
||||
func newHandler(config []byte) (*handler, error) {
|
||||
h := new(handler)
|
||||
if err := yaml.Unmarshal(config, &h.m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, e := range h.m {
|
||||
if e.Display != "" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(e.Repo, "github.com") {
|
||||
e.Display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", e.Repo, e.Repo, e.Repo)
|
||||
}
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
current := r.URL.Path
|
||||
p, ok := h.m[current]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
host := h.host
|
||||
if host == "" {
|
||||
host = requestHost(r)
|
||||
}
|
||||
if err := vanityTmpl.Execute(w, struct {
|
||||
Import string
|
||||
Repo string
|
||||
Display string
|
||||
}{
|
||||
Import: host + current,
|
||||
Repo: p.Repo,
|
||||
Display: p.Display,
|
||||
}); err != nil {
|
||||
http.Error(w, "cannot render the page", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
var vanityTmpl = template.Must(template.New("vanity").Parse(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
<meta name="go-import" content="{{.Import}} git {{.Repo}}">
|
||||
<meta name="go-source" content="{{.Import}} {{.Display}}">
|
||||
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Import}}">
|
||||
</head>
|
||||
<body>
|
||||
Nothing to see here; <a href="https://godoc.org/{{.Import}}">see the package on godoc</a>.
|
||||
</body>
|
||||
</html>`))
|
100
handler_test.go
Normal file
100
handler_test.go
Normal file
@ -0,0 +1,100 @@
|
||||
// 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.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config string
|
||||
path string
|
||||
|
||||
goImport string
|
||||
goSource string
|
||||
}{
|
||||
{
|
||||
name: "explicit display",
|
||||
config: "/portmidi:\n" +
|
||||
" repo: https://github.com/rakyll/portmidi\n" +
|
||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||
path: "/portmidi",
|
||||
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
||||
goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _",
|
||||
},
|
||||
{
|
||||
name: "display GitHub inference",
|
||||
config: "/portmidi:\n" +
|
||||
" repo: https://github.com/rakyll/portmidi\n",
|
||||
path: "/portmidi",
|
||||
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
||||
goSource: "example.com/portmidi https://github.com/rakyll/portmidi https://github.com/rakyll/portmidi/tree/master{/dir} https://github.com/rakyll/portmidi/blob/master{/dir}/{file}#L{line}",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
h, err := newHandler([]byte(test.config))
|
||||
if err != nil {
|
||||
t.Errorf("%s: newHandler: %v", test.name, err)
|
||||
continue
|
||||
}
|
||||
h.host = "example.com"
|
||||
s := httptest.NewServer(h)
|
||||
resp, err := http.Get(s.URL + test.path)
|
||||
if err != nil {
|
||||
s.Close()
|
||||
t.Errorf("%s: http.Get: %v", test.name, err)
|
||||
continue
|
||||
}
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
s.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("%s: status code = %s; want 200 OK", test.name, resp.Status)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("%s: ioutil.ReadAll: %v", test.name, err)
|
||||
continue
|
||||
}
|
||||
if got := findMeta(data, "go-import"); got != test.goImport {
|
||||
t.Errorf("%s: meta go-import = %q; want %q", test.name, got, test.goImport)
|
||||
}
|
||||
if got := findMeta(data, "go-source"); got != test.goSource {
|
||||
t.Errorf("%s: meta go-source = %q; want %q", test.name, got, test.goSource)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func findMeta(data []byte, name string) string {
|
||||
var sep []byte
|
||||
sep = append(sep, `<meta name="`...)
|
||||
sep = append(sep, name...)
|
||||
sep = append(sep, `" content="`...)
|
||||
i := bytes.Index(data, sep)
|
||||
if i == -1 {
|
||||
return ""
|
||||
}
|
||||
content := data[i+len(sep):]
|
||||
j := bytes.IndexByte(content, '"')
|
||||
if j == -1 {
|
||||
return ""
|
||||
}
|
||||
return string(content[:j])
|
||||
}
|
72
main.go
72
main.go
@ -12,77 +12,35 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package main contains an App Engine that serves vanity URLs for git repos.
|
||||
//+build !appengine
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/appengine"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var m map[string]struct {
|
||||
Repo string `yaml:"repo,omitempty"`
|
||||
Display string `yaml:"display,omitempty"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
vanity, err := ioutil.ReadFile("./vanity.yaml")
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
log.Fatal("usage: govanityurls CONFIG")
|
||||
}
|
||||
vanity, err := ioutil.ReadFile(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := yaml.Unmarshal(vanity, &m); err != nil {
|
||||
h, err := newHandler(vanity)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, e := range m {
|
||||
if e.Display != "" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(e.Repo, "github.com") {
|
||||
e.Display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", e.Repo, e.Repo, e.Repo)
|
||||
}
|
||||
}
|
||||
http.HandleFunc("/", handle)
|
||||
}
|
||||
|
||||
func handle(w http.ResponseWriter, r *http.Request) {
|
||||
current := r.URL.Path
|
||||
p, ok := m[current]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
host := appengine.DefaultVersionHostname(appengine.NewContext(r))
|
||||
if err := vanityTmpl.Execute(w, struct {
|
||||
Import string
|
||||
Repo string
|
||||
Display string
|
||||
}{
|
||||
Import: host + current,
|
||||
Repo: p.Repo,
|
||||
Display: p.Display,
|
||||
}); err != nil {
|
||||
http.Error(w, "cannot render the page", http.StatusInternalServerError)
|
||||
http.Handle("/", h)
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var vanityTmpl, _ = template.New("vanity").Parse(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
<meta name="go-import" content="{{.Import}} git {{.Repo}}">
|
||||
<meta name="go-source" content="{{.Import}} {{.Display}}">
|
||||
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Import}}">
|
||||
</head>
|
||||
<body>
|
||||
Nothing to see here; <a href="https://godoc.org/{{.Import}}">see the package on godoc</a>.
|
||||
</body>
|
||||
</html>`)
|
||||
func requestHost(r *http.Request) string {
|
||||
return r.Host
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user