Skip to content

Commit

Permalink
Merge pull request #35 from friedbutter/refactor-html-template
Browse files Browse the repository at this point in the history
small refactor: to use struct for html layout pages
  • Loading branch information
javuto authored Feb 10, 2020
2 parents 27909a6 + c994d0d commit ca80abb
Showing 1 changed file with 45 additions and 96 deletions.
141 changes: 45 additions & 96 deletions cmd/admin/handlers-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ const (
ctxCSRF string = "csrftoken"
)

// TemplateFiles for building UI layout
type TemplateFiles struct {
filepaths []string
}

// NewTemplateFiles defines based on layout and default static pages
func NewTemplateFiles(base string, layoutFilename string) *TemplateFiles {
paths := []string{
base + "/" + layoutFilename,
base + "/components/page-head.html",
base + "/components/page-js.html",
base + "/components/page-header.html",
base + "/components/page-aside-left.html",
base + "/components/page-aside-right.html",
base + "/components/page-modals.html",
}
tf := TemplateFiles{filepaths: paths}
return &tf
}

// Handler for login page for GET requests
func loginGETHandler(w http.ResponseWriter, r *http.Request) {
incMetric(metricAdminReq)
Expand Down Expand Up @@ -93,14 +113,9 @@ func environmentHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/table.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "table.html").filepaths
t, err := template.ParseFiles(tempateFiles...)

if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -304,14 +319,8 @@ func queryListGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/queries.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "queries.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -363,14 +372,8 @@ func carvesRunGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/carves-run.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "carves-run.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -439,14 +442,8 @@ func carvesListGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/carves.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "carves.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -506,14 +503,8 @@ func queryLogsHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.New("queries-logs.html").ParseFiles(
templatesFilesFolder+"/queries-logs.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "queries-logs.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -594,14 +585,8 @@ func carvesDetailsHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.New("carves-details.html").ParseFiles(
templatesFilesFolder+"/carves-details.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "carves-details.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -695,14 +680,8 @@ func confGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/conf.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-modals.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "conf.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting conf template: %v", err)
Expand Down Expand Up @@ -769,14 +748,8 @@ func enrollGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/enroll.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "enroll.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting enroll template: %v", err)
Expand Down Expand Up @@ -859,14 +832,8 @@ func nodeHandler(w http.ResponseWriter, r *http.Request) {
"resultLogsLink": resultLogsLink,
}
// Prepare template
t, err := template.New("node.html").Funcs(funcMap).ParseFiles(
templatesFilesFolder+"/node.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "node.html").filepaths
t, err := template.New("node.html").Funcs(funcMap).ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting table template: %v", err)
Expand Down Expand Up @@ -928,14 +895,8 @@ func envsGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/environments.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "node.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting environments template: %v", err)
Expand Down Expand Up @@ -1000,14 +961,8 @@ func settingsGETHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Prepare template
t, err := template.ParseFiles(
templatesFilesFolder+"/settings.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "settings.html").filepaths
t, err := template.ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting environments template: %v", err)
Expand Down Expand Up @@ -1078,14 +1033,8 @@ func usersGETHandler(w http.ResponseWriter, r *http.Request) {
"inFutureTime": inFutureTime,
}
// Prepare template
t, err := template.New("users.html").Funcs(funcMap).ParseFiles(
templatesFilesFolder+"/users.html",
templatesFilesFolder+"/components/page-head.html",
templatesFilesFolder+"/components/page-js.html",
templatesFilesFolder+"/components/page-header.html",
templatesFilesFolder+"/components/page-aside-left.html",
templatesFilesFolder+"/components/page-aside-right.html",
templatesFilesFolder+"/components/page-modals.html")
tempateFiles := NewTemplateFiles(templatesFilesFolder, "users.html").filepaths
t, err := template.New("users.html").Funcs(funcMap).ParseFiles(tempateFiles...)
if err != nil {
incMetric(metricAdminErr)
log.Printf("error getting environments template: %v", err)
Expand Down

0 comments on commit ca80abb

Please sign in to comment.