-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Will Alexander
committed
Aug 24, 2015
1 parent
c939b38
commit ea3395f
Showing
1 changed file
with
68 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Handler responds a request with its IP address. | ||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
host, _, err := net.SplitHostPort(r.RemoteAddr) | ||
if err != nil { | ||
http.Error(w, "unable to get IP address", 500) | ||
return | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
fportName = "port" | ||
fcertName = "cert" | ||
fkeyName = "key" | ||
) | ||
|
||
// Handler responds a request with its IP address. | ||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
host, _, err := net.SplitHostPort(r.RemoteAddr) | ||
if err != nil { | ||
http.Error(w, "unable to get IP address", 500) | ||
return | ||
} | ||
fmt.Fprintln(w, host) | ||
} | ||
|
||
func flagIsSet(name string) bool { | ||
set := false | ||
flag.Visit(func(f *flag.Flag) { | ||
if f.Name == name { | ||
set = true | ||
} | ||
}) | ||
return set | ||
} | ||
|
||
func main() { | ||
fcert := flag.String(fcertName, "", "the TLS certificate to use") | ||
fkey := flag.String(fkeyName, "", "the TLS key to use") | ||
fport := flag.Uint("port", 18768, "the port to listen on") | ||
flag.Parse() | ||
fcertSet := flagIsSet(fcertName) | ||
fkeySet := flagIsSet(fkeyName) | ||
if fcertSet && !fkeySet { | ||
log.Fatalf("%s set without %s", fcertName, fkeyName) | ||
} | ||
fmt.Fprintln(w, host) | ||
} | ||
|
||
func main() { | ||
fport := flag.Uint("port", 18768, "the port to listen on") | ||
flag.Parse() | ||
logFile, err := os.OpenFile(filepath.Base(strings.TrimSuffix(os.Args[0], | ||
filepath.Ext(os.Args[0])))+".log", | ||
os.O_APPEND|os.O_CREATE, 0666) | ||
if err != nil { | ||
log.Fatal("unable to open log file") | ||
} | ||
log.SetOutput(logFile) | ||
http.HandleFunc("/", Handler) | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *fport), nil)) | ||
} | ||
if !fcertSet && fkeySet { | ||
log.Fatalf("%s set without %s", fkeyName, fcertName) | ||
} | ||
tls := fcertSet && fkeySet | ||
logFile, err := os.OpenFile(filepath.Base(strings.TrimSuffix(os.Args[0], | ||
filepath.Ext(os.Args[0])))+".log", | ||
os.O_APPEND|os.O_CREATE, 0666) | ||
if err != nil { | ||
log.Fatal("unable to open log file") | ||
} | ||
log.SetOutput(logFile) | ||
http.HandleFunc("/", Handler) | ||
addr := fmt.Sprintf(":%d", *fport) | ||
if tls { | ||
err = http.ListenAndServeTLS(addr, *fcert, *fkey, nil) | ||
} else { | ||
err = http.ListenAndServe(addr, nil) | ||
} | ||
log.Fatal(err) | ||
} |