|
|
@ -1,12 +1,13 @@
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"html/template"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/GeertJohan/go.rice"
|
|
|
|
"github.com/gorilla/pat"
|
|
|
|
"github.com/gorilla/pat"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"github.com/urfave/negroni"
|
|
|
|
"github.com/urfave/negroni"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -59,16 +60,28 @@ func action(c *cli.Context) error {
|
|
|
|
address := c.String("address")
|
|
|
|
address := c.String("address")
|
|
|
|
certificate := c.String("certificate")
|
|
|
|
certificate := c.String("certificate")
|
|
|
|
key := c.String("key")
|
|
|
|
key := c.String("key")
|
|
|
|
handler, service := getHandler(c.StringSlice("header"))
|
|
|
|
handler, service, err := getHandler(c.StringSlice("header"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
service.logger.Printf("listening on %s\n", address)
|
|
|
|
service.logger.Printf("listening on %s\n", address)
|
|
|
|
return http.ListenAndServeTLS(address, certificate, key, handler)
|
|
|
|
return http.ListenAndServeTLS(address, certificate, key, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getHandler(headers []string) (http.Handler, *Server) {
|
|
|
|
func getHandler(headers []string) (http.Handler, *Server, error) {
|
|
|
|
|
|
|
|
logger := NewLogger()
|
|
|
|
|
|
|
|
templateBox := rice.MustFindBox("templates")
|
|
|
|
|
|
|
|
templateString, err := templateBox.String("index.tmpl")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
wrappedErr := errors.Wrap(err, "Could not read index.tmpl")
|
|
|
|
|
|
|
|
logger.Println(wrappedErr)
|
|
|
|
|
|
|
|
return nil, nil, wrappedErr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
service := &Server{
|
|
|
|
service := &Server{
|
|
|
|
headerNames: headers,
|
|
|
|
headerNames: headers,
|
|
|
|
logger: NewLogger(),
|
|
|
|
logger: NewLogger(),
|
|
|
|
tmpl: template.Must(template.New("html").Parse(htmlTemplate)),
|
|
|
|
tmpl: template.Must(template.New("index").Parse(templateString)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
recover := negroni.NewRecovery()
|
|
|
|
recover := negroni.NewRecovery()
|
|
|
@ -94,17 +107,5 @@ func getHandler(headers []string) (http.Handler, *Server) {
|
|
|
|
n.Use(service.logger)
|
|
|
|
n.Use(service.logger)
|
|
|
|
n.UseHandler(router)
|
|
|
|
n.UseHandler(router)
|
|
|
|
|
|
|
|
|
|
|
|
return n, service
|
|
|
|
return n, service, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const htmlTemplate = `<!DOCTYPE html>
|
|
|
|
|
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
|
|
|
|
<title>What is My IP?</title>
|
|
|
|
|
|
|
|
<style> .ip { text-align: center; } </style>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div class="ip">{{.IP}}</div>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>`
|
|
|
|
|
|
|
|