Skip to content

Commit

Permalink
Add version and listen cli-flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Aug 6, 2018
1 parent 1863b9f commit 999885f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.git
http-echo*
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
)

const Version = "0.0.1"

var (
httpAddr = flag.String("listen", ":80", "Listen address")
versDisp = flag.Bool("version", false, "Display version")
)

func redirect(w http.ResponseWriter, req *http.Request) {
hostname := strings.Split(req.Host, ":")
target := "https://" + hostname[0] + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}
log.Printf("redirect to: %s", target)
log.Printf("redirect to: %s from: ", target, req.RemoteAddr)
http.Redirect(w, req, target,
http.StatusTemporaryRedirect)
}

func main() {
http.ListenAndServe(":80", http.HandlerFunc(redirect))
flag.Parse()

if *versDisp {
fmt.Printf("Version: v%s\n", Version)
os.Exit(0)
}

http.ListenAndServe(*httpAddr, http.HandlerFunc(redirect))
}

0 comments on commit 999885f

Please sign in to comment.