only parse templates for the first requests (#2180)

* only parse templates for the first requests

* fix race condition isse

* parse all templates before start the new server

* add comment for parseTemplates and remove unused code

* remove unused type
This commit is contained in:
Yingrong Zhao 2019-06-12 16:27:07 -04:00 committed by GitHub
parent e85c56518a
commit 474d9e7492
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 31 deletions

View File

@ -34,6 +34,11 @@ type Server struct {
server http.Server
templateDir string
templates struct {
home *template.Template
pageNotFound *template.Template
internalError *template.Template
}
}
// commonPages returns templates that are required for everything.
@ -46,7 +51,7 @@ func (s *Server) commonPages() []string {
}
// NewServer creates new instance of offersweb server
func NewServer(logger *zap.Logger, config Config, listener net.Listener) *Server {
func NewServer(logger *zap.Logger, config Config, listener net.Listener) (*Server, error) {
s := &Server{
log: logger,
config: config,
@ -65,7 +70,11 @@ func NewServer(logger *zap.Logger, config Config, listener net.Listener) *Server
s.templateDir = filepath.Join(s.config.StaticDir, "pages")
return s
if err := s.parseTemplates(); err != nil {
return nil, Error.Wrap(err)
}
return s, nil
}
// ServeHTTP handles index request
@ -75,7 +84,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
files := append(s.commonPages(),
err := s.templates.home.ExecuteTemplate(w, "base", nil)
if err != nil {
s.log.Error("failed to execute template", zap.Error(err))
}
}
// parseTemplates parses and stores all templates in server
func (s *Server) parseTemplates() (err error) {
homeFiles := append(s.commonPages(),
filepath.Join(s.templateDir, "home.html"),
filepath.Join(s.templateDir, "refOffers.html"),
filepath.Join(s.templateDir, "freeOffers.html"),
@ -83,51 +100,45 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
filepath.Join(s.templateDir, "foModal.html"),
)
home, err := template.New("landingPage").ParseFiles(files...)
s.templates.home, err = template.New("landingPage").ParseFiles(homeFiles...)
if err != nil {
s.serveInternalError(w, req)
return
return Error.Wrap(err)
}
err = home.ExecuteTemplate(w, "base", nil)
if err != nil {
s.log.Error("failed to execute template", zap.Error(err))
}
}
func (s *Server) serveNotFound(w http.ResponseWriter, req *http.Request) {
files := append(s.commonPages(),
pageNotFoundFiles := append(s.commonPages(),
filepath.Join(s.templateDir, "page-not-found.html"),
)
unavailable, err := template.New("page-not-found").ParseFiles(files...)
s.templates.pageNotFound, err = template.New("page-not-found").ParseFiles(pageNotFoundFiles...)
if err != nil {
s.serveInternalError(w, req)
return
return Error.Wrap(err)
}
internalErrorFiles := append(s.commonPages(),
filepath.Join(s.templateDir, "internal-server-error.html"),
)
s.templates.internalError, err = template.New("internal-server-error").ParseFiles(internalErrorFiles...)
if err != nil {
return Error.Wrap(err)
}
return nil
}
func (s *Server) serveNotFound(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
err = unavailable.ExecuteTemplate(w, "base", nil)
err := s.templates.pageNotFound.ExecuteTemplate(w, "base", nil)
if err != nil {
s.log.Error("failed to execute template", zap.Error(err))
}
}
func (s *Server) serveInternalError(w http.ResponseWriter, req *http.Request) {
files := append(s.commonPages(),
filepath.Join(s.templateDir, "internal-server-error.html"),
)
unavailable, err := template.New("internal-server-error").ParseFiles(files...)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.log.Error("failed to parse internal server error", zap.Error(err))
return
}
w.WriteHeader(http.StatusInternalServerError)
err = unavailable.ExecuteTemplate(w, "base", nil)
err := s.templates.internalError.ExecuteTemplate(w, "base", nil)
if err != nil {
s.log.Error("failed to execute template", zap.Error(err))
}

View File

@ -596,11 +596,14 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, config *Config, ve
return nil, errs.Combine(err, peer.Close())
}
peer.Marketing.Endpoint = marketingweb.NewServer(
peer.Marketing.Endpoint, err = marketingweb.NewServer(
peer.Log.Named("marketing:endpoint"),
marketingConfig,
peer.Marketing.Listener,
)
if err != nil {
return nil, errs.Combine(err, peer.Close())
}
}
return peer, nil