Skip to content

Commit

Permalink
http: Begin migrating http service to use lib/http code, removing any…
Browse files Browse the repository at this point in the history
… concept of a server from the service.
  • Loading branch information
innovate-invent committed Jan 2, 2020
1 parent 048fe34 commit a434215
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
58 changes: 29 additions & 29 deletions cmd/serve/http/http.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package http

import (
"github.com/go-chi/chi"
"github.com/rclone/rclone/cmd/serve/httplib/serve"
"github.com/rclone/rclone/cmd/serve/httplib/serve/data"
"html/template"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"

"github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/cmd/serve/httplib"
"github.com/rclone/rclone/cmd/serve/httplib/httpflags"
"github.com/rclone/rclone/cmd/serve/httplib/serve"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/accounting"
httplib "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/lib/http/auth"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags"
"github.com/spf13/cobra"
)

func init() {
httpflags.AddFlags(Command.Flags())
httplib.AddFlags(Command.Flags())
auth.AddFlags(Command.Flags())
vfsflags.AddFlags(Command.Flags())
}

Expand All @@ -38,49 +43,46 @@ The server will log errors. Use -v to see access logs.
--bwlimit will be respected for file transfers. Use --stats to
control the stats printing.
` + httplib.Help + vfs.Help,
` + httplib.Help + auth.Help + vfs.Help,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args)
f := cmd.NewFsSrc(args)
cmd.Run(false, true, command, func() error {
s := newServer(f, &httpflags.Opt)
s := newServer(f)
err := s.Serve()
if err != nil {
return err
}
s.Wait()
return nil
})
},
}

// server contains everything to run the server
type server struct {
*httplib.Server
f fs.Fs
vfs *vfs.VFS
f fs.Fs
vfs *vfs.VFS
HTMLTemplate *template.Template // HTML template for web interface
}

func newServer(f fs.Fs, opt *httplib.Options) *server {
mux := http.NewServeMux()
func newServer(f fs.Fs) *server {
htmlTemplate, templateErr := data.GetTemplate()
if templateErr != nil {
log.Fatalf(templateErr.Error())
}
s := &server{
Server: httplib.NewServer(mux, opt),
f: f,
vfs: vfs.New(f, &vfsflags.Opt),
f: f,
vfs: vfs.New(f, &vfsflags.Opt),
HTMLTemplate: htmlTemplate,
}
mux.HandleFunc(s.Opt.BaseURL+"/", s.handler)

return s
}

// 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 {
return err
}
fs.Logf(s.f, "Serving on %s", s.URL())
//TODO rewrite using httplib.Route
httplib.Mount("/", http.HandlerFunc(s.handler))
return nil
}

Expand All @@ -93,12 +95,10 @@ func (s *server) handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Server", "rclone/"+fs.Version)

urlPath, ok := s.Path(w, r)
if !ok {
return
}
isDir := strings.HasSuffix(urlPath, "/")
remote := strings.Trim(urlPath, "/")
rctx := chi.RouteContext(r.Context())

isDir := strings.HasSuffix(rctx.RoutePath, "/")
remote := strings.Trim(rctx.RoutePath, "/")
if isDir {
s.serveDir(w, r, remote)
} else {
Expand Down
9 changes: 4 additions & 5 deletions cmd/serve/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"time"

_ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/cmd/serve/httplib"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/filter"
httplib "github.com/rclone/rclone/lib/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -30,9 +30,9 @@ const (
func startServer(t *testing.T, f fs.Fs) {
opt := httplib.DefaultOpt
opt.ListenAddr = testBindAddress
httpServer = newServer(f, &opt)
httpServer = newServer(f)
assert.NoError(t, httpServer.Serve())
testURL = httpServer.Server.URL()
testURL = httplib.URL()

// try to connect to the test server
pause := time.Millisecond
Expand Down Expand Up @@ -200,6 +200,5 @@ func TestGET(t *testing.T) {
}

func TestFinalise(t *testing.T) {
httpServer.Close()
httpServer.Wait()
_ = httplib.Shutdown()
}

0 comments on commit a434215

Please sign in to comment.