hlfw.ca

webbing

Download patch

ref: baf2c3b670574cdb96cd5a567476b1fc85c899ed
parent: 2624fc5e06b240c55dc2d23cfdd27594bed3e4f3
author: halfwit <michaelmisch1985@gmail.com>
date: Tue Nov 12 04:07:17 PST 2019

Finish the GET based plugins

--- a/pages/doctor/application.go
+++ b/pages/doctor/application.go
@@ -12,7 +12,7 @@
 		CSS:    "",
 		Path:   "doctor/application",
 		Data:   Application,
-		Extra:  plugins.ListCountries | plugins.ListServices | router.FormErrors | router.FormToken,
+		Extra:  plugins.ListCountries | plugins.ListServices | plugins.FormErrors | plugins.FormToken,
 	}
 	router.AddPage(b)
 }
--- a/pages/doctor/profile.go
+++ b/pages/doctor/profile.go
@@ -1,6 +1,7 @@
 package doctor
 
 import (
+	"github.com/olmaxmedical/olmax_go/plugins"
 	"github.com/olmaxmedical/olmax_go/router"
 	"golang.org/x/text/message"
 )
@@ -11,7 +12,7 @@
 		CSS:    "",
 		Path:   "doctor/profile",
 		Data:   Profile,
-		Extra:  router.FormErrors | router.FormToken,
+		Extra:  plugins.FormErrors | plugins.FormToken,
 	}
 	router.AddPage(b)
 }
--- a/pages/index.go
+++ b/pages/index.go
@@ -12,7 +12,7 @@
 		CSS:    "index.css",
 		Path:   "index",
 		Data:   Index,
-		Extra:  plugins.ListDoctors | router.FormErrors,
+		Extra:  plugins.ListDoctors | plugins.FormErrors,
 	}
 	router.AddPage(b)
 }
--- a/pages/login.go
+++ b/pages/login.go
@@ -1,6 +1,7 @@
 package pages
 
 import (
+	"github.com/olmaxmedical/olmax_go/plugins"
 	"github.com/olmaxmedical/olmax_go/router"
 	"golang.org/x/text/message"
 )
@@ -11,7 +12,7 @@
 		CSS:    "",
 		Path:   "login",
 		Data:   Login,
-		Extra:  router.FormErrors,
+		Extra:  plugins.FormErrors,
 	}
 	//router.AddGet(b)
 	router.AddPage(b)
--- a/pages/newpassword.go
+++ b/pages/newpassword.go
@@ -1,6 +1,7 @@
 package pages
 
 import (
+	"github.com/olmaxmedical/olmax_go/plugins"
 	"github.com/olmaxmedical/olmax_go/router"
 	"golang.org/x/text/message"
 )
@@ -11,7 +12,7 @@
 		CSS:    "",
 		Path:   "newpassword",
 		Data:   NewPassword,
-		Extra:  router.FormToken | router.FormErrors,
+		Extra:  plugins.FormToken | plugins.FormErrors,
 	}
 	router.AddPage(b)
 }
--- a/pages/patient/offer.go
+++ b/pages/patient/offer.go
@@ -12,7 +12,7 @@
 		CSS:    "",
 		Path:   "patient/offer",
 		Data:   Createoffer,
-		Extra:  plugins.ListServices | router.FormErrors,
+		Extra:  plugins.ListServices | plugins.FormErrors,
 	}
 	router.AddPage(b)
 }
--- a/pages/patient/symptoms.go
+++ b/pages/patient/symptoms.go
@@ -1,6 +1,7 @@
 package patient
 
 import (
+	"github.com/olmaxmedical/olmax_go/plugins"
 	"github.com/olmaxmedical/olmax_go/router"
 	"golang.org/x/text/message"
 )
@@ -11,7 +12,7 @@
 		CSS:    "",
 		Path:   "patient/symptoms",
 		Data:   Symptoms,
