github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/rc/rc.go (about)

     1  // Package rc implements a remote control server and registry for rclone
     2  //
     3  // To register your internal calls, call rc.Add(path, function).  Your
     4  // function should take ane return a Param.  It can also return an
     5  // error.  Use rc.NewError to wrap an existing error along with an
     6  // http response type if another response other than 500 internal
     7  // error is required on error.
     8  package rc
     9  
    10  import (
    11  	"encoding/json"
    12  	"io"
    13  	_ "net/http/pprof" // install the pprof http handlers
    14  	"time"
    15  
    16  	"github.com/rclone/rclone/cmd/serve/httplib"
    17  )
    18  
    19  // Options contains options for the remote control server
    20  type Options struct {
    21  	HTTPOptions              httplib.Options
    22  	Enabled                  bool   // set to enable the server
    23  	Serve                    bool   // set to serve files from remotes
    24  	Files                    string // set to enable serving files locally
    25  	NoAuth                   bool   // set to disable auth checks on AuthRequired methods
    26  	WebUI                    bool   // set to launch the web ui
    27  	WebGUIUpdate             bool   // set to check new update
    28  	WebGUIForceUpdate        bool   // set to force download new update
    29  	WebGUINoOpenBrowser      bool   // set to disable auto opening browser
    30  	WebGUIFetchURL           string // set the default url for fetching webgui
    31  	AccessControlAllowOrigin string // set the access control for CORS configuration
    32  	JobExpireDuration        time.Duration
    33  	JobExpireInterval        time.Duration
    34  }
    35  
    36  // DefaultOpt is the default values used for Options
    37  var DefaultOpt = Options{
    38  	HTTPOptions:       httplib.DefaultOpt,
    39  	Enabled:           false,
    40  	JobExpireDuration: 60 * time.Second,
    41  	JobExpireInterval: 10 * time.Second,
    42  }
    43  
    44  func init() {
    45  	DefaultOpt.HTTPOptions.ListenAddr = "localhost:5572"
    46  }
    47  
    48  // WriteJSON writes JSON in out to w
    49  func WriteJSON(w io.Writer, out Params) error {
    50  	enc := json.NewEncoder(w)
    51  	enc.SetIndent("", "\t")
    52  	return enc.Encode(out)
    53  }