forked from spheromak/htecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (56 loc) · 1.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/kelseyhightower/envconfig"
"log"
"net/http"
"os"
)
type Options struct {
Port int `short:"p" long:"port" description:"Port to listen on" default:"9999"`
File string `short:"c" long:"config" description:"Configfile to load" default:".proxypass.json"`
Bind string `short:"b" long:"bind" description:"Ip address to bind too" default:"127.0.0.1"`
}
type Response map[string]interface{}
func (r Response) String() (s string) {
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
s = ""
return
}
s = string(b)
return
}
func main() {
opts := Options{}
err := envconfig.Process("htecho", &opts)
if err != nil {
log.Fatalf("Error parsing ENV vars %s", err)
}
if _, err := flags.Parse(&opts); err != nil {
if err.(*flags.Error).Type == flags.ErrHelp {
os.Exit(0)
} else {
log.Println(err.Error())
os.Exit(1)
}
}
hf := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, Response{
"headers": r.Header,
"method": r.Method,
"host": r.Host,
"proto": r.Proto,
"request_uri": r.RequestURI,
"remote_addr": r.RemoteAddr,
})
}
http.HandleFunc("/", hf)
err = http.ListenAndServe(fmt.Sprintf("%s:%d", opts.Bind, opts.Port), nil)
if err != nil {
log.Fatal(err)
}
}