// Package admin desrcibes the admin view containing references to // various managers and editors package admin import ( "bytes" "encoding/json" "fmt" "html/template" "net/http" "github.com/haturatu/ponzu/system/admin/user" "github.com/haturatu/ponzu/system/api/analytics" "github.com/haturatu/ponzu/system/db" "github.com/haturatu/ponzu/system/item" ) var startAdminHTML = ` {{ .Logo }}
` var mainAdminHTML = `
{{ if .Subview}}
{{ .Subview }}
{{ end }}` var endAdminHTML = `
` type admin struct { Logo string Types map[string]func() interface{} Subview template.HTML } // Admin ... func Admin(view []byte) (_ []byte, err error) { cfg, err := db.Config("name") if err != nil { return } if cfg == nil { cfg = []byte("") } a := admin{ Logo: string(cfg), Types: item.Types, Subview: template.HTML(view), } buf := &bytes.Buffer{} html := startAdminHTML + mainAdminHTML + endAdminHTML tmpl := template.Must(template.New("admin").Parse(html)) err = tmpl.Execute(buf, a) if err != nil { return } return buf.Bytes(), nil } var initAdminHTML = `
Welcome!
You need to initialize your system by filling out the form below. All of this information can be updated later on, but you will not be able to start without first completing this step.
Configuration
Admin Details
` // Init ... func Init() ([]byte, error) { html := startAdminHTML + initAdminHTML + endAdminHTML name, err := db.Config("name") if err != nil { return nil, err } if name == nil { name = []byte("") } a := admin{ Logo: string(name), } buf := &bytes.Buffer{} tmpl := template.Must(template.New("init").Parse(html)) err = tmpl.Execute(buf, a) if err != nil { return nil, err } return buf.Bytes(), nil } var loginAdminHTML = `
Welcome!
Please log in to the system using your email address and password.
Forgot password?
` // Login ... func Login() ([]byte, error) { html := startAdminHTML + loginAdminHTML + endAdminHTML cfg, err := db.Config("name") if err != nil { return nil, err } if cfg == nil { cfg = []byte("") } a := admin{ Logo: string(cfg), } buf := &bytes.Buffer{} tmpl := template.Must(template.New("login").Parse(html)) err = tmpl.Execute(buf, a) if err != nil { return nil, err } return buf.Bytes(), nil } var forgotPasswordHTML = `
Account Recovery
Please enter the email for your account and a recovery message will be sent to you at this address. Check your spam folder in case the message was flagged.
Already have a recovery key?
` // ForgotPassword ... func ForgotPassword() ([]byte, error) { html := startAdminHTML + forgotPasswordHTML + endAdminHTML cfg, err := db.Config("name") if err != nil { return nil, err } if cfg == nil { cfg = []byte("") } a := admin{ Logo: string(cfg), } buf := &bytes.Buffer{} tmpl := template.Must(template.New("forgotPassword").Parse(html)) err = tmpl.Execute(buf, a) if err != nil { return nil, err } return buf.Bytes(), nil } var recoveryKeyHTML = `
Account Recovery
Please check for your recovery key inside an email sent to the address you provided. Check your spam folder in case the message was flagged.
` // RecoveryKey ... func RecoveryKey() ([]byte, error) { html := startAdminHTML + recoveryKeyHTML + endAdminHTML cfg, err := db.Config("name") if err != nil { return nil, err } if cfg == nil { cfg = []byte("") } a := admin{ Logo: string(cfg), } buf := &bytes.Buffer{} tmpl := template.Must(template.New("recoveryKey").Parse(html)) err = tmpl.Execute(buf, a) if err != nil { return nil, err } return buf.Bytes(), nil } // UsersList ... func UsersList(req *http.Request) ([]byte, error) { html := `
Edit your account:
To approve changes, enter your password:
Add a new user:
Remove Admin Users
` script := ` ` // get current user out to pass as data to execute template j, err := db.CurrentUser(req) if err != nil { return nil, err } var usr user.User err = json.Unmarshal(j, &usr) if err != nil { return nil, err } // get all users to list jj, err := db.UserAll() if err != nil { return nil, err } var usrs []user.User for i := range jj { var u user.User err = json.Unmarshal(jj[i], &u) if err != nil { return nil, err } if u.Email != usr.Email { usrs = append(usrs, u) } } // make buffer to execute html into then pass buffer's bytes to Admin buf := &bytes.Buffer{} tmpl := template.Must(template.New("users").Parse(html + script)) data := map[string]interface{}{ "User": usr, "Users": usrs, } err = tmpl.Execute(buf, data) if err != nil { return nil, err } return Admin(buf.Bytes()) } var analyticsHTML = `

Data range: {{ .from }} - {{ .to }} (UTC)

API Requests
` // Dashboard returns the admin view with analytics dashboard func Dashboard() ([]byte, error) { buf := &bytes.Buffer{} data, err := analytics.ChartData() if err != nil { return nil, err } tmpl := template.Must(template.New("analytics").Parse(analyticsHTML)) err = tmpl.Execute(buf, data) if err != nil { return nil, err } return Admin(buf.Bytes()) } var err400HTML = []byte(`
400 Error: Bad Request
Sorry, the request was unable to be completed.
`) // Error400 creates a subview for a 400 error page func Error400() ([]byte, error) { return Admin(err400HTML) } var err404HTML = []byte(`
404 Error: Not Found
Sorry, the page you requested could not be found.
`) // Error404 creates a subview for a 404 error page func Error404() ([]byte, error) { return Admin(err404HTML) } var err405HTML = []byte(`
405 Error: Method Not Allowed
Sorry, the method of your request is not allowed.
`) // Error405 creates a subview for a 405 error page func Error405() ([]byte, error) { return Admin(err405HTML) } var err500HTML = []byte(`
500 Error: Internal Service Error
Sorry, something unexpectedly went wrong.
`) // Error500 creates a subview for a 500 error page func Error500() ([]byte, error) { return Admin(err500HTML) } var errMessageHTML = `
Error: %s
%s
` // ErrorMessage is a generic error message container, similar to Error500() and // others in this package, ecxept it expects the caller to provide a title and // message to describe to a view why the error is being shown func ErrorMessage(title, message string) ([]byte, error) { eHTML := fmt.Sprintf(errMessageHTML, title, message) return Admin([]byte(eHTML)) }