github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/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 and 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  	libhttp "github.com/rclone/rclone/lib/http"
    17  )
    18  
    19  // Options contains options for the remote control server
    20  type Options struct {
    21  	HTTP                libhttp.Config
    22  	Auth                libhttp.AuthConfig
    23  	Template            libhttp.TemplateConfig
    24  	Enabled             bool   // set to enable the server
    25  	Serve               bool   // set to serve files from remotes
    26  	ServeNoModTime      bool   // don't read the modification time
    27  	Files               string // set to enable serving files locally
    28  	NoAuth              bool   // set to disable auth checks on AuthRequired methods
    29  	WebUI               bool   // set to launch the web ui
    30  	WebGUIUpdate        bool   // set to check new update
    31  	WebGUIForceUpdate   bool   // set to force download new update
    32  	WebGUINoOpenBrowser bool   // set to disable auto opening browser
    33  	WebGUIFetchURL      string // set the default url for fetching webgui
    34  	EnableMetrics       bool   // set to disable prometheus metrics on /metrics
    35  	JobExpireDuration   time.Duration
    36  	JobExpireInterval   time.Duration
    37  }
    38  
    39  // DefaultOpt is the default values used for Options
    40  var DefaultOpt = Options{
    41  	HTTP:              libhttp.DefaultCfg(),
    42  	Auth:              libhttp.DefaultAuthCfg(),
    43  	Template:          libhttp.DefaultTemplateCfg(),
    44  	Enabled:           false,
    45  	JobExpireDuration: 60 * time.Second,
    46  	JobExpireInterval: 10 * time.Second,
    47  }
    48  
    49  func init() {
    50  	DefaultOpt.HTTP.ListenAddr = []string{"localhost:5572"}
    51  }
    52  
    53  // WriteJSON writes JSON in out to w
    54  func WriteJSON(w io.Writer, out Params) error {
    55  	enc := json.NewEncoder(w)
    56  	enc.SetIndent("", "\t")
    57  	return enc.Encode(out)
    58  }