-		Extra:  router.FormErrors | router.SessionToken,
+		Extra:  plugins.FormErrors | plugins.SessionToken,
 	}
 	router.AddPage(b)
 }
--- a/pages/resetpassword.go
+++ b/pages/resetpassword.go
@@ -1,6 +1,7 @@
 package pages
 
 import (
+	"github.com/olmaxmedical/olmax_go/plugins"
 	"github.com/olmaxmedical/olmax_go/router"
 	"golang.org/x/text/message"
 )
@@ -11,7 +12,7 @@
 		CSS:    "",
 		Path:   "resetpassword",
 		Data:   ResetPassword,
-		Extra:  router.FormErrors,
+		Extra:  plugins.FormErrors,
 	}
 	router.AddPage(b)
 }
--- a/pages/signup.go
+++ b/pages/signup.go
@@ -1,6 +1,7 @@
 package pages
 
 import (
+	"github.com/olmaxmedical/olmax_go/plugins"
 	"github.com/olmaxmedical/olmax_go/router"
 	"golang.org/x/text/message"
 )
@@ -11,7 +12,7 @@
 		CSS:    "",
 		Path:   "signup",
 		Data:   Signup,
-		Extra:  router.FormErrors,
+		Extra:  plugins.FormErrors,
 	}
 	router.AddPage(b)
 }
--- a/plugins/countries.go
+++ b/plugins/countries.go
@@ -10,7 +10,7 @@
 )
 
 // ListCountries - Populate a localized spinner to select country
-const ListCountries router.IncludeExtra = 4
+const ListCountries router.PluginMask = 4
 
 // Country - Mapping token to internationalized country code
 type Country struct {
@@ -37,7 +37,7 @@
 	b := &router.Plugin{
 		Name:     "countrylist",
 		Run:      Countries,
-		Validate: CheckCountries,
+		Validate: nil,
 	}
 	router.AddPlugin(b, ListCountries)
 }
@@ -66,16 +66,12 @@
 
 // Countries - return a localized list of countries
 func Countries(_ *router.Request) map[string]interface{} {
+	// TODO(halfwit): Use Request to get a localized country name
 	c := make(map[string]interface{})
 	for _, item := range cache.list {
 		c[item.Name.Common] = item.Name.Common
 	}
 	return c
-}
-
-// CheckCountries - no-op
-func CheckCountries() error {
-	return nil
 }
 
 // TODO: Export this so it's available to form parsing as a bitmask
--- a/plugins/doctor.go
+++ b/plugins/doctor.go
@@ -5,7 +5,7 @@
 )
 
 // ListDoctors - Bitmask to list doctors of in client country
-const ListDoctors router.IncludeExtra = 3
+const ListDoctors router.PluginMask = 3
 
 type doctor struct {
 	Image     string
@@ -22,14 +22,9 @@
 	b := &router.Plugin{
 		Name:     "doctors",
 		Run:      ListDocs,
-		Validate: ValidateListDocs,
+		Validate: nil,
 	}
 	router.AddPlugin(b, ListDoctors)
-}
-
-// ValidateListDocs - Check that db entries exist
-func ValidateListDocs() error {
-	return nil
 }
 
 // ListDocs - Query db and return list of doctors in country
--- /dev/null
+++ b/plugins/errors.go
@@ -1,0 +1,28 @@
+package plugins
+
+import (
+	"github.com/olmaxmedical/olmax_go/router"
+)
+
+// FormErrors - A list of errors present on a POST request
+const FormErrors router.PluginMask = 6
+
+func init() {
+	b := &router.Plugin{
+		Name:     "Form Errors",
+		Run:      GetFormErrors,
+		Validate: nil,
+	}
+	router.AddPlugin(b, FormErrors)
+}
+
+// GetFormErrors - Return all errors encountered during form parse
+func GetFormErrors(r *router.Request) map[string]interface{} {
+	s := r.Session()
+	if s == nil {
+		return nil
+	}
+	return map[string]interface{}{
+		"errors": s.Get("errors"),
+	}
+}
--- a/plugins/specialties.go
+++ b/plugins/specialties.go
@@ -5,20 +5,15 @@
 )
 
 // ListServices - Bitmask to list services in native language
