From 3defc062bdf7c873d7514f7754a2a98eb1061555 Mon Sep 17 00:00:00 2001 From: Harvey Kandola Date: Fri, 21 Jul 2017 13:39:53 +0100 Subject: [PATCH] refactored smart section code --- README.md | 2 +- core/api/convert/convert.go | 3 +- core/api/endpoint/authentication_endpoint.go | 2 +- core/api/endpoint/jwt.go | 28 ------- core/api/endpoint/page_endpoint.go | 2 +- core/api/endpoint/sections_endpoint.go | 2 +- core/api/endpoint/server.go | 24 ------ core/api/plugins/glick.go | 7 -- core/api/request/init.go | 79 ------------------- core/section/register.go | 43 ---------- {core => domain}/section/airtable/airtable.go | 4 +- {core => domain}/section/code/code.go | 4 +- {core => domain}/section/gemini/gemini.go | 28 +++---- {core => domain}/section/gemini/model.go | 5 +- {core => domain}/section/github/auth.go | 0 {core => domain}/section/github/commits.go | 0 .../section/github/commits_template.go | 0 {core => domain}/section/github/github.go | 42 +++++----- {core => domain}/section/github/issues.go | 0 .../section/github/issues_template.go | 0 {core => domain}/section/github/lists.go | 15 ++-- {core => domain}/section/github/milestones.go | 0 .../section/github/milestones_template.go | 0 {core => domain}/section/github/model.go | 0 {core => domain}/section/github/sort.go | 0 {core => domain}/section/github/summary.go | 0 .../section/github/summary_template.go | 0 {core => domain}/section/markdown/markdown.go | 4 +- {core => domain}/section/papertrail/model.go | 0 .../section/papertrail/papertrail.go | 37 ++++----- {core => domain}/section/provider/provider.go | 0 domain/section/register.go | 45 +++++++++++ {core => domain}/section/section_test.go | 0 {core => domain}/section/table/table.go | 4 +- {core => domain}/section/trello/model.go | 0 {core => domain}/section/trello/trello.go | 38 ++++----- {core => domain}/section/wysiwyg/wysiwyg.go | 4 +- edition/community.go | 46 +++++------ gui/package.json | 2 +- meta.json | 8 +- 40 files changed, 172 insertions(+), 306 deletions(-) delete mode 100644 core/section/register.go rename {core => domain}/section/airtable/airtable.go (93%) rename {core => domain}/section/code/code.go (91%) rename {core => domain}/section/gemini/gemini.go (92%) rename {core => domain}/section/gemini/model.go (96%) rename {core => domain}/section/github/auth.go (100%) rename {core => domain}/section/github/commits.go (100%) rename {core => domain}/section/github/commits_template.go (100%) rename {core => domain}/section/github/github.go (83%) rename {core => domain}/section/github/issues.go (100%) rename {core => domain}/section/github/issues_template.go (100%) rename {core => domain}/section/github/lists.go (83%) rename {core => domain}/section/github/milestones.go (100%) rename {core => domain}/section/github/milestones_template.go (100%) rename {core => domain}/section/github/model.go (100%) rename {core => domain}/section/github/sort.go (100%) rename {core => domain}/section/github/summary.go (100%) rename {core => domain}/section/github/summary_template.go (100%) rename {core => domain}/section/markdown/markdown.go (91%) rename {core => domain}/section/papertrail/model.go (100%) rename {core => domain}/section/papertrail/papertrail.go (83%) rename {core => domain}/section/provider/provider.go (100%) create mode 100644 domain/section/register.go rename {core => domain}/section/section_test.go (100%) rename {core => domain}/section/table/table.go (91%) rename {core => domain}/section/trello/model.go (100%) rename {core => domain}/section/trello/trello.go (85%) rename {core => domain}/section/wysiwyg/wysiwyg.go (91%) diff --git a/README.md b/README.md index 2b69c1e5..40b843bb 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The mission is to bring software dev inspired features (refactoring, testing, li ## Latest version -v1.50.1 +v1.50.2 ## OS Support diff --git a/core/api/convert/convert.go b/core/api/convert/convert.go index 0f512f7a..d38ec097 100644 --- a/core/api/convert/convert.go +++ b/core/api/convert/convert.go @@ -14,6 +14,7 @@ package convert import ( "errors" + "github.com/documize/community/core/api/convert/html" "github.com/documize/community/core/api/plugins" api "github.com/documize/community/core/convapi" @@ -53,7 +54,7 @@ func Convert(ctx context.Context, xtn string, fileRequest *api.DocumentConversio /* TODO add sanitisation for body & title HTML var titlePolicy, bodyPolicy *bluemonday.Policy -func init() { +func xxx() { policy := bluemonday.UGCPolicy() policy.RequireNoFollowOnLinks(true) diff --git a/core/api/endpoint/authentication_endpoint.go b/core/api/endpoint/authentication_endpoint.go index d03d2d79..2943bb75 100644 --- a/core/api/endpoint/authentication_endpoint.go +++ b/core/api/endpoint/authentication_endpoint.go @@ -26,8 +26,8 @@ import ( "github.com/documize/community/core/api/util" "github.com/documize/community/core/log" "github.com/documize/community/core/secrets" - "github.com/documize/community/core/section/provider" "github.com/documize/community/core/web" + "github.com/documize/community/domain/section/provider" ) // Authenticate user based up HTTP Authorization header. diff --git a/core/api/endpoint/jwt.go b/core/api/endpoint/jwt.go index b309fa77..616d64cb 100644 --- a/core/api/endpoint/jwt.go +++ b/core/api/endpoint/jwt.go @@ -23,34 +23,6 @@ import ( "github.com/documize/community/core/log" ) -// var jwtKey string - -// func init() { -// env.GetString(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated", -// func(t *string, n string) bool { -// if jwtKey == "" { -// b := make([]byte, 17) -// _, err := rand.Read(b) -// if err != nil { -// jwtKey = err.Error() -// log.Error("problem using crypto/rand", err) -// return false -// } -// for k, v := range b { -// if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '0') { -// b[k] = v -// } else { -// s := fmt.Sprintf("%x", v) -// b[k] = s[0] -// } -// } -// jwtKey = string(b) -// log.Info("Please set DOCUMIZESALT or use -salt with this value: " + jwtKey) -// } -// return true -// }) -// } - // Generates JSON Web Token (http://jwt.io) func generateJWT(user, org, domain string) string { token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ diff --git a/core/api/endpoint/page_endpoint.go b/core/api/endpoint/page_endpoint.go index e2ce2110..e993cf71 100644 --- a/core/api/endpoint/page_endpoint.go +++ b/core/api/endpoint/page_endpoint.go @@ -24,9 +24,9 @@ import ( "github.com/documize/community/core/api/request" "github.com/documize/community/core/api/util" "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" "github.com/documize/community/core/streamutil" "github.com/documize/community/core/uniqueid" + "github.com/documize/community/domain/section/provider" htmldiff "github.com/documize/html-diff" "github.com/gorilla/mux" ) diff --git a/core/api/endpoint/sections_endpoint.go b/core/api/endpoint/sections_endpoint.go index f2647632..ffb7dd94 100644 --- a/core/api/endpoint/sections_endpoint.go +++ b/core/api/endpoint/sections_endpoint.go @@ -21,9 +21,9 @@ import ( "github.com/documize/community/core/api/request" "github.com/documize/community/core/api/util" "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" "github.com/documize/community/core/streamutil" "github.com/documize/community/core/uniqueid" + "github.com/documize/community/domain/section/provider" "github.com/gorilla/mux" ) diff --git a/core/api/endpoint/server.go b/core/api/endpoint/server.go index c0a3396d..75e9245c 100644 --- a/core/api/endpoint/server.go +++ b/core/api/endpoint/server.go @@ -26,30 +26,6 @@ import ( "github.com/gorilla/mux" ) -// var port, certFile, keyFile, forcePort2SSL string - -// Product details app edition and version -// var Product env.ProdInfo - -// func init() { -// // Product.Major = "1" -// // Product.Minor = "50" -// // Product.Patch = "0" -// // Product.Version = fmt.Sprintf("%s.%s.%s", Product.Major, Product.Minor, Product.Patch) -// // Product.Edition = "Community" -// // Product.Title = fmt.Sprintf("%s Edition", Product.Edition) -// // Product.License = env.License{} -// // Product.License.Seats = 1 -// // Product.License.Valid = true -// // Product.License.Trial = false -// // Product.License.Edition = "Community" - -// // env.GetString(&certFile, "cert", false, "the cert.pem file used for https", nil) -// // env.GetString(&keyFile, "key", false, "the key.pem file used for https", nil) -// // env.GetString(&port, "port", false, "http/https port number", nil) -// // env.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil) -// } - var testHost string // used during automated testing // Serve the Documize endpoint. diff --git a/core/api/plugins/glick.go b/core/api/plugins/glick.go index 6ce9340f..fcf91e46 100644 --- a/core/api/plugins/glick.go +++ b/core/api/plugins/glick.go @@ -31,13 +31,6 @@ import ( var PluginFile = "DB" // this points to the database var insecure = "false" -// func init() { -// env.GetString(&PluginFile, "plugin", false, -// "the JSON file describing plugins, default 'DB' uses the database config table 'FILEPLUGINS' entry", nil) -// env.GetString(&insecure, "insecure", false, -// "if 'true' allow https endpoints with invalid certificates (only for testing)", nil) -// } - type infoLog struct{} func (i infoLog) Write(b []byte) (int, error) { diff --git a/core/api/request/init.go b/core/api/request/init.go index 15f25f18..1ae64ef3 100644 --- a/core/api/request/init.go +++ b/core/api/request/init.go @@ -45,85 +45,6 @@ func (dr *databaseRequest) MakeTx() (err error) { return err } -// func init() { -// var err error - -// env.GetString(&connectionString, "db", true, -// `'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`, -// func(*string, string) bool { - -// Db, err = sqlx.Open("mysql", stdConn(connectionString)) - -// if err != nil { -// log.Error("Unable to setup database", err) -// } - -// Db.SetMaxIdleConns(30) -// Db.SetMaxOpenConns(100) -// Db.SetConnMaxLifetime(time.Second * 14400) - -// err = Db.Ping() - -// if err != nil { -// log.Error("Unable to connect to database, connection string should be of the form: '"+ -// "username:password@tcp(host:3306)/database'", err) -// os.Exit(2) -// } - -// // go into setup mode if required -// if web.SiteMode != web.SiteModeOffline { -// if database.Check(Db, connectionString) { -// if err := database.Migrate(true /* the config table exists */); err != nil { -// log.Error("Unable to run database migration: ", err) -// os.Exit(2) -// } -// } else { -// log.Info("database.Check(Db) !OK, going into setup mode") -// } -// } - -// return false // value not changed -// }) -// } - -// var stdParams = map[string]string{ -// "charset": "utf8", -// "parseTime": "True", -// "maxAllowedPacket": "4194304", // 4194304 // 16777216 = 16MB -// } - -// func stdConn(cs string) string { -// queryBits := strings.Split(cs, "?") -// ret := queryBits[0] + "?" -// retFirst := true -// if len(queryBits) == 2 { -// paramBits := strings.Split(queryBits[1], "&") -// for _, pb := range paramBits { -// found := false -// if assignBits := strings.Split(pb, "="); len(assignBits) == 2 { -// _, found = stdParams[strings.TrimSpace(assignBits[0])] -// } -// if !found { // if we can't work out what it is, put it through -// if retFirst { -// retFirst = false -// } else { -// ret += "&" -// } -// ret += pb -// } -// } -// } -// for k, v := range stdParams { -// if retFirst { -// retFirst = false -// } else { -// ret += "&" -// } -// ret += k + "=" + v -// } -// return ret -// } - type baseManager struct { } diff --git a/core/section/register.go b/core/section/register.go deleted file mode 100644 index 47e2bbad..00000000 --- a/core/section/register.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2016 Documize Inc. . All rights reserved. -// -// This software (Documize Community Edition) is licensed under -// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html -// -// You can operate outside the AGPL restrictions by purchasing -// Documize Enterprise Edition and obtaining a commercial license -// by contacting . -// -// https://documize.com - -package section - -import ( - "fmt" - - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/airtable" - "github.com/documize/community/core/section/code" - "github.com/documize/community/core/section/gemini" - "github.com/documize/community/core/section/github" - "github.com/documize/community/core/section/markdown" - "github.com/documize/community/core/section/papertrail" - "github.com/documize/community/core/section/provider" - "github.com/documize/community/core/section/table" - "github.com/documize/community/core/section/trello" - "github.com/documize/community/core/section/wysiwyg" -) - -// Register sections -func Register() { - provider.Register("code", &code.Provider{}) - provider.Register("gemini", &gemini.Provider{}) - provider.Register("github", &github.Provider{}) - provider.Register("markdown", &markdown.Provider{}) - provider.Register("papertrail", &papertrail.Provider{}) - provider.Register("table", &table.Provider{}) - provider.Register("trello", &trello.Provider{}) - provider.Register("wysiwyg", &wysiwyg.Provider{}) - provider.Register("airtable", &airtable.Provider{}) - p := provider.List() - log.Info(fmt.Sprintf("Documize registered %d sections and tabs", len(p))) -} diff --git a/core/section/airtable/airtable.go b/domain/section/airtable/airtable.go similarity index 93% rename from core/section/airtable/airtable.go rename to domain/section/airtable/airtable.go index 767bd4f1..77f0f32e 100644 --- a/core/section/airtable/airtable.go +++ b/domain/section/airtable/airtable.go @@ -14,11 +14,13 @@ package airtable import ( "net/http" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) // Provider represents Airtable type Provider struct { + Runtime env.Runtime } // Meta describes us diff --git a/core/section/code/code.go b/domain/section/code/code.go similarity index 91% rename from core/section/code/code.go rename to domain/section/code/code.go index e5a32632..048f7844 100644 --- a/core/section/code/code.go +++ b/domain/section/code/code.go @@ -14,11 +14,13 @@ package code import ( "net/http" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) // Provider represents code snippet type Provider struct { + Runtime env.Runtime } // Meta describes us. diff --git a/core/section/gemini/gemini.go b/domain/section/gemini/gemini.go similarity index 92% rename from core/section/gemini/gemini.go rename to domain/section/gemini/gemini.go index 415d9b65..1f2922a7 100644 --- a/core/section/gemini/gemini.go +++ b/domain/section/gemini/gemini.go @@ -20,12 +20,13 @@ import ( "io/ioutil" "net/http" - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) // Provider represents Gemini type Provider struct { + Runtime env.Runtime } // Meta describes us. @@ -87,29 +88,29 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R } // Refresh just sends back data as-is. -func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData string) { +func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) { var c = geminiConfig{} err := json.Unmarshal([]byte(config), &c) if err != nil { - log.Error("Unable to read Gemini config", err) + p.Runtime.Log.Error("Unable to read Gemini config", err) return } c.Clean(ctx) if len(c.URL) == 0 { - log.Info("Gemini.Refresh received empty URL") + p.Runtime.Log.Info("Gemini.Refresh received empty URL") return } if len(c.Username) == 0 { - log.Info("Gemini.Refresh received empty username") + p.Runtime.Log.Info("Gemini.Refresh received empty username") return } if len(c.APIKey) == 0 { - log.Info("Gemini.Refresh received empty API key") + p.Runtime.Log.Info("Gemini.Refresh received empty API key") return } @@ -136,16 +137,15 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st dec := json.NewDecoder(res.Body) err = dec.Decode(&items) - if err != nil { - fmt.Println(err) + p.Runtime.Log.Error("unable to Decode gemini items", err) return } j, err := json.Marshal(items) if err != nil { - log.Error("unable to marshall gemini items", err) + p.Runtime.Log.Error("unable to marshal gemini items", err) return } @@ -196,7 +196,6 @@ func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { res, err := client.Do(req) if err != nil { - fmt.Println(err) provider.WriteError(w, "gemini", err) return } @@ -215,7 +214,6 @@ func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { err = dec.Decode(&g) if err != nil { - fmt.Println(err) provider.WriteError(w, "gemini", err) return } @@ -288,7 +286,6 @@ func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { err = dec.Decode(&workspace) if err != nil { - fmt.Println(err) provider.WriteError(w, "gemini", err) return } @@ -334,7 +331,6 @@ func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { filter, err := json.Marshal(config.Filter) if err != nil { - fmt.Println(err) provider.WriteError(w, "gemini", err) return } @@ -348,7 +344,6 @@ func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { res, err := client.Do(req) if err != nil { - fmt.Println(err) provider.WriteError(w, "gemini", err) return } @@ -374,7 +369,6 @@ func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { } func secs(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { - sec, err := getSecrets(ctx) - log.IfErr(err) + sec, _ := getSecrets(ctx) provider.WriteJSON(w, sec) } diff --git a/core/section/gemini/model.go b/domain/section/gemini/model.go similarity index 96% rename from core/section/gemini/model.go rename to domain/section/gemini/model.go index 6c10158e..f7224c0d 100644 --- a/core/section/gemini/model.go +++ b/domain/section/gemini/model.go @@ -14,8 +14,7 @@ package gemini import ( "strings" - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/domain/section/provider" ) // the HTML that is rendered by this section. @@ -108,7 +107,7 @@ func (c *geminiConfig) SaveSecrets(ctx *provider.Context) { sec.APIKey = strings.TrimSpace(c.APIKey) sec.Username = strings.TrimSpace(c.Username) sec.URL = strings.TrimSpace(c.URL) - log.IfErr(ctx.MarshalSecrets(sec)) + ctx.MarshalSecrets(sec) } type secrets struct { diff --git a/core/section/github/auth.go b/domain/section/github/auth.go similarity index 100% rename from core/section/github/auth.go rename to domain/section/github/auth.go diff --git a/core/section/github/commits.go b/domain/section/github/commits.go similarity index 100% rename from core/section/github/commits.go rename to domain/section/github/commits.go diff --git a/core/section/github/commits_template.go b/domain/section/github/commits_template.go similarity index 100% rename from core/section/github/commits_template.go rename to domain/section/github/commits_template.go diff --git a/core/section/github/github.go b/domain/section/github/github.go similarity index 83% rename from core/section/github/github.go rename to domain/section/github/github.go index 8516f584..e9c80860 100644 --- a/core/section/github/github.go +++ b/domain/section/github/github.go @@ -15,15 +15,13 @@ import ( "bytes" "encoding/json" "errors" - //"fmt" "html/template" "io/ioutil" "net/http" "strings" - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" - + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" gogithub "github.com/google/go-github/github" ) @@ -45,6 +43,7 @@ func init() { // Provider represents GitHub type Provider struct { + Runtime env.Runtime } // Meta describes us. @@ -59,7 +58,6 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http if len(method) == 0 { msg := "missing method name" - log.ErrorString("github: " + msg) provider.WriteMessage(w, "gitub", msg) return } @@ -80,9 +78,8 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http body, err := ioutil.ReadAll(r.Body) if err != nil { - msg := "Bad body" - log.ErrorString("github: " + msg) - provider.WriteMessage(w, "github", msg) + p.Runtime.Log.Error("bad body", errors.New("Missing body")) + provider.WriteMessage(w, "github", "bad body") return } @@ -90,7 +87,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http // write the new one, direct from JS if err = ctx.SaveSecrets(string(body)); err != nil { - log.Error("github settoken configuration", err) + p.Runtime.Log.Error("github settoken configuration", err) provider.WriteError(w, "github", err) return } @@ -104,7 +101,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http err = json.Unmarshal(body, &config) if err != nil { - log.Error("github Command Unmarshal", err) + p.Runtime.Log.Error("github Command Unmarshal", err) provider.WriteError(w, "github", err) return } @@ -127,7 +124,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http if err != nil { // token now invalid, so wipe it ctx.SaveSecrets("") // ignore error, already in an error state - log.Error("github check token validation", err) + p.Runtime.Log.Error("github check token validation", err) provider.WriteError(w, "github", err) return } @@ -135,11 +132,11 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http default: - if listFailed(method, config, client, w) { + if listFailed(p.Runtime, method, config, client, w) { gr := githubRender{} for _, rep := range reports { - log.IfErr(rep.refresh(&gr, &config, client)) + rep.refresh(&gr, &config, client) } provider.WriteJSON(w, &gr) @@ -153,9 +150,8 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin var c = githubConfig{} err := json.Unmarshal([]byte(configJSON), &c) - if err != nil { - log.Error("unable to unmarshall github config", err) + p.Runtime.Log.Error("unable to unmarshall github config", err) return "internal configuration error '" + err.Error() + "'" } @@ -166,7 +162,7 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin byts, err := json.Marshal(refreshReportData(&c, client)) if err != nil { - log.Error("unable to marshall github data", err) + p.Runtime.Log.Error("unable to marshall github data", err) return "internal configuration error '" + err.Error() + "'" } @@ -177,7 +173,7 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin func refreshReportData(c *githubConfig, client *gogithub.Client) *githubRender { var gr = githubRender{} for _, rep := range reports { - log.IfErr(rep.refresh(&gr, c, client)) + rep.refresh(&gr, c, client) } return &gr } @@ -192,7 +188,7 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string { err = json.Unmarshal([]byte(config), &c) if err != nil { - log.Error("unable to unmarshall github config", err) + p.Runtime.Log.Error("unable to unmarshall github config", err) return "Please delete and recreate this Github section." } @@ -208,7 +204,7 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string { err = json.Unmarshal([]byte(data), &payload) if err != nil { - log.Error("unable to unmarshall github data", err) + p.Runtime.Log.Error("unable to unmarshall github data", err) return "Please delete and recreate this Github section." } @@ -222,12 +218,12 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string { rep, ok := reports[repID] if !ok { msg := "github report not found for: " + repID - log.ErrorString(msg) + p.Runtime.Log.Info(msg) return "Documize internal error: " + msg } if err = rep.render(&payload, &c); err != nil { - log.Error("unable to render "+repID, err) + p.Runtime.Log.Error("unable to render "+repID, err) return "Documize internal github render " + repID + " error: " + err.Error() + "
" + data } @@ -236,7 +232,7 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string { t, err = t.Parse(rep.template) if err != nil { - log.Error("github render template.Parse error:", err) + p.Runtime.Log.Error("github render template.Parse error:", err) //for k, v := range strings.Split(rep.template, "\n") { // fmt.Println("DEBUG", k+1, v) //} @@ -246,7 +242,7 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string { buffer := new(bytes.Buffer) err = t.Execute(buffer, payload) if err != nil { - log.Error("github render template.Execute error:", err) + p.Runtime.Log.Error("github render template.Execute error:", err) return "Documize internal github template.Execute error: " + err.Error() } diff --git a/core/section/github/issues.go b/domain/section/github/issues.go similarity index 100% rename from core/section/github/issues.go rename to domain/section/github/issues.go diff --git a/core/section/github/issues_template.go b/domain/section/github/issues_template.go similarity index 100% rename from core/section/github/issues_template.go rename to domain/section/github/issues_template.go diff --git a/core/section/github/lists.go b/domain/section/github/lists.go similarity index 83% rename from core/section/github/lists.go rename to domain/section/github/lists.go index a69f7a96..8444064c 100644 --- a/core/section/github/lists.go +++ b/domain/section/github/lists.go @@ -15,27 +15,26 @@ import ( "fmt" "net/http" - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" - + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" gogithub "github.com/google/go-github/github" ) -func listFailed(method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) { +func listFailed(rt env.Runtime, method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) { switch method { // which list to choose? case "owners": me, _, err := client.Users.Get("") if err != nil { - log.Error("github get user details:", err) + rt.Log.Error("github get user details:", err) provider.WriteError(w, "github", err) return } orgs, _, err := client.Organizations.List("", nil) if err != nil { - log.Error("github get user's organisations:", err) + rt.Log.Error("github get user's organisations:", err) provider.WriteError(w, "github", err) return } @@ -59,7 +58,7 @@ func listFailed(method string, config githubConfig, client *gogithub.Client, w h me, _, err := client.Users.Get("") if err != nil { - log.Error("github get user details:", err) + rt.Log.Error("github get user details:", err) provider.WriteError(w, "github", err) return } @@ -74,7 +73,7 @@ func listFailed(method string, config githubConfig, client *gogithub.Client, w h repos, _, err = client.Repositories.ListByOrg(config.Owner, opt) } if err != nil { - log.Error("github get user/org repositories:", err) + rt.Log.Error("github get user/org repositories:", err) provider.WriteError(w, "github", err) return } diff --git a/core/section/github/milestones.go b/domain/section/github/milestones.go similarity index 100% rename from core/section/github/milestones.go rename to domain/section/github/milestones.go diff --git a/core/section/github/milestones_template.go b/domain/section/github/milestones_template.go similarity index 100% rename from core/section/github/milestones_template.go rename to domain/section/github/milestones_template.go diff --git a/core/section/github/model.go b/domain/section/github/model.go similarity index 100% rename from core/section/github/model.go rename to domain/section/github/model.go diff --git a/core/section/github/sort.go b/domain/section/github/sort.go similarity index 100% rename from core/section/github/sort.go rename to domain/section/github/sort.go diff --git a/core/section/github/summary.go b/domain/section/github/summary.go similarity index 100% rename from core/section/github/summary.go rename to domain/section/github/summary.go diff --git a/core/section/github/summary_template.go b/domain/section/github/summary_template.go similarity index 100% rename from core/section/github/summary_template.go rename to domain/section/github/summary_template.go diff --git a/core/section/markdown/markdown.go b/domain/section/markdown/markdown.go similarity index 91% rename from core/section/markdown/markdown.go rename to domain/section/markdown/markdown.go index c9b4e6d1..6c8b1fe6 100644 --- a/core/section/markdown/markdown.go +++ b/domain/section/markdown/markdown.go @@ -15,11 +15,13 @@ import ( "net/http" "github.com/documize/blackfriday" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) // Provider represents Markdown type Provider struct { + Runtime env.Runtime } // Meta describes us diff --git a/core/section/papertrail/model.go b/domain/section/papertrail/model.go similarity index 100% rename from core/section/papertrail/model.go rename to domain/section/papertrail/model.go diff --git a/core/section/papertrail/papertrail.go b/domain/section/papertrail/papertrail.go similarity index 83% rename from core/section/papertrail/papertrail.go rename to domain/section/papertrail/papertrail.go index af11f6ec..32ba96cf 100644 --- a/core/section/papertrail/papertrail.go +++ b/domain/section/papertrail/papertrail.go @@ -21,14 +21,15 @@ import ( "net/http" "net/url" - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) const me = "papertrail" // Provider represents Papertrail type Provider struct { + Runtime env.Runtime } // Meta describes us. @@ -116,19 +117,19 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http switch method { case "auth": - auth(ctx, config, w, r) + auth(p.Runtime, ctx, config, w, r) case "options": options(config, w, r) } } // Refresh just sends back data as-is. -func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData string) { +func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) { var c = papertrailConfig{} err := json.Unmarshal([]byte(config), &c) if err != nil { - log.Error("unable to read Papertrail config", err) + p.Runtime.Log.Error("unable to read Papertrail config", err) return } @@ -137,21 +138,21 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st c.APIToken = ctx.GetSecrets("APIToken") if len(c.APIToken) == 0 { - log.Error("missing API token", err) + p.Runtime.Log.Error("missing API token", err) return } - result, err := fetchEvents(c) + result, err := fetchEvents(p.Runtime, c) if err != nil { - log.Error("Papertrail fetchEvents failed", err) + p.Runtime.Log.Error("Papertrail fetchEvents failed", err) return } j, err := json.Marshal(result) if err != nil { - log.Error("unable to marshal Papaertrail events", err) + p.Runtime.Log.Error("unable to marshal Papaertrail events", err) return } @@ -159,8 +160,8 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st return } -func auth(ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) { - result, err := fetchEvents(config) +func auth(rt env.Runtime, ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) { + result, err := fetchEvents(rt, config) if result == nil { err = errors.New("nil result of papertrail query") @@ -168,7 +169,7 @@ func auth(ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, if err != nil { - log.IfErr(ctx.SaveSecrets(`{"APIToken":""}`)) // invalid token, so reset it + ctx.SaveSecrets(`{"APIToken":""}`) // invalid token, so reset it if err.Error() == "forbidden" { provider.WriteForbidden(w) @@ -179,7 +180,7 @@ func auth(ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, return } - log.IfErr(ctx.SaveSecrets(`{"APIToken":"` + config.APIToken + `"}`)) + ctx.SaveSecrets(`{"APIToken":"` + config.APIToken + `"}`) provider.WriteJSON(w, result) } @@ -252,7 +253,7 @@ func options(config papertrailConfig, w http.ResponseWriter, r *http.Request) { provider.WriteJSON(w, options) } -func fetchEvents(config papertrailConfig) (result interface{}, err error) { +func fetchEvents(rt env.Runtime, config papertrailConfig) (result interface{}, err error) { var filter string if len(config.Query) > 0 { filter = fmt.Sprintf("q=%s", url.QueryEscape(config.Query)) @@ -268,7 +269,7 @@ func fetchEvents(config papertrailConfig) (result interface{}, err error) { var req *http.Request req, err = http.NewRequest("GET", "https://papertrailapp.com/api/v1/events/search.json?"+filter, nil) if err != nil { - log.Error("new request", err) + rt.Log.Error("new request", err) return } req.Header.Set("X-Papertrail-Token", config.APIToken) @@ -278,12 +279,12 @@ func fetchEvents(config papertrailConfig) (result interface{}, err error) { res, err = client.Do(req) if err != nil { - log.Error("message", err) + rt.Log.Error("message", err) return } if res.StatusCode != http.StatusOK { - log.Error("forbidden", err) + rt.Log.Error("forbidden", err) return } @@ -293,7 +294,7 @@ func fetchEvents(config papertrailConfig) (result interface{}, err error) { err = dec.Decode(&result) if err != nil { - log.Error("unable to read result", err) + rt.Log.Error("unable to read result", err) } return diff --git a/core/section/provider/provider.go b/domain/section/provider/provider.go similarity index 100% rename from core/section/provider/provider.go rename to domain/section/provider/provider.go diff --git a/domain/section/register.go b/domain/section/register.go new file mode 100644 index 00000000..5f679b5f --- /dev/null +++ b/domain/section/register.go @@ -0,0 +1,45 @@ +// Copyright 2016 Documize Inc. . All rights reserved. +// +// This software (Documize Community Edition) is licensed under +// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html +// +// You can operate outside the AGPL restrictions by purchasing +// Documize Enterprise Edition and obtaining a commercial license +// by contacting . +// +// https://documize.com + +package section + +import ( + "fmt" + + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/airtable" + "github.com/documize/community/domain/section/code" + "github.com/documize/community/domain/section/gemini" + "github.com/documize/community/domain/section/github" + "github.com/documize/community/domain/section/markdown" + "github.com/documize/community/domain/section/papertrail" + "github.com/documize/community/domain/section/provider" + "github.com/documize/community/domain/section/table" + "github.com/documize/community/domain/section/trello" + "github.com/documize/community/domain/section/wysiwyg" +) + +// Register sections +func Register(rt env.Runtime) { + provider.Register("code", &code.Provider{Runtime: rt}) + provider.Register("gemini", &gemini.Provider{Runtime: rt}) + provider.Register("github", &github.Provider{Runtime: rt}) + provider.Register("markdown", &markdown.Provider{Runtime: rt}) + provider.Register("papertrail", &papertrail.Provider{Runtime: rt}) + provider.Register("table", &table.Provider{Runtime: rt}) + provider.Register("code", &code.Provider{Runtime: rt}) + provider.Register("trello", &trello.Provider{Runtime: rt}) + provider.Register("wysiwyg", &wysiwyg.Provider{Runtime: rt}) + provider.Register("airtable", &airtable.Provider{Runtime: rt}) + + p := provider.List() + rt.Log.Info(fmt.Sprintf("Registered %d sections", len(p))) +} diff --git a/core/section/section_test.go b/domain/section/section_test.go similarity index 100% rename from core/section/section_test.go rename to domain/section/section_test.go diff --git a/core/section/table/table.go b/domain/section/table/table.go similarity index 91% rename from core/section/table/table.go rename to domain/section/table/table.go index 0cf0e0e1..0350a225 100644 --- a/core/section/table/table.go +++ b/domain/section/table/table.go @@ -14,11 +14,13 @@ package table import ( "net/http" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) // Provider represents Table type Provider struct { + Runtime env.Runtime } // Meta describes us diff --git a/core/section/trello/model.go b/domain/section/trello/model.go similarity index 100% rename from core/section/trello/model.go rename to domain/section/trello/model.go diff --git a/core/section/trello/trello.go b/domain/section/trello/trello.go similarity index 85% rename from core/section/trello/trello.go rename to domain/section/trello/trello.go index 0a8fdb5c..02fa0c93 100644 --- a/core/section/trello/trello.go +++ b/domain/section/trello/trello.go @@ -20,8 +20,8 @@ import ( "net/http" "github.com/documize/community/core/api/request" - "github.com/documize/community/core/log" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) var meta provider.TypeMeta @@ -36,8 +36,9 @@ func init() { meta.PageType = "tab" } -// Provider represents GitHub +// Provider represents Trello type Provider struct { + Runtime env.Runtime } // Meta describes us. @@ -46,7 +47,7 @@ func (*Provider) Meta() provider.TypeMeta { } // Command stub. -func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { +func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) { query := r.URL.Query() method := query.Get("method") @@ -70,7 +71,7 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R config.AppKey = request.ConfigString(meta.ConfigHandle(), "appKey") if len(config.AppKey) == 0 { - log.ErrorString("missing trello App Key") + p.Runtime.Log.Info("missing trello App Key") provider.WriteMessage(w, "trello", "Missing appKey") return } @@ -91,9 +92,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R render, err := getCards(config) if err != nil { - log.IfErr(err) + p.Runtime.Log.Error("failed to render cards", err) provider.WriteError(w, "trello", err) - log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid + ctx.SaveSecrets("") // failure means our secrets are invalid return } @@ -103,9 +104,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R render, err := getBoards(config) if err != nil { - log.IfErr(err) + p.Runtime.Log.Error("failed to render board", err) provider.WriteError(w, "trello", err) - log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid + ctx.SaveSecrets("") // failure means our secrets are invalid return } @@ -115,9 +116,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R render, err := getLists(config) if err != nil { - log.IfErr(err) + p.Runtime.Log.Error("failed to get Trello lists", err) provider.WriteError(w, "trello", err) - log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid + ctx.SaveSecrets("") // failure means our secrets are invalid return } @@ -136,7 +137,7 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R return default: - log.ErrorString("trello unknown method name: " + method) + p.Runtime.Log.Info("unknown trello method called: " + method) provider.WriteMessage(w, "trello", "missing method name") return } @@ -145,8 +146,11 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R var s secrets s.Token = config.Token b, e := json.Marshal(s) - log.IfErr(e) - log.IfErr(ctx.SaveSecrets(string(b))) + if err != nil { + p.Runtime.Log.Error("failed save Trello secrets", e) + } + + ctx.SaveSecrets(string(b)) } // Render just sends back HMTL as-is. @@ -176,7 +180,7 @@ func (*Provider) Render(ctx *provider.Context, config, data string) string { } // Refresh just sends back data as-is. -func (*Provider) Refresh(ctx *provider.Context, config, data string) string { +func (p *Provider) Refresh(ctx *provider.Context, config, data string) string { var c = trelloConfig{} json.Unmarshal([]byte(config), &c) @@ -189,7 +193,7 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) string { j, err := json.Marshal(refreshed) if err != nil { - log.Error("unable to marshall trello cards", err) + p.Runtime.Log.Error("failed to marshal trello data", err) return data } @@ -224,7 +228,6 @@ func getBoards(config trelloConfig) (boards []trelloBoard, err error) { } if err != nil { - fmt.Println(err) return nil, err } @@ -250,7 +253,6 @@ func getLists(config trelloConfig) (lists []trelloList, err error) { err = dec.Decode(&lists) if err != nil { - fmt.Println(err) return nil, err } diff --git a/core/section/wysiwyg/wysiwyg.go b/domain/section/wysiwyg/wysiwyg.go similarity index 91% rename from core/section/wysiwyg/wysiwyg.go rename to domain/section/wysiwyg/wysiwyg.go index b1bc60b8..6004d1d5 100644 --- a/core/section/wysiwyg/wysiwyg.go +++ b/domain/section/wysiwyg/wysiwyg.go @@ -14,11 +14,13 @@ package wysiwyg import ( "net/http" - "github.com/documize/community/core/section/provider" + "github.com/documize/community/core/env" + "github.com/documize/community/domain/section/provider" ) // Provider represents WYSIWYG type Provider struct { + Runtime env.Runtime } // Meta describes us diff --git a/edition/community.go b/edition/community.go index 1d26ccd6..d3a940e8 100644 --- a/edition/community.go +++ b/edition/community.go @@ -19,49 +19,49 @@ import ( "github.com/documize/community/core/api/endpoint" "github.com/documize/community/core/api/request" "github.com/documize/community/core/env" - "github.com/documize/community/core/section" + "github.com/documize/community/domain/section" "github.com/documize/community/edition/boot" "github.com/documize/community/edition/logging" _ "github.com/documize/community/embed" // the compressed front-end code and static data _ "github.com/go-sql-driver/mysql" // the mysql driver is required behind the scenes ) -func init() { +var rt env.Runtime + +func main() { // runtime stores server/application level information - runtime := env.Runtime{} + rt := env.Runtime{} // wire up logging implementation - runtime.Log = logging.NewLogger() + rt.Log = logging.NewLogger() // define product edition details - runtime.Product = env.ProdInfo{} - runtime.Product.Major = "1" - runtime.Product.Minor = "50" - runtime.Product.Patch = "1" - runtime.Product.Version = fmt.Sprintf("%s.%s.%s", runtime.Product.Major, runtime.Product.Minor, runtime.Product.Patch) - runtime.Product.Edition = "Community" - runtime.Product.Title = fmt.Sprintf("%s Edition", runtime.Product.Edition) - runtime.Product.License = env.License{} - runtime.Product.License.Seats = 1 - runtime.Product.License.Valid = true - runtime.Product.License.Trial = false - runtime.Product.License.Edition = "Community" + rt.Product = env.ProdInfo{} + rt.Product.Major = "1" + rt.Product.Minor = "50" + rt.Product.Patch = "2" + rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch) + rt.Product.Edition = "Community" + rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition) + rt.Product.License = env.License{} + rt.Product.License.Seats = 1 + rt.Product.License.Valid = true + rt.Product.License.Trial = false + rt.Product.License.Edition = "Community" // parse settings from command line and environment - runtime.Flags = env.ParseFlags() - flagsOK := boot.InitRuntime(&runtime) + rt.Flags = env.ParseFlags() + flagsOK := boot.InitRuntime(&rt) if flagsOK { // runtime.Log = runtime.Log.SetDB(runtime.Db) } // temp code repair - api.Runtime = runtime - request.Db = runtime.Db -} + api.Runtime = rt + request.Db = rt.Db -func main() { - section.Register() + section.Register(rt) ready := make(chan struct{}, 1) // channel is used for testing endpoint.Serve(ready) diff --git a/gui/package.json b/gui/package.json index 07cf3a98..9e4cfe87 100644 --- a/gui/package.json +++ b/gui/package.json @@ -1,6 +1,6 @@ { "name": "documize", - "version": "1.50.1", + "version": "1.50.2", "description": "The Document IDE", "private": true, "repository": "", diff --git a/meta.json b/meta.json index 9191bd6f..a59f2ec9 100644 --- a/meta.json +++ b/meta.json @@ -1,16 +1,16 @@ { "community": { - "version": "1.50.1", + "version": "1.50.2", "major": 1, "minor": 50, - "patch": 1 + "patch": 2 }, "enterprise": { - "version": "1.52.1", + "version": "1.52.2", "major": 1, "minor": 52, - "patch": 1 + "patch": 2 } } \ No newline at end of file