Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from secrethub/feature/overridable-html-page
Browse files Browse the repository at this point in the history
Add flag to override served page
  • Loading branch information
florisvdg authored May 18, 2020
2 parents bab6de6 + 64c65b1 commit 224707f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/page.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app

const page = `
const defaultPage = `
<!doctype html>
<html lang="en">
<head>
Expand Down
16 changes: 14 additions & 2 deletions app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
Expand All @@ -13,17 +14,28 @@ type Server struct {

appUsername string
appPassword string

page []byte
}

func NewServer(host string, port int) *Server {
func NewServer(host string, port int, pagePath string) *Server {
username := os.Getenv("DEMO_USERNAME")
password := os.Getenv("DEMO_PASSWORD")

page := []byte(defaultPage)
if pagePath != "" {
altPage, err := ioutil.ReadFile(pagePath)
if err == nil {
page = altPage
}
}

return &Server{
host: host,
port: port,
appUsername: username,
appPassword: password,
page: page,
}
}

Expand All @@ -36,7 +48,7 @@ func (s *Server) Serve() error {
}

func (s *Server) ServeIndex(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(page))
_, err := w.Write(s.page)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
Expand Down
5 changes: 4 additions & 1 deletion cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type ServeCommand struct {

host string
port int

altPage string
}

func NewServeCommand(io ui.IO) *ServeCommand {
Expand All @@ -28,12 +30,13 @@ func (cmd *ServeCommand) Register(r command.Registerer) {

clause.Flag("host", "The host to serve the webpage on").Short('h').Default("127.0.0.1").StringVar(&cmd.host)
clause.Flag("port", "The port to serve the webpage on").Default("8080").IntVar(&cmd.port)
clause.Flag("alt-page", "Path to alternative page file to serve").StringVar(&cmd.altPage)

command.BindAction(clause, cmd.Run)
}

// Run handles the command with the options as specified in the command.
func (cmd *ServeCommand) Run() error {
fmt.Fprintf(cmd.io.Stdout(), "Serving example app on http://%s:%d\n", cmd.host, cmd.port)
return app.NewServer(cmd.host, cmd.port).Serve()
return app.NewServer(cmd.host, cmd.port, cmd.altPage).Serve()
}

0 comments on commit 224707f

Please sign in to comment.