commit 0bb8dfdcafacc559b6de01d297d06b9c25e4f7e1 Author: Jaana Burcu Dogan Date: Sat Jun 24 16:31:36 2017 -0700 initial commit diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..ae319c7 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,23 @@ +# How to Contribute + +We'd love to accept your patches and contributions to this project. There are +just a few small guidelines you need to follow. + +## Contributor License Agreement + +Contributions to this project must be accompanied by a Contributor License +Agreement. You (or your employer) retain the copyright to your contribution, +this simply gives us permission to use and redistribute your contributions as +part of the project. Head over to to see +your current agreements on file or to sign a new one. + +You generally only need to submit a CLA once, so if you've already submitted one +(even if it was for a different project), you probably don't need to do it +again. + +## Code reviews + +All submissions, including submissions by project members, require review. We +use GitHub pull requests for this purpose. Consult +[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more +information on using pull requests. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e88ff2b --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Go Vanity URLs + +Go Vanity URLs is a simple App Engine Go app that allows you +to set custom import paths for your Go packages. + +## Quickstart + +Get the application: +``` +go get -u -d github.com/GoogleCloudPlatform/govanityurls +cd $(go env GOPATH)/github.com/GoogleCloudPlatform/govanityurls +``` + +Edit `app.yaml` with your domain and git repo information. + +``` +env_variables: + DOMAIN: go.grpcutil.org + REPO: https://github.com/rakyll/grpcutil +``` + +Deploy the app: + +``` +$ gcloud app deploy +``` diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..233d5f1 --- /dev/null +++ b/app.yaml @@ -0,0 +1,10 @@ +runtime: go +api_version: go1 + +handlers: +- url: /.* + script: _go_app + +env_variables: + DOMAIN: portmidigo.org + REPO: https://github.com/rakyll/portmidi diff --git a/main.go b/main.go new file mode 100644 index 0000000..d571c33 --- /dev/null +++ b/main.go @@ -0,0 +1,83 @@ +// 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" +) + +type pkg struct { + Import string + Repo string + Display string + CurrentPkg string +} + +var ( + domain string + repo string + display string +) + +func init() { + mustLoad("DOMAIN", &domain) + mustLoad("REPO", &repo) + if strings.Contains(repo, "github.com") { + display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", repo, repo, repo) + } + if display == "" { + mustLoad("DISPLAY", &display) + } + http.HandleFunc("/", handle) +} + +func handle(w http.ResponseWriter, r *http.Request) { + current := r.URL.Path + if err := vanityTmpl.Execute(w, &pkg{ + Import: domain, + Repo: repo, + Display: display, + CurrentPkg: current, + }); err != nil { + http.Error(w, "cannot render the page", http.StatusInternalServerError) + } +} + +var vanityTmpl, _ = template.New("vanity").Parse(` + + + + + + + + +Nothing to see here; move along. + +`) + +func mustLoad(key string, value *string) { + v := os.Getenv(key) + if v == "" { + log.Fatalf("missing %v env variable", key) + } + *value = v +}