hlfw.ca

webbing

Download patch

ref: 1a73181a17b05104cc0e2e52e8b02fbc8a0eea68
parent: 23ac9d04854f28a1abdd945650c23b2580e0aa4b
author: Michael Misch <michaelmisch1985@gmail.com>
date: Tue Nov 12 01:27:28 PST 2019

Swap to using methods to access the printer as it mostly is unused, we may need session as well, allow access

--- a/plugins/countries.go
+++ b/plugins/countries.go
@@ -65,7 +65,7 @@
 }
 
 // Countries - return a localized list of countries
-func Countries(_ *message.Printer) map[string]interface{} {
+func Countries(_ *router.Request) map[string]interface{} {
 	c := make(map[string]interface{})
 	for _, item := range cache.list {
 		c[item.Name.Common] = item.Name.Common
@@ -78,6 +78,7 @@
 	return nil
 }
 
+// TODO: Export this so it's available to form parsing as a bitmask
 func validateCountries(p *message.Printer, countries []string) string {
 	for _, c := range countries {
 		if msg := validateCountry(p, c); msg != "" {
--- a/plugins/doctor.go
+++ b/plugins/doctor.go
@@ -2,7 +2,6 @@
 
 import (
 	"github.com/olmaxmedical/olmax_go/router"
-	"golang.org/x/text/message"
 )
 
 // ListDoctors - Bitmask to list doctors of in client country
@@ -34,7 +33,8 @@
 }
 
 // ListDocs - Query db and return list of doctors in country
-func ListDocs(p *message.Printer) map[string]interface{} {
+// These may need eventual localization for any bilingual doctors we have
+func ListDocs(r *router.Request) map[string]interface{} {
 	return map[string]interface{}{
 		"Mark Abuzamzam, MD": &doctor{
 			Image:     "AbuzamzamMD.jpg",
--- a/plugins/specialties.go
+++ b/plugins/specialties.go
@@ -2,7 +2,6 @@
 
 import (
 	"github.com/olmaxmedical/olmax_go/router"
-	"golang.org/x/text/message"
 )
 
 // ListServices - Bitmask to list services in native language
@@ -23,7 +22,8 @@
 }
 
 // Specialties - return a list of native language representations of our medical fields
-func Specialties(p *message.Printer) map[string]interface{} {
+func Specialties(r *router.Request) map[string]interface{} {
+	p := r.Printer()
 	return map[string]interface{}{
 		"acutepain":      p.Sprintf("Acute Pain Medicine"),
 		"anesthesiology": p.Sprintf("Anesthesiology"),
--- a/router/forms.go
+++ b/router/forms.go
@@ -53,7 +53,7 @@
 
 // This large ladder just adds conditional logic to forms for a more generic
 // Ideally, the *page abstraction will never leak into the form validation
-func parseform(p *request, w http.ResponseWriter, r *http.Request) (*Form, []string) {
+func parseform(p *Request, w http.ResponseWriter, r *http.Request) (*Form, []string) {
 	var errors, errs []string
 	var msg string
 	form, ok := formlist[p.path]
--- a/router/pages.go
+++ b/router/pages.go
@@ -72,7 +72,7 @@
 			errs = append(errs, fmt.Errorf("parsing in %s - %v", path.Dir(item.Path), err))
 			continue
 		}
-		p := &request{
+		p := &Request{
 			printer: printer,
 			path:    item.Path + ".html",
 			role:    db.PatientAuth | db.DoctorAuth | db.GuestAuth,
@@ -85,7 +85,7 @@
 	return errs
 }
 
-func getdata(p *request, in string) ([]byte, error) {
+func getdata(p *Request, in string) ([]byte, error) {
 	cache, ok := pagecache[p.path]
 	if !ok {
 		return nil, fmt.Errorf("No such page: %s", p.path)
@@ -98,10 +98,10 @@
 	r["header"] = header(p.printer, p.status)
 	r["footer"] = footer(p.printer)
 	r["basedir"] = getBaseDir(cache.Path)
-	// comparing an int against cache.Extra is not useful, we need an array of keys instead set at AddPlugin.
+	// 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.printer)
+			r[pluginCache[key].Name] = pluginCache[key].Run(p)
 		}
 	}
 
--- a/router/plugins.go
+++ b/router/plugins.go
@@ -2,8 +2,6 @@
 
 import (
 	"fmt"
-
-	"golang.org/x/text/message"
 )
 
 // PluginMask - (Must be unique) ID for a plugin
@@ -19,7 +17,7 @@
 // Plugin - Provide extra data or functionality from GET/POST pages
 type Plugin struct {
 	Name     string
-	Run      func(p *message.Printer) map[string]interface{}
+	Run      func(p *Request) map[string]interface{}
 	Validate func() error
 }
 
--- a/router/run.go
+++ b/router/run.go
@@ -60,7 +60,8 @@
 	d.normal(w, r)
 }
 
-type request struct {
+// Request represents an incoming GET/POST
+type Request struct {
 	printer *message.Printer
 	session session.Session
 	user    string
@@ -69,6 +70,16 @@
 	role    db.Access
 }
 
+// Printer - returns the client's localized printer handler
+func (r *Request) Printer() *message.Printer {
+	return r.printer
+}
+
+// Session - returns the client's session
+func (r *Request) Session() session.Session {
+	return r.session
+}
+
 func (d *handle) normal(w http.ResponseWriter, r *http.Request) {
 	if r.URL.Path == "/" {
 		http.Redirect(w, r, "/index.html", 302)
@@ -75,7 +86,7 @@
 		return
 	}
 	user, status, us, role := getUser(d, w, r)
-	p := &request{
+	p := &Request{
 		printer: userLang(r),
 		status:  status,
 		user:    user,
@@ -97,7 +108,7 @@
 
 }
 
-func post(p *request, us session.Session, w http.ResponseWriter, r *http.Request) {
+func post(p *Request, us session.Session, w http.ResponseWriter, r *http.Request) {
 	form, errors := parseform(p, w, r)
 	if len(errors) > 0 && errors[0] != "nil" {
 		// NOTE(halfwit) this stashes previous entries, but does not work
@@ -112,7 +123,7 @@
 	}
 }
 
-func get(p *request, w http.ResponseWriter) {
+func get(p *Request, w http.ResponseWriter) {
 	var data []byte
 	var err error
 	switch db.UserRole(p.user) {
@@ -158,7 +169,7 @@
 		http.Error(w, "Unauthorized", 401)
 		return
 	}
-	p := &request{
+	p := &Request{
 		printer: userLang(r),
 		status:  status,
 		session: us,