github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/documize/api/plugins/glick.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under 
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>. 
     9  //
    10  // https://documize.com
    11  
    12  // Package plugins manages the plug-in componenets of the Documize system.
    13  package plugins
    14  
    15  import (
    16  	"bytes"
    17  	"fmt"
    18  	"io/ioutil"
    19  	"time"
    20  
    21  	"github.com/documize/community/documize/api/convert/apidocumizecom"
    22  	"github.com/documize/community/documize/api/convert/documizeapi"
    23  	"github.com/documize/community/documize/api/convert/html"
    24  	"github.com/documize/community/documize/api/convert/md"
    25  	"github.com/documize/community/documize/api/request"
    26  	"github.com/documize/community/wordsmith/api"
    27  	"github.com/documize/community/wordsmith/environment"
    28  	"github.com/documize/community/wordsmith/log"
    29  	"github.com/documize/glick"
    30  )
    31  
    32  // PluginFile is the path to the file containing the configuration information for the plugin system in JSON format.
    33  var PluginFile = "DB" // this points to the database
    34  var insecure = "false"
    35  
    36  func init() {
    37  	environment.GetString(&PluginFile, "plugin", false,
    38  		"the JSON file describing plugins, default 'DB' uses the database config table 'FILEPLUGINS' entry", nil)
    39  	environment.GetString(&insecure, "insecure", false,
    40  		"if 'true' allow https endpoints with invalid certificates (only for testing)", nil)
    41  }
    42  
    43  type infoLog struct{}
    44  
    45  func (i infoLog) Write(b []byte) (int, error) {
    46  	log.Info(string(b))
    47  	return len(b), nil
    48  }
    49  
    50  type errorLog struct{}
    51  
    52  func (i errorLog) Write(b []byte) (int, error) {
    53  	log.ErrorString(string(b))
    54  	return len(b), nil
    55  }
    56  
    57  // Lib holds a pointer to the global glick Library for the Documize app.
    58  var Lib *glick.Library
    59  
    60  // LibSetup configures the global library at Lib,
    61  // largely based on the "config.json" file. It should be called only once.
    62  func LibSetup() error {
    63  	if insecure == "true" {
    64  		glick.InsecureSkipVerifyTLS = true
    65  	}
    66  	var err error
    67  	Lib, err = glick.New(nil)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	ppo := func() interface{} { return interface{}(&api.DocumentConversionResponse{}) }
    73  	err = Lib.RegAPI("Convert", &api.DocumentConversionRequest{}, ppo, 2*time.Minute)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	for _, xtn := range []string{"htm", "html"} {
    79  		err = Lib.RegPlugin("Convert", xtn, html.Convert, nil)
    80  		if err != nil {
    81  			return err
    82  		}
    83  	}
    84  
    85  	for _, xtn := range []string{"md"} {
    86  		err = Lib.RegPlugin("Convert", xtn, md.Convert, nil)
    87  		if err != nil {
    88  			return err
    89  		}
    90  	}
    91  
    92  	if err = apidocumizecom.CheckToken(); err == nil {
    93  		for _, xtn := range []string{"doc", "docx"} {
    94  			err = Lib.RegPlugin("Convert", xtn, apidocumizecom.MSwordConvert, nil)
    95  			if err != nil {
    96  				return err
    97  			}
    98  		}
    99  	} else {
   100  		log.Info(fmt.Sprintf("Documize API token is not valid: %v", err))
   101  	}
   102  
   103  	err = Lib.RegPlugin("Convert", "documizeapi", documizeapi.Convert, nil)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	ppoe := func() interface{} { return interface{}(&api.DocumentExport{}) }
   109  	err = Lib.RegAPI("Export", []byte{}, ppoe, 5*time.Minute)
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	var json = make([]byte, 0)
   115  	if PluginFile == "DB" {
   116  		json = []byte(request.ConfigString("FILEPLUGINS", ""))
   117  		if len(bytes.TrimSpace(json)) == 0 {
   118  			return nil // don't fail if the DB does not exist yet
   119  		}
   120  	} else {
   121  		json, err = ioutil.ReadFile(PluginFile)
   122  		if err != nil {
   123  			log.Info("Plugin file '" + PluginFile + "' not found, using no plugins")
   124  			json = []byte(" [ ] \n")
   125  			err = nil
   126  		}
   127  	}
   128  	err = Lib.Configure(json)
   129  	if err != nil {
   130  		//fmt.Println("DEBUG plugin: "+string(json))
   131  		return err
   132  	}
   133  	return Lib.StartLocalRPCservers(infoLog{}, errorLog{})
   134  }