-const ListServices router.IncludeExtra = 2
+const ListServices router.PluginMask = 2
 
 func init() {
 	b := &router.Plugin{
 		Name:     "specialties",
 		Run:      Specialties,
-		Validate: ValidateSpecialties,
+		Validate: nil,
 	}
 	router.AddPlugin(b, ListServices)
-}
-
-// ValidateSpecialties - No-op
-func ValidateSpecialties() error {
-	return nil
 }
 
 // Specialties - return a list of native language representations of our medical fields
--- /dev/null
+++ b/plugins/tokens.go
@@ -1,0 +1,64 @@
+package plugins
+
+import (
+	"github.com/google/uuid"
+	"github.com/olmaxmedical/olmax_go/router"
+)
+
+var tokens []string
+
+// SessionToken - An in-memory token to allow a client to track
+const SessionToken router.PluginMask = 4
+
+// FormToken - A database-persisted one time use token to relate forms to POST requests
+const FormToken router.PluginMask = 5
+
+func init() {
+	b := &router.Plugin{
+		Name:     "sessionToken",
+		Run:      NewSessionToken,
+		Validate: nil,
+	}
+	router.AddPlugin(b, SessionToken)
+	c := &router.Plugin{
+		Name:     "formToken",
+		Run:      NewFormToken,
+		Validate: nil,
+	}
+	router.AddPlugin(c, FormToken)
+}
+
+// NewSessionToken returns a unique session token
+func NewSessionToken(r *router.Request) map[string]interface{} {
+	return map[string]interface{}{
+		"token": newToken(),
+	}
+}
+
+// NewFormToken returns a unique token associated with a client's form entry session
+// This will fall back to a database call
+func NewFormToken(r *router.Request) map[string]interface{} {
+	return map[string]interface{}{
+		"token": newToken(),
+	}
+}
+
+// TODO(halfwit) - Form plugins will use this
+func validateToken(token string) bool {
+	for n, t := range tokens {
+		if token == t {
+			// n will always be at least 0, tokens at least 1
+			tokens[n] = tokens[len(tokens)-1]
+			tokens = tokens[:len(tokens)-1]
+			return true
+		}
+	}
+	return false
+}
+
+func newToken() string {
+	u, _ := uuid.NewRandom()
+	t := u.String()
+	tokens = append(tokens, t)
+	return t
+}
--- a/resources/css/default.css
+++ b/resources/css/default.css
@@ -76,7 +76,7 @@
 footer nav details[open] li a:hover {
   color: #3b4a96;
 }
