govanityurls/main.go

101 lines
2.4 KiB
Go
Raw Normal View History

2017-06-25 01:31:36 +02:00
// 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 contains an App Engine that serves vanity URLs for a git repo.
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
2017-06-26 01:49:28 +02:00
"google.golang.org/appengine"
"io/ioutil"
"gopkg.in/yaml.v2"
2017-06-25 01:31:36 +02:00
)
2017-06-26 01:49:28 +02:00
type ypkg struct {
Repo string `yaml:"repo,omitempty"`
Display string `yaml:"display,omitempty"`
2017-06-25 01:31:36 +02:00
}
2017-06-26 01:49:28 +02:00
var m map[string]ypkg
2017-06-25 01:31:36 +02:00
func init() {
2017-06-26 01:49:28 +02:00
vanity, err := ioutil.ReadFile("./vanity.yaml")
if err != nil {
log.Fatal(err)
2017-06-25 01:31:36 +02:00
}
2017-06-26 01:49:28 +02:00
if err := yaml.Unmarshal(vanity, &m); 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)
}
2017-06-25 01:31:36 +02:00
}
http.HandleFunc("/", handle)
}
func handle(w http.ResponseWriter, r *http.Request) {
current := r.URL.Path
2017-06-26 01:49:28 +02:00
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,
2017-06-25 01:31:36 +02:00
}); err != nil {
http.Error(w, "cannot render the page", http.StatusInternalServerError)
}
}
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}}">
2017-06-26 01:49:28 +02:00
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Import}}">
2017-06-25 01:31:36 +02:00
</head>
<body>
2017-06-26 01:55:56 +02:00
Nothing to see here; <a href="https://godoc.org/{{.Import}}">see the package on godoc</a>.
2017-06-25 01:31:36 +02:00
</body>
</html>`)
func mustLoad(key string, value *string) {
v := os.Getenv(key)
if v == "" {
log.Fatalf("missing %v env variable", key)
}
*value = v
}