Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows library users to replace the underlying listener used by the application #710

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ func Initialize(apper Apper, debug bool) (*App, error) {
return apper.App(), nil
}

var Listen = net.Listen

func Serve(app *App, r *mux.Router) {
log.Info("Going to serve...")

Expand Down Expand Up @@ -489,17 +491,23 @@ requests. We recommend supplying a valid host name.`)

go func() {
log.Info("Serving redirects on http://%s:80", bindAddress)
err = http.ListenAndServe(":80", m.HTTPHandler(nil))
listener, err := Listen("tcp", fmt.Sprintf("%s:80", bindAddress))
log.Error("Unable to listen on %s: %v", bindAddress, err)
err = http.Serve(listener, m.HTTPHandler(nil))
log.Error("Unable to start redirect server: %v", err)
}()

log.Info("Serving on https://%s:443", bindAddress)
log.Info("---")
err = s.ListenAndServeTLS("", "")
listener, err := Listen("tcp", fmt.Sprintf("%s:443", bindAddress))
log.Error("Unable to listen on %s: %v", bindAddress, err)
err = s.ServeTLS(listener, "", "")
log.Error("Unable to start https server: %v", err)
} else {
go func() {
listener, err := Listen("tcp", fmt.Sprintf("%s:80", bindAddress))
log.Error("Unable to listen on %s: %v", bindAddress, err)
log.Info("Serving redirects on http://%s:80", bindAddress)
err = http.ListenAndServe(fmt.Sprintf("%s:80", bindAddress), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, app.cfg.App.Host, http.StatusMovedPermanently)
}))
log.Error("Unable to start redirect server: %v", err)
Expand All @@ -508,7 +516,10 @@ requests. We recommend supplying a valid host name.`)
log.Info("Serving on https://%s:443", bindAddress)
log.Info("Using manual certificates")
log.Info("---")
err = http.ListenAndServeTLS(fmt.Sprintf("%s:443", bindAddress), app.cfg.Server.TLSCertPath, app.cfg.Server.TLSKeyPath, r)
listener, err := Listen("tcp", fmt.Sprintf("%s:443", bindAddress))
log.Error("Unable to listen on %s: %v", bindAddress, err)
err = http.ServeTLS(listener, r, app.cfg.Server.TLSCertPath, app.cfg.Server.TLSKeyPath)
log.Error("Unable to start https server: %v", err)
}
} else {
network := "tcp"
Expand All @@ -530,7 +541,7 @@ requests. We recommend supplying a valid host name.`)

log.Info("Serving on %s://%s", protocol, bindAddress)
log.Info("---")
listener, err := net.Listen(network, bindAddress)
listener, err := Listen(network, bindAddress)
if err != nil {
log.Error("Could not bind to address: %v", err)
os.Exit(1)
Expand All @@ -546,6 +557,7 @@ requests. We recommend supplying a valid host name.`)

defer listener.Close()
err = http.Serve(listener, r)
log.Error("Unable to start http server: %v", err)
}
if err != nil {
log.Error("Unable to start: %v", err)
Expand Down
7 changes: 5 additions & 2 deletions gopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import (

func initGopher(apper Apper) {
handler := NewWFHandler(apper)

gopher.HandleFunc("/", handler.Gopher(handleGopher))
log.Info("Serving on gopher://localhost:%d", apper.App().Config().Server.GopherPort)
gopher.ListenAndServe(fmt.Sprintf(":%d", apper.App().Config().Server.GopherPort), nil)
listener, err := Listen("tcp", fmt.Sprintf("%s:%d", apper.App().Config().Server.Bind, apper.App().Config().Server.GopherPort))
if err != nil {
panic(err)
}
gopher.Serve(listener, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting an error here when trying to compile:

gopher.go:33:9: undefined: gopher.Serve

}

// Utility function to strip the URL from the hostname provided by app.cfg.App.Host
Expand Down