Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jul 18, 2019
1 parent ee1c750 commit 374d497
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
11 changes: 8 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sort"
"strings"

"google.golang.org/appengine"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -155,10 +156,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// Hostname returns the appropriate Host header for this request.
func (h *Handler) Hostname(r *http.Request) string {
if h.Host == "" {
return defaultHost(r)
if h.Host != "" {
return h.Host
}
return h.Host
appHost := appengine.DefaultVersionHostname(appengine.NewContext(r))
if appHost == "" {
return r.Host
}
return appHost
}

// StringInSlices checks if a string exists in a list of strings.
Expand Down
48 changes: 23 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,48 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"

"code.golift.io/badgedata"
_ "code.golift.io/badgedata/grafana"
"google.golang.org/appengine"
)

func main() {
var configPath string
switch len(os.Args) {
case 1:
configPath = "config.yaml"
case 2:
configPath = os.Args[1]
default:
log.Fatal("usage: govanityurls [CONFIG]")
listenAddr := os.Getenv("PORT")
if listenAddr == "" {
listenAddr = ":8080"
}
vanity, err := ioutil.ReadFile(configPath)
flag.StringVar(&listenAddr, "listen", listenAddr, "HTTP server listen address")
configPath := flag.String("config", "./config.yaml", "config file path")
flag.Usage = func() {
fmt.Println("Usage: govanityurls [-config <config-file>] [-listen <listen-address>]")
flag.PrintDefaults()
}
flag.Parse()

vanity, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatal(err)
}
h, err := newHandler(vanity)
vanityHandler, err := newHandler(vanity)
if err != nil {
log.Fatal(err)
}
http.Handle("/bd/", badgedata.Handler())
http.Handle("/", h)
port := ":" + os.Getenv("PORT")
if port == ":" {
port = ":8080"
http.Handle("/", vanityHandler)
// msg is only used to print a message. Useful to know when the app has
// finished starting and provides a clickable link to get right to it.
msg := listenAddr
if msg[0] == ':' {
msg = "127.0.0.1" + msg
}
log.Println("Listening at http://127.0.0.1" + port)
if err := http.ListenAndServe(port, nil); err != nil {
log.Println("Listening at http://" + msg)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
log.Fatal(err)
}
}

func defaultHost(r *http.Request) string {
host := appengine.DefaultVersionHostname(appengine.NewContext(r))
if host == "" {
return r.Host
}
return host
}

0 comments on commit 374d497

Please sign in to comment.