diff options
| author | Shulhan <m.shulhan@gmail.com> | 2021-01-08 19:39:36 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2021-01-08 19:39:36 +0700 |
| commit | 27ff5bc5b3ea87d8795996eba10d20678c61c0f2 (patch) | |
| tree | c5fe33220edae05dd12092acb88a1efb9fb70f99 /lib | |
| parent | dcbba0ceb1c82167cd332760d0fe02ba0ff47051 (diff) | |
| download | pakakeh.go-27ff5bc5b3ea87d8795996eba10d20678c61c0f2.tar.xz | |
http: embed the memfs.Options into ServerOptions
This is to minimize duplicate on fields names and give clear distinction
between options for Server and options for serving files on memory using
memfs.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/http/http_test.go | 11 | ||||
| -rw-r--r-- | lib/http/server.go | 9 | ||||
| -rw-r--r-- | lib/http/serveroptions.go | 34 |
3 files changed, 26 insertions, 28 deletions
diff --git a/lib/http/http_test.go b/lib/http/http_test.go index fb49ebce..ff80b972 100644 --- a/lib/http/http_test.go +++ b/lib/http/http_test.go @@ -10,6 +10,8 @@ import ( "net/http" "os" "testing" + + "github.com/shuLhan/share/lib/memfs" ) var ( @@ -48,9 +50,12 @@ func TestMain(m *testing.M) { var err error opts := &ServerOptions{ - Address: "127.0.0.1:8080", - Root: "./testdata", - MaxFileSize: 30, + Options: memfs.Options{ + Root: "./testdata", + MaxFileSize: 30, + WithContent: true, + }, + Address: "127.0.0.1:8080", } testServer, err = NewServer(opts) diff --git a/lib/http/server.go b/lib/http/server.go index b9e846a3..07c786e3 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -72,14 +72,7 @@ func NewServer(opts *ServerOptions) (srv *Server, err error) { } if len(opts.Root) > 0 { - memfsOpts := &memfs.Options{ - Root: opts.Root, - Includes: opts.Includes, - Excludes: opts.Excludes, - MaxFileSize: opts.MaxFileSize, - WithContent: true, - } - srv.Memfs, err = memfs.New(memfsOpts) + srv.Memfs, err = memfs.New(&opts.Options) if err != nil { return nil, err } diff --git a/lib/http/serveroptions.go b/lib/http/serveroptions.go index 12112a61..77eca661 100644 --- a/lib/http/serveroptions.go +++ b/lib/http/serveroptions.go @@ -8,16 +8,32 @@ import ( "net/http" "strconv" "strings" + + "github.com/shuLhan/share/lib/memfs" ) // // ServerOptions define an options to initialize HTTP server. // type ServerOptions struct { + // The server options embed memfs.Options to allow serving directory + // from the memory. + // // Root contains path to file system to be served. // This field is optional, if its empty the server will not serve the // local file system, only registered handler. - Root string + // + // Includes contains list of regex to include files to be served from + // Root. + // This field is optional. + // + // Excludes contains list of regex to exclude files to be served from + // Root. + // This field is optional. + // + // Development if its true, the Root file system is served by reading + // the content directly instead of using memory file system. + memfs.Options // Address define listen address, using ip:port format. // This field is optional, default to ":80". @@ -27,18 +43,6 @@ type ServerOptions struct { // This fields is optional. Conn *http.Server - // Includes contains list of regex to include files to be served from - // Root. - // This field is optional. - Includes []string - - // Excludes contains list of regex to exclude files to be served from - // Root. - // This field is optional. - Excludes []string - - MaxFileSize int64 - // CORSAllowOrigins contains global list of cross-site Origin that are // allowed during preflight requests by the OPTIONS method. // The list is case-sensitive. @@ -67,10 +71,6 @@ type ServerOptions struct { // can be made using credentials. CORSAllowCredentials bool - // Development if its true, the Root file system is served by reading - // the content directly instead of using memory file system. - Development bool - corsAllowHeadersAll bool // flag to indicate wildcards on list. corsAllowOriginsAll bool // flag to indicate wildcards on list. } |
