handle URLs specified on the command line
This commit is contained in:
@@ -8,8 +8,13 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
"github.com/stapelberg/qrbill"
|
"github.com/stapelberg/qrbill"
|
||||||
|
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
@@ -65,7 +70,9 @@ func logic() error {
|
|||||||
var listen = flag.String("listen", "localhost:9933", "[host]:port to listen on")
|
var listen = flag.String("listen", "localhost:9933", "[host]:port to listen on")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
http.HandleFunc("/qr", func(w http.ResponseWriter, r *http.Request) {
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.HandleFunc("/qr", func(w http.ResponseWriter, r *http.Request) {
|
||||||
prefix := "[" + r.RemoteAddr + "]"
|
prefix := "[" + r.RemoteAddr + "]"
|
||||||
format := r.FormValue("format")
|
format := r.FormValue("format")
|
||||||
log.Printf("%s handling request for %s, format=%s", prefix, r.URL.Path, format)
|
log.Printf("%s handling request for %s, format=%s", prefix, r.URL.Path, format)
|
||||||
@@ -147,15 +154,42 @@ func logic() error {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
http.Error(w, "not found", http.StatusNotFound)
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/qr?format=html", http.StatusFound)
|
http.Redirect(w, r, "/qr?format=html", http.StatusFound)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if flag.NArg() > 0 {
|
||||||
|
srv := httptest.NewServer(mux)
|
||||||
|
defer srv.Close()
|
||||||
|
for _, arg := range flag.Args() {
|
||||||
|
u, err := url.Parse(arg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
u.Host = strings.TrimPrefix(srv.URL, "http://")
|
||||||
|
resp, err := srv.Client().Get(u.String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ct := resp.Header.Get("Content-Type")
|
||||||
|
if !strings.HasPrefix(ct, "text/") &&
|
||||||
|
isatty.IsTerminal(os.Stdout.Fd()) {
|
||||||
|
fmt.Fprintf(os.Stderr, "not writing raw image data to terminal, did you forget to redirect the output?\n")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("QR Bill generation URL: http://%s/qr?format=html", *listen)
|
log.Printf("QR Bill generation URL: http://%s/qr?format=html", *listen)
|
||||||
return http.ListenAndServe(*listen, nil)
|
return http.ListenAndServe(*listen, mux)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -7,5 +7,6 @@ require (
|
|||||||
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca
|
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca
|
||||||
github.com/boombuler/barcode v1.0.0
|
github.com/boombuler/barcode v1.0.0
|
||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
|
github.com/mattn/go-isatty v0.0.12
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -6,5 +6,9 @@ github.com/boombuler/barcode v1.0.0 h1:s1TvRnXwL2xJRaccrdcBQMZxq6X7DvsMogtmJeHDd
|
|||||||
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
|||||||
Reference in New Issue
Block a user