Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

traceapp: take a base URL parameter in New #162

Merged
merged 7 commits into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions traceapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
htmpl "html/template"
"io/ioutil"
"log"
Expand Down Expand Up @@ -45,17 +46,35 @@ type App struct {
tmplLock sync.Mutex
tmpls map[string]*htmpl.Template

Log *log.Logger
Log *log.Logger
baseURL *url.URL
}

// New creates a new application handler. If r is nil, a new router is
// created.
func New(r *Router) *App {
//
// The given base URL is the absolute base URL under which traceapp is being
// served, e.g., "https://appdash.mysite.com" or "https://mysite.com/appdash".
// The base URL must contain a scheme and host, or else an error will be
// returned.
func New(r *Router, base *url.URL) (*App, error) {
if r == nil {
r = NewRouter(nil)
}

app := &App{Router: r, Log: log.New(os.Stderr, "appdash: ", log.LstdFlags)}
// Validate the base URL and use the root path if none was specified.
if base.Scheme == "" || base.Host == "" {
return nil, fmt.Errorf("appdash: base URL must contain both scheme and port, found %q", base.String())
Copy link
Member

Choose a reason for hiding this comment

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

you are checking host is non-nil, but then complain about the port missing.

Also, can we not just trust that the user passed in a reasonable URL? We don't actually use the scheme or host specifically right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, I've changed port to host.

Also, can we not just trust that the user passed in a reasonable URL? We don't actually use the scheme or host specifically right?

Not entirely. The whole point of this change is that we are explicitly using a scheme and host in order to link to trace permalinks. This is because traceapp really can't know where it is hosted unless someone tells it.

We could omit the check for scheme and host.. but it would make it a lot easier for users to shoot themselves in the foot.

}
if base.Path == "" {
base.Path = "/"
Copy link
Member

Choose a reason for hiding this comment

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

I'd document that you potentially modify base, or make a copy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it to make a copy.

}

app := &App{
Router: r,
Log: log.New(os.Stderr, "appdash: ", log.LstdFlags),
baseURL: base,
}

r.r.Get(RootRoute).Handler(handlerFunc(app.serveRoot))
r.r.Get(TraceRoute).Handler(handlerFunc(app.serveTrace))
Expand All @@ -71,7 +90,7 @@ func New(r *Router) *App {
// Static file serving.
r.r.Get(StaticRoute).Handler(http.StripPrefix("/static/", http.FileServer(static.Data)))

return app
return app, nil
}

// ServeHTTP implements http.Handler.
Expand Down
6 changes: 1 addition & 5 deletions traceapp/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ func (a *App) renderTemplate(w http.ResponseWriter, r *http.Request, name string

if data != nil {
// Set TemplateCommon values.
baseURL, err := a.URLTo(RootRoute)
if err != nil {
return err
}
reflect.ValueOf(data).Elem().FieldByName("TemplateCommon").Set(reflect.ValueOf(TemplateCommon{
CurrentRoute: mux.CurrentRoute(r).GetName(),
CurrentURI: r.URL,
BaseURL: baseURL,
BaseURL: a.baseURL,
}))
}

Expand Down