1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-16 00:04:40 +00:00

serve http, webdav, restic: ensure rclone exits if the port is in use

This commit is contained in:
Nick Craig-Wood
2018-11-01 17:16:31 +00:00
parent 1f05d5bf4a
commit bb5637d46a
7 changed files with 65 additions and 40 deletions

View File

@@ -46,7 +46,11 @@ control the stats printing.
f := cmd.NewFsSrc(args)
cmd.Run(false, true, command, func() error {
s := newServer(f, &httpflags.Opt)
s.serve()
err := s.Serve()
if err != nil {
return err
}
s.Wait()
return nil
})
},
@@ -54,30 +58,32 @@ control the stats printing.
// server contains everything to run the server
type server struct {
*httplib.Server
f fs.Fs
vfs *vfs.VFS
srv *httplib.Server
}
func newServer(f fs.Fs, opt *httplib.Options) *server {
mux := http.NewServeMux()
s := &server{
f: f,
vfs: vfs.New(f, &vfsflags.Opt),
srv: httplib.NewServer(mux, opt),
Server: httplib.NewServer(mux, opt),
f: f,
vfs: vfs.New(f, &vfsflags.Opt),
}
mux.HandleFunc("/", s.handler)
return s
}
// serve runs the http server - doesn't return
func (s *server) serve() {
err := s.srv.Serve()
// Serve runs the http server in the background.
//
// Use s.Close() and s.Wait() to shutdown server
func (s *server) Serve() error {
err := s.Server.Serve()
if err != nil {
fs.Errorf(s.f, "Opening listener: %v", err)
return err
}
fs.Logf(s.f, "Serving on %s", s.srv.URL())
s.srv.Wait()
fs.Logf(s.f, "Serving on %s", s.URL())
return nil
}
// handler reads incoming requests and dispatches them