1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-07 22:45:24 +02:00

Refactored smart section registration and organization

This commit is contained in:
Harvey Kandola 2016-06-07 14:54:28 +01:00
parent f44695186b
commit 29bc955c6d
21 changed files with 340 additions and 392 deletions

View file

@ -20,12 +20,12 @@ import (
"strings"
"time"
"github.com/dgrijalva/jwt-go"
jwt "github.com/dgrijalva/jwt-go"
"github.com/documize/community/documize/api/endpoint/models"
"github.com/documize/community/documize/api/entity"
"github.com/documize/community/documize/api/request"
"github.com/documize/community/documize/api/util"
"github.com/documize/community/documize/section"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
"github.com/documize/community/wordsmith/utility"
)
@ -228,7 +228,7 @@ func ValidateAuthToken(w http.ResponseWriter, r *http.Request) {
// TODO should this go after token validation?
if s := r.URL.Query().Get("section"); s != "" {
if err:=section.Callback(s, w, r); err!=nil {
if err := provider.Callback(s, w, r); err != nil {
log.Error("section validation failure", err)
w.WriteHeader(http.StatusUnauthorized)
}

View file

@ -1,11 +1,11 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// 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 <sales@documize.com>.
// by contacting <sales@documize.com>.
//
// https://documize.com
@ -23,7 +23,7 @@ import (
"github.com/documize/community/documize/api/entity"
"github.com/documize/community/documize/api/request"
"github.com/documize/community/documize/api/util"
"github.com/documize/community/documize/section"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
"github.com/documize/community/wordsmith/utility"
@ -90,9 +90,9 @@ func AddDocumentPage(w http.ResponseWriter, r *http.Request) {
p.Context.Transaction = tx
output, ok := section.Render(model.Page.ContentType, model.Meta.Config, model.Meta.RawBody)
output, ok := provider.Render(model.Page.ContentType, model.Meta.Config, model.Meta.RawBody)
if !ok {
log.ErrorString("section.Render could not find: " + model.Page.ContentType)
log.ErrorString("provider.Render could not find: " + model.Page.ContentType)
}
model.Page.Body = output
@ -433,9 +433,9 @@ func UpdateDocumentPage(w http.ResponseWriter, r *http.Request) {
model.Page.SetDefaults()
model.Meta.SetDefaults()
output, ok := section.Render(model.Page.ContentType, model.Meta.Config, model.Meta.RawBody)
output, ok := provider.Render(model.Page.ContentType, model.Meta.Config, model.Meta.RawBody)
if !ok {
log.ErrorString("section.Render could not find: " + model.Page.ContentType)
log.ErrorString("provider.Render could not find: " + model.Page.ContentType)
}
model.Page.Body = output

View file

@ -1,11 +1,11 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// 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 <sales@documize.com>.
// by contacting <sales@documize.com>.
//
// https://documize.com
@ -18,7 +18,7 @@ import (
"github.com/documize/community/documize/api/entity"
"github.com/documize/community/documize/api/request"
"github.com/documize/community/documize/api/util"
"github.com/documize/community/documize/section"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
)
@ -26,7 +26,7 @@ import (
func GetSections(w http.ResponseWriter, r *http.Request) {
method := "GetSections"
json, err := json.Marshal(section.GetSectionMeta())
json, err := json.Marshal(provider.GetSectionMeta())
if err != nil {
writeJSONMarshalError(w, method, "section", err)
@ -70,8 +70,8 @@ func RunSectionCommand(w http.ResponseWriter, r *http.Request) {
return
}
if !section.Command(sectionName, w, r) {
log.ErrorString("Unable to run section.Command() for: " + sectionName)
if !provider.Command(sectionName, w, r) {
log.ErrorString("Unable to run provider.Command() for: " + sectionName)
writeNotFoundError(w, "RunSectionCommand", sectionName)
}
}
@ -126,15 +126,15 @@ func RefreshSections(w http.ResponseWriter, r *http.Request) {
}
// Ask for data refresh
data, ok := section.Refresh(page.ContentType, pm.Config, pm.RawBody)
data, ok := provider.Refresh(page.ContentType, pm.Config, pm.RawBody)
if !ok {
log.ErrorString("section.Refresh could not find: " + page.ContentType)
log.ErrorString("provider.Refresh could not find: " + page.ContentType)
}
// Render again
body, ok := section.Render(page.ContentType, pm.Config, data)
body, ok := provider.Render(page.ContentType, pm.Config, data)
if !ok {
log.ErrorString("section.Render could not find: " + page.ContentType)
log.ErrorString("provider.Render could not find: " + page.ContentType)
}
// Compare to stored render

View file

@ -1,11 +1,11 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// 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 <sales@documize.com>.
// by contacting <sales@documize.com>.
//
// https://documize.com
@ -14,6 +14,7 @@ package main
import (
"github.com/documize/community/documize/api/endpoint"
"github.com/documize/community/documize/section"
"github.com/documize/community/wordsmith/environment"
_ "github.com/go-sql-driver/mysql" // the mysql driver is required behind the scenes
@ -23,5 +24,7 @@ func main() {
environment.Parse("db") // process the db value first
ready := make(chan struct{}, 1) // channel is used for testing
section.Register()
endpoint.Serve(ready)
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package asana
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type asana struct {
// Provider represents Asana
type Provider struct {
}
func init() {
sectionsMap["asana"] = &asana{}
}
func (*asana) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "3a3f4661-2195-46b1-a69c-546eaccb5f93"
section.Title = "Asana"
@ -35,16 +35,16 @@ func (*asana) Meta() TypeMeta {
}
// Command stub.
func (*asana) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*asana) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*asana) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package code
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type code struct {
// Provider represents code snippet
type Provider struct {
}
func init() {
sectionsMap["code"] = &code{}
}
func (*code) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "4f6f2b02-8397-483d-9bb9-eea1fef13304"
section.Title = "Code"
@ -35,16 +35,16 @@ func (*code) Meta() TypeMeta {
}
// Command stub.
func (*code) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*code) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*code) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package docusign
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type docusign struct {
// Provider represents DocuSign
type Provider struct {
}
func init() {
sectionsMap["docusign"] = &docusign{}
}
func (*docusign) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "a195f983-4bd7-412b-879e-2d71d2f822a7"
section.Title = "DocuSign"
@ -35,16 +35,16 @@ func (*docusign) Meta() TypeMeta {
}
// Command stub.
func (*docusign) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*docusign) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*docusign) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,7 +9,7 @@
//
// https://documize.com
package section
package gemini
import (
"bytes"
@ -21,6 +21,7 @@ import (
"net/http"
"strings"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
)
@ -55,17 +56,13 @@ const renderTemplate = `
{{end}}
`
type gemini struct {
// Provider represents Gemini
type Provider struct {
}
// Register ourselves.
func init() {
sectionsMap["gemini"] = &gemini{}
}
// Meta describes this section type.
func (*gemini) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "23b133f9-4020-4616-9291-a98fb939735f"
section.Title = "Gemini"
section.Description = "Display work items and tickets from workspaces"
@ -75,7 +72,7 @@ func (*gemini) Meta() TypeMeta {
}
// Render converts Gemini data into HTML suitable for browser rendering.
func (*gemini) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
var items []geminiItem
var payload = geminiRender{}
var c = geminiConfig{}
@ -99,12 +96,12 @@ func (*gemini) Render(config, data string) string {
}
// Command handles authentication, workspace listing and items retrieval.
func (*gemini) Command(w http.ResponseWriter, r *http.Request) {
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
writeMessage(w, "gemini", "missing method name")
provider.WriteMessage(w, "gemini", "missing method name")
return
}
@ -119,7 +116,7 @@ func (*gemini) Command(w http.ResponseWriter, r *http.Request) {
}
// Refresh just sends back data as-is.
func (*gemini) Refresh(config, data string) (newData string) {
func (*Provider) Refresh(config, data string) (newData string) {
var c = geminiConfig{}
err := json.Unmarshal([]byte(config), &c)
@ -234,7 +231,7 @@ func auth(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeMessage(w, "gemini", "Bad payload")
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
@ -242,24 +239,24 @@ func auth(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &config)
if err != nil {
writeMessage(w, "gemini", "Bad payload")
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean()
if len(config.URL) == 0 {
writeMessage(w, "gemini", "Missing URL value")
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
writeMessage(w, "gemini", "Missing Username value")
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
writeMessage(w, "gemini", "Missing APIKey value")
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
@ -273,12 +270,12 @@ func auth(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
writeForbidden(w)
provider.WriteForbidden(w)
return
}
@ -290,11 +287,11 @@ func auth(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
writeJSON(w, g)
provider.WriteJSON(w, g)
}
func workspace(w http.ResponseWriter, r *http.Request) {
@ -302,7 +299,7 @@ func workspace(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeMessage(w, "gemini", "Bad payload")
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
@ -310,29 +307,29 @@ func workspace(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &config)
if err != nil {
writeMessage(w, "gemini", "Bad payload")
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean()
if len(config.URL) == 0 {
writeMessage(w, "gemini", "Missing URL value")
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
writeMessage(w, "gemini", "Missing Username value")
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
writeMessage(w, "gemini", "Missing APIKey value")
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
if config.UserID == 0 {
writeMessage(w, "gemini", "Missing UserId value")
provider.WriteMessage(w, "gemini", "Missing UserId value")
return
}
@ -346,12 +343,12 @@ func workspace(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
writeForbidden(w)
provider.WriteForbidden(w)
return
}
@ -363,11 +360,11 @@ func workspace(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
writeJSON(w, workspace)
provider.WriteJSON(w, workspace)
}
func items(w http.ResponseWriter, r *http.Request) {
@ -375,7 +372,7 @@ func items(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeMessage(w, "gemini", "Bad payload")
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
@ -383,24 +380,24 @@ func items(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &config)
if err != nil {
writeMessage(w, "gemini", "Bad payload")
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean()
if len(config.URL) == 0 {
writeMessage(w, "gemini", "Missing URL value")
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
writeMessage(w, "gemini", "Missing Username value")
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
writeMessage(w, "gemini", "Missing APIKey value")
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
@ -409,7 +406,7 @@ func items(w http.ResponseWriter, r *http.Request) {
filter, err := json.Marshal(config.Filter)
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
@ -423,12 +420,12 @@ func items(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
writeForbidden(w)
provider.WriteForbidden(w)
return
}
@ -440,9 +437,9 @@ func items(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "gemini", err)
provider.WriteError(w, "gemini", err)
return
}
writeJSON(w, items)
provider.WriteJSON(w, items)
}

View file

@ -1,36 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. 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 <sales@documize.com>.
//
// https://documize.com
package section
import gh "github.com/documize/community/documize/section/github"
type github struct {
gh.GithubT
}
func (*github) Meta() TypeMeta {
section := TypeMeta{}
section.ID = "38c0e4c5-291c-415e-8a4d-262ee80ba5df"
section.Title = "GitHub"
section.Description = "Code commits and branches"
section.ContentType = "github"
//section.Preview = true
section.Callback = gh.Callback
return section
}
func init() {
sectionsMap["github"] = &github{}
}

View file

@ -24,12 +24,32 @@ import (
"time"
"github.com/documize/community/documize/api/request"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
gogithub "github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// Provider represents GitHub
type Provider struct {
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "38c0e4c5-291c-415e-8a4d-262ee80ba5df"
section.Title = "GitHub"
section.Description = "Code commits and branches"
section.ContentType = "github"
//section.Preview = true
section.Callback = Callback
return section
}
const configKey = "SECTION-GITHUB"
func clientID() string {
@ -43,46 +63,15 @@ func authorizationCallbackURL() string {
return request.ConfigString(configKey, "authorizationCallbackURL")
}
type GithubT struct {
/* TODO use the shared functions in the "section" package
WriteJSON func (w http.ResponseWriter, v interface{})
WriteString func(w http.ResponseWriter, data string)
WriteEmpty func (w http.ResponseWriter)
WriteMarshalError func (w http.ResponseWriter, err error)
WriteMessage func (w http.ResponseWriter, section, msg string)
WriteError func (w http.ResponseWriter, section string, err error)
WriteForbidden func (w http.ResponseWriter)
*/
}
/* done at top level in the "section" package
func init() {
sectionsMap["github"] = &GithubT{}
}
func (*GithubT) Meta() TypeMeta {
section := TypeMeta{}
section.ID = "38c0e4c5-291c-415e-8a4d-262ee80ba5df"
section.Title = "GitHub"
section.Description = "Code commits and branches"
section.ContentType = "github"
//section.Preview = true
return section
}
*/
// Command to run the various functions required...
func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
func (t *Provider) Command(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
msg := "missing method name"
log.ErrorString("github: " + msg)
writeMessage(w, "gitub", msg)
provider.WriteMessage(w, "gitub", msg)
return
}
@ -93,7 +82,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
}
ret.CID = clientID()
ret.URL = authorizationCallbackURL()
writeJSON(w, ret)
provider.WriteJSON(w, ret)
return
}
@ -103,7 +92,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
if err != nil {
msg := "Bad body"
log.ErrorString("github: " + msg)
writeMessage(w, "gitub", msg)
provider.WriteMessage(w, "gitub", msg)
return
}
@ -112,7 +101,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Error("github Command Unmarshal", err)
writeError(w, "github", err)
provider.WriteError(w, "github", err)
return
}
@ -121,7 +110,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
if len(config.Token) == 0 {
msg := "Missing token"
log.ErrorString("github: " + msg)
writeMessage(w, "gitub", msg)
provider.WriteMessage(w, "gitub", msg)
return
}
@ -134,25 +123,25 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
render, err := t.getCommits(client, config)
if err != nil {
log.Error("github getCommits:", err)
writeError(w, "github", err)
provider.WriteError(w, "github", err)
return
}
writeJSON(w, render)
provider.WriteJSON(w, render)
case "repos":
me, _, err := client.Users.Get("")
if err != nil {
log.Error("github get user details:", err)
writeError(w, "github", err)
provider.WriteError(w, "github", err)
return
}
orgs, _, err := client.Organizations.List("", nil)
if err != nil {
log.Error("github get user's organisations:", err)
writeError(w, "github", err)
provider.WriteError(w, "github", err)
return
}
@ -175,7 +164,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
log.Error("github get user/org repositories:", err)
writeError(w, "github", err)
provider.WriteError(w, "github", err)
return
}
for kr, vr := range repos {
@ -197,18 +186,18 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
render = sortRepos(render)
writeJSON(w, render)
provider.WriteJSON(w, render)
case "lists":
if config.Owner == "" || config.Repo == "" {
writeJSON(w, []githubBranch{}) // we have nothing to return
provider.WriteJSON(w, []githubBranch{}) // we have nothing to return
return
}
branches, _, err := client.Repositories.ListBranches(config.Owner, config.Repo,
&gogithub.ListOptions{PerPage: 100})
if err != nil {
log.Error("github get branch details:", err)
writeError(w, "github", err)
provider.WriteError(w, "github", err)
return
}
render := make([]githubBranch, len(branches))
@ -221,15 +210,14 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
}
}
writeJSON(w, render)
provider.WriteJSON(w, render)
default:
writeEmpty(w)
provider.WriteEmpty(w)
}
}
func (*GithubT) githubClient(config githubConfig) *gogithub.Client {
func (*Provider) githubClient(config githubConfig) *gogithub.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)
@ -238,7 +226,7 @@ func (*GithubT) githubClient(config githubConfig) *gogithub.Client {
return gogithub.NewClient(tc)
}
func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]githubBranchCommits, error) {
func (*Provider) getCommits(client *gogithub.Client, config githubConfig) ([]githubBranchCommits, error) {
opts := &gogithub.CommitsListOptions{
SHA: config.Branch,
@ -320,7 +308,7 @@ func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]gith
}
// Refresh ... gets the latest version
func (t *GithubT) Refresh(configJSON, data string) string {
func (t *Provider) Refresh(configJSON, data string) string {
var c = githubConfig{}
json.Unmarshal([]byte(configJSON), &c)
c.Clean()
@ -349,7 +337,7 @@ type githubRender struct {
}
// Render ... just returns the data given
func (*GithubT) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
raw := []githubBranchCommits{}
payload := githubRender{}
@ -535,68 +523,3 @@ func Callback(res http.ResponseWriter, req *http.Request) error {
return nil
}
// TODO don't copy these functions... use the ones in the "section" package
// writeJSON writes data as JSON to HTTP response.
func writeJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
j, err := json.Marshal(v)
if err != nil {
writeMarshalError(w, err)
return
}
_, err = w.Write(j)
log.IfErr(err)
}
// writeString writes string tp HTTP response.
func writeString(w http.ResponseWriter, data string) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(data))
log.IfErr(err)
}
// writeEmpty returns just OK to HTTP response.
func writeEmpty(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("{}"))
log.IfErr(err)
}
// writeMarshalError write JSON marshalling error to HTTP response.
func writeMarshalError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
log.IfErr(err2)
log.Error("JSON marshall failed", err)
}
func writeMessage(w http.ResponseWriter, section, msg string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("{Message: " + msg + "}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Error for section %s: %s", section, msg))
}
func writeError(w http.ResponseWriter, section string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Error for section %s", section), err)
}
func writeForbidden(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))
log.IfErr(err)
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package intercom
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type intercom struct {
// Provider represents Intercom
type Provider struct {
}
func init() {
sectionsMap["intercom"] = &intercom{}
}
func (*intercom) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "bf40314d-3b3c-41f9-b283-517da56aa7e4"
section.Title = "Intercom"
@ -35,16 +35,16 @@ func (*intercom) Meta() TypeMeta {
}
// Command stub.
func (*intercom) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*intercom) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*intercom) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package mailchimp
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type mailchimp struct {
// Provider represents Mailchimp
type Provider struct {
}
func init() {
sectionsMap["mailchimp"] = &mailchimp{}
}
func (*mailchimp) Meta() TypeMeta {
section := TypeMeta{}
// Meta descibes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "feab735b-2d02-4bb1-b501-ced825e22465"
section.Title = "Mailchimp"
@ -35,16 +35,16 @@ func (*mailchimp) Meta() TypeMeta {
}
// Command stub.
func (*mailchimp) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*mailchimp) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*mailchimp) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,23 +9,22 @@
//
// https://documize.com
package section
package markdown
import (
"net/http"
"github.com/documize/blackfriday"
"github.com/documize/community/documize/section/provider"
)
type markdown struct {
// Provider represents Markdown
type Provider struct {
}
func init() {
sectionsMap["markdown"] = &markdown{}
}
func (*markdown) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "1470bb4a-36c6-4a98-a443-096f5658378b"
section.Title = "Markdown"
@ -37,18 +36,18 @@ func (*markdown) Meta() TypeMeta {
}
// Command stub.
func (*markdown) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render converts markdown data into HTML suitable for browser rendering.
func (*markdown) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
result := blackfriday.MarkdownCommon([]byte(data))
return string(result)
}
// Refresh just sends back data as-is.
func (*markdown) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,7 +9,7 @@
//
// https://documize.com
package section
package provider
import (
"encoding/json"
@ -22,7 +22,7 @@ import (
)
// sectionsMap is where individual sections register themselves.
var sectionsMap = make(map[string]section)
var sectionsMap = make(map[string]Provider)
// TypeMeta details a "smart section" that represents a "page" in a document.
type TypeMeta struct {
@ -35,14 +35,24 @@ type TypeMeta struct {
Callback func(http.ResponseWriter, *http.Request) error `json:"-"`
}
// section represents a 'page' in a document.
type section interface {
// Provider represents a 'page' in a document.
type Provider interface {
Meta() TypeMeta // Meta returns section details
Command(w http.ResponseWriter, r *http.Request) // Command is general-purpose method that can return data to UI
Render(config, data string) string // Render converts section data into presentable HTML
Refresh(config, data string) string // Refresh returns latest data
}
// Register makes document section type available
func Register(name string, p Provider) {
sectionsMap[name] = p
}
// List returns available types
func List() map[string]Provider {
return sectionsMap
}
// GetSectionMeta returns a list of smart sections.
func GetSectionMeta() []TypeMeta {
sections := []TypeMeta{}
@ -92,15 +102,15 @@ func Refresh(section, config, data string) (string, bool) {
return "", false
}
// writeJSON writes data as JSON to HTTP response.
func writeJSON(w http.ResponseWriter, v interface{}) {
// WriteJSON writes data as JSON to HTTP response.
func WriteJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
j, err := json.Marshal(v)
if err != nil {
writeMarshalError(w, err)
WriteMarshalError(w, err)
return
}
@ -108,23 +118,23 @@ func writeJSON(w http.ResponseWriter, v interface{}) {
log.IfErr(err)
}
// writeString writes string tp HTTP response.
func writeString(w http.ResponseWriter, data string) {
// WriteString writes string tp HTTP response.
func WriteString(w http.ResponseWriter, data string) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(data))
log.IfErr(err)
}
// writeEmpty returns just OK to HTTP response.
func writeEmpty(w http.ResponseWriter) {
// WriteEmpty returns just OK to HTTP response.
func WriteEmpty(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("{}"))
log.IfErr(err)
}
// writeMarshalError write JSON marshalling error to HTTP response.
func writeMarshalError(w http.ResponseWriter, err error) {
// WriteMarshalError write JSON marshalling error to HTTP response.
func WriteMarshalError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
@ -132,7 +142,8 @@ func writeMarshalError(w http.ResponseWriter, err error) {
log.Error("JSON marshall failed", err)
}
func writeMessage(w http.ResponseWriter, section, msg string) {
// WriteMessage write string to HTTP response.
func WriteMessage(w http.ResponseWriter, section, msg string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("{Message: " + msg + "}"))
@ -140,7 +151,8 @@ func writeMessage(w http.ResponseWriter, section, msg string) {
log.Info(fmt.Sprintf("Error for section %s: %s", section, msg))
}
func writeError(w http.ResponseWriter, section string, err error) {
// WriteError write given error to HTTP response.
func WriteError(w http.ResponseWriter, section string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
@ -148,7 +160,8 @@ func writeError(w http.ResponseWriter, section string, err error) {
log.Error(fmt.Sprintf("Error for section %s", section), err)
}
func writeForbidden(w http.ResponseWriter) {
// WriteForbidden write 403 to HTTP response.
func WriteForbidden(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))

View file

@ -0,0 +1,54 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. 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 <sales@documize.com>.
//
// https://documize.com
package section
import (
"fmt"
"github.com/documize/community/documize/section/asana"
"github.com/documize/community/documize/section/code"
"github.com/documize/community/documize/section/docusign"
"github.com/documize/community/documize/section/gemini"
"github.com/documize/community/documize/section/github"
"github.com/documize/community/documize/section/intercom"
"github.com/documize/community/documize/section/mailchimp"
"github.com/documize/community/documize/section/markdown"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/documize/section/salesforce"
"github.com/documize/community/documize/section/stripe"
"github.com/documize/community/documize/section/table"
"github.com/documize/community/documize/section/trello"
"github.com/documize/community/documize/section/wysiwyg"
"github.com/documize/community/documize/section/zendesk"
"github.com/documize/community/wordsmith/log"
)
// Register sections
func Register() {
provider.Register("asana", &asana.Provider{})
provider.Register("code", &code.Provider{})
provider.Register("docusign", &docusign.Provider{})
provider.Register("gemini", &gemini.Provider{})
provider.Register("github", &github.Provider{})
provider.Register("intercom", &intercom.Provider{})
provider.Register("mailchimp", &mailchimp.Provider{})
provider.Register("markdown", &markdown.Provider{})
provider.Register("salesforce", &salesforce.Provider{})
provider.Register("stripe", &stripe.Provider{})
provider.Register("table", &table.Provider{})
provider.Register("trello", &trello.Provider{})
provider.Register("wysiwyg", &wysiwyg.Provider{})
provider.Register("zendesk", &zendesk.Provider{})
p := provider.List()
log.Info(fmt.Sprintf("Documize registered %d smart sections", len(p)))
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package salesforce
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type salesforce struct {
// Provider represents Salesforce
type Provider struct {
}
func init() {
sectionsMap["salesforce"] = &salesforce{}
}
func (*salesforce) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "2240c0f8-b795-47b0-bcd4-5f6b171a2ffd"
section.Title = "Salesforce"
@ -35,16 +35,16 @@ func (*salesforce) Meta() TypeMeta {
}
// Command stub.
func (*salesforce) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*salesforce) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*salesforce) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package stripe
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type stripe struct {
// Provider represents Stripe
type Provider struct {
}
func init() {
sectionsMap["stripe"] = &stripe{}
}
func (*stripe) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "cb597d8d-c724-4034-b272-e8d9e261444f"
section.Title = "Stripe"
@ -35,16 +35,16 @@ func (*stripe) Meta() TypeMeta {
}
// Command stub.
func (*stripe) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*stripe) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*stripe) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package table
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type table struct {
// Provider represents Table
type Provider struct {
}
func init() {
sectionsMap["table"] = &table{}
}
func (*table) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "81a2ea93-2dfc-434d-841e-54b832492c92"
section.Title = "Tabular"
@ -35,16 +35,16 @@ func (*table) Meta() TypeMeta {
}
// Command stub.
func (*table) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render sends back data as-is (HTML).
func (*table) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*table) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,7 +9,7 @@
//
// https://documize.com
package section
package trello
import (
"bytes"
@ -20,18 +20,17 @@ import (
"net/http"
"strings"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
)
type trello struct {
// Provider represents Trello
type Provider struct {
}
func init() {
sectionsMap["trello"] = &trello{}
}
func (*trello) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "c455a552-202e-441c-ad79-397a8152920b"
section.Title = "Trello"
section.Description = "Embed cards from boards and lists"
@ -41,12 +40,12 @@ func (*trello) Meta() TypeMeta {
}
// Command stub.
func (*trello) Command(w http.ResponseWriter, r *http.Request) {
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
writeMessage(w, "trello", "missing method name")
provider.WriteMessage(w, "trello", "missing method name")
return
}
@ -54,7 +53,7 @@ func (*trello) Command(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeMessage(w, "trello", "Bad body")
provider.WriteMessage(w, "trello", "Bad body")
return
}
@ -62,19 +61,19 @@ func (*trello) Command(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &config)
if err != nil {
writeError(w, "trello", err)
provider.WriteError(w, "trello", err)
return
}
config.Clean()
if len(config.AppKey) == 0 {
writeMessage(w, "trello", "Missing appKey")
provider.WriteMessage(w, "trello", "Missing appKey")
return
}
if len(config.Token) == 0 {
writeMessage(w, "trello", "Missing token")
provider.WriteMessage(w, "trello", "Missing token")
return
}
@ -84,38 +83,38 @@ func (*trello) Command(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
writeError(w, "trello", err)
provider.WriteError(w, "trello", err)
return
}
writeJSON(w, render)
provider.WriteJSON(w, render)
case "boards":
render, err := getBoards(config)
if err != nil {
fmt.Println(err)
writeError(w, "trello", err)
provider.WriteError(w, "trello", err)
return
}
writeJSON(w, render)
provider.WriteJSON(w, render)
case "lists":
render, err := getLists(config)
if err != nil {
fmt.Println(err)
writeError(w, "trello", err)
provider.WriteError(w, "trello", err)
return
}
writeJSON(w, render)
provider.WriteJSON(w, render)
}
}
// Render just sends back HMTL as-is.
func (*trello) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
raw := []trelloListCards{}
payload := trelloRender{}
var c = trelloConfig{}
@ -141,7 +140,7 @@ func (*trello) Render(config, data string) string {
}
// Refresh just sends back data as-is.
func (*trello) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
var c = trelloConfig{}
json.Unmarshal([]byte(config), &c)

View file

@ -9,25 +9,21 @@
//
// https://documize.com
package section
package wysiwyg
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
// reading:
// composition
// reflection
type wysiwyg struct {
// Provider represents WYSIWYG
type Provider struct {
}
func init() {
sectionsMap["wysiwyg"] = &wysiwyg{}
}
func (*wysiwyg) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "0f024fa0-d017-4bad-a094-2c13ce6edad7"
section.Title = "Rich Text"
@ -39,16 +35,16 @@ func (*wysiwyg) Meta() TypeMeta {
}
// Command stub.
func (*wysiwyg) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render returns data as-is (HTML).
func (*wysiwyg) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*wysiwyg) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}

View file

@ -9,21 +9,21 @@
//
// https://documize.com
package section
package zendesk
import (
"net/http"
"github.com/documize/community/documize/section/provider"
)
type zendesk struct {
// Provider represents Zendesk
type Provider struct {
}
func init() {
sectionsMap["zendesk"] = &zendesk{}
}
func (*zendesk) Meta() TypeMeta {
section := TypeMeta{}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "05b02331-4ca2-4fc2-a31a-82bc45dceafe"
section.Title = "Zendesk"
@ -35,16 +35,16 @@ func (*zendesk) Meta() TypeMeta {
}
// Command stub.
func (*zendesk) Command(w http.ResponseWriter, r *http.Request) {
writeEmpty(w)
func (*Provider) Command(w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*zendesk) Render(config, data string) string {
func (*Provider) Render(config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*zendesk) Refresh(config, data string) string {
func (*Provider) Refresh(config, data string) string {
return data
}