Move html template to it's own file

master
Buddy Sandidge 8 years ago
parent c0c6118582
commit 3b715b3753

1
.gitignore vendored

@ -1 +1,2 @@
what-is-my-ip
rice-box.go

@ -0,0 +1,11 @@
<!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>

@ -1,12 +1,13 @@
package main
import (
"errors"
"html/template"
"net/http"
"os"
"github.com/GeertJohan/go.rice"
"github.com/gorilla/pat"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/urfave/negroni"
)
@ -59,16 +60,28 @@ func action(c *cli.Context) error {
address := c.String("address")
certificate := c.String("certificate")
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)
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{
headerNames: headers,
logger: NewLogger(),
tmpl: template.Must(template.New("html").Parse(htmlTemplate)),
tmpl: template.Must(template.New("index").Parse(templateString)),
}
recover := negroni.NewRecovery()
@ -94,17 +107,5 @@ func getHandler(headers []string) (http.Handler, *Server) {
n.Use(service.logger)
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>`

Loading…
Cancel
Save