-@media only screen and (min-width: 800px) {
+@media only screen and (min-width: 480px) {
   main {
     font-size: 1rem;
   }
--- a/router/forms.go
+++ b/router/forms.go
@@ -61,12 +61,13 @@
 		errors = append(errors, "No such page")
 		return nil, errors
 	}
-	if form.After&ValidateToken != 0 {
-		t := r.PostFormValue("token")
-		if !validateToken(t) {
-			return nil, []string{p.printer.Sprint("Invalid form token")}
-		}
-	}
+	/*
+		if form.After&ValidateToken != 0 {
+			t := r.PostFormValue("token")
+			if !validateToken(t) {
+				return nil, []string{p.printer.Sprint("Invalid form token")}
+			}
+		}*/
 	if form.After&WithOffer != 0 {
 		//token := r.PostFormValue("sessiontoken")
 		//offer := db.GetOffer(token)
--- a/router/pages.go
+++ b/router/pages.go
@@ -25,24 +25,18 @@
 type Access uint8
 
 const (
+	// GuestAuth - non registered user
 	GuestAuth Access = 1 << iota
+	// PatientAuth - normal user, added by registration process
 	PatientAuth
+	// DoctorAuth - manually added to the database
 	DoctorAuth
 )
 
-// IncludeExtra - helper bitmasks to populate common elements across the site
-type IncludeExtra uint32
-
-const (
-	FormToken IncludeExtra = 1 << iota
-	FormErrors
-	SessionToken
-)
-
 // Page defines what a client receives from a GET request
 type Page struct {
 	Access Access
-	Extra  IncludeExtra
+	Extra  PluginMask
 	CSS    string
 	Path   string
 	Data   func(p *message.Printer) map[string]interface{}
@@ -98,30 +92,11 @@
 	r["header"] = header(p.printer, p.status)
 	r["footer"] = footer(p.printer)
 	r["basedir"] = getBaseDir(cache.Path)
-	// We may want some helper functions to p instead to access the printer, the session, etc
 	for _, key := range pluginKey {
 		if (cache.Extra & key) != 0 {
 			r[pluginCache[key].Name] = pluginCache[key].Run(p)
 		}
 	}
-
-	if p.session != nil && cache.Extra&FormErrors != 0 {
-		r["errors"] = p.session.Get("errors")
-	}
-
-	//if cache.Extra&ClientName != 0 {
-	//	i["firstname"] = db.ClientName(p.session)
-	//}
-	//if cache.Extra&ClientSurname != 0 {
-	//	i["surname"] = db.ClientSurname(p.session)
-	//}
-	//if cache.Extra&ClientUsername != 0 {
-	//	i["username"] = db.ClientUsername(p.session)
-	//}
-	if cache.Extra&FormErrors != 0 && p.session != nil {
-		r["errors"] = p.session.Get("errors")
-	}
-
 	if p.session != nil {
 		r["username"] = p.session.Get("username")
 	}
--- a/router/plugins.go
+++ b/router/plugins.go
@@ -5,14 +5,13 @@
 )
 
 // PluginMask - (Must be unique) ID for a plugin
-// Once everything is moved to plugins, remove IncludeExtra in favour of PluginMask
 type PluginMask uint32
 
 // DEAD is a magic string to indicate a non-unique plugin key
-const DEAD IncludeExtra = 0x0666000
+const DEAD PluginMask = 0x0666000
 
-var pluginCache map[IncludeExtra]*Plugin
-var pluginKey []IncludeExtra
+var pluginCache map[PluginMask]*Plugin
+var pluginKey []PluginMask
 
 // Plugin - Provide extra data or functionality from GET/POST pages
 type Plugin struct {
@@ -22,15 +21,17 @@
 }
 
 func init() {
-	pluginCache = make(map[IncludeExtra]*Plugin)
+	pluginCache = make(map[PluginMask]*Plugin)
 }
 
 // ValidatePlugins - Run through each plugin
 // make sure that its mapping isn't redundant with any other
-// Make sure the code runs accurately without error
 func ValidatePlugins() []error {
 	errs := []error{}
 	for key, item := range pluginCache {
+		if item.Validate == nil {
+			continue
+		}
 		err := item.Validate()
 		if err != nil {
 			errs = append(errs, err)
@@ -43,7 +44,7 @@
 }
 
 // AddPlugin - Add Plugin to map by key
-func AddPlugin(p *Plugin, key IncludeExtra) {
+func AddPlugin(p *Plugin, key PluginMask) {
 
 	if pluginCache[key] != nil {
 		key |= DEAD
--- a/router/tokens.go
+++ /dev/null
@@ -1,26 +1,0 @@
-package router
-
-import (
-	"github.com/google/uuid"
-)
-
-var tokens []string
-
-func newToken() string {
-	u, _ := uuid.NewRandom()
-	t := u.String()
-	tokens = append(tokens, t)
-	return t
-}
-
-func validateToken(token string) bool {
-	for n, t := range tokens {
-		if token == t {
-			// n will always be at least 0, tokens at least 1
-			tokens[n] = tokens[len(tokens)-1]
-			tokens = tokens[:len(tokens)-1]
-			return true
-		}
-	}
-	return false
-}