aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-04-15 02:11:44 +0700
committerShulhan <ms@kilabit.info>2019-04-15 02:11:44 +0700
commit5cf76aeb90afbe6638b9526b354a33307b904a06 (patch)
tree41e399232efa1cc3901e69d227925238825e712f
parentc3e0cd88fb0ed9e7c55412f74177124db8a13cc0 (diff)
downloadpakakeh.go-5cf76aeb90afbe6638b9526b354a33307b904a06.tar.xz
http: change server initialization using options
Previously, we only pass the root directory to be served and custom http Server connection when creating new server. This commit use a custom type to initialize server using ServerOptions. This model is more robust and flexible. Caller can set custom address, setting filter to include or exclude specific files in file system, and allow development mode.
-rw-r--r--lib/http/http_test.go7
-rw-r--r--lib/http/server.go20
-rw-r--r--lib/http/serveroptions.go40
3 files changed, 57 insertions, 10 deletions
diff --git a/lib/http/http_test.go b/lib/http/http_test.go
index 9976bc96..9341cb50 100644
--- a/lib/http/http_test.go
+++ b/lib/http/http_test.go
@@ -49,14 +49,15 @@ var (
func TestMain(m *testing.M) {
var err error
- conn := &http.Server{
- Addr: "127.0.0.1:8080",
+ opts := &ServerOptions{
+ Address: "127.0.0.1:8080",
+ Root: "./testdata",
}
// Testing handleFS with large size.
libmemfs.MaxFileSize = 30
- testServer, err = NewServer("testdata", conn)
+ testServer, err = NewServer(opts)
if err != nil {
log.Fatal(err)
}
diff --git a/lib/http/server.go b/lib/http/server.go
index f7a605df..b8c2a0cc 100644
--- a/lib/http/server.go
+++ b/lib/http/server.go
@@ -36,7 +36,7 @@ type Server struct {
// NewServer create and initialize new HTTP server that serve root directory
// with custom connection.
//
-func NewServer(root string, conn *http.Server) (srv *Server, e error) {
+func NewServer(opts *ServerOptions) (srv *Server, e error) {
srv = &Server{
regDelete: make(map[string]*Endpoint),
regGet: make(map[string]*Endpoint),
@@ -45,25 +45,31 @@ func NewServer(root string, conn *http.Server) (srv *Server, e error) {
regPut: make(map[string]*Endpoint),
}
- if conn == nil {
+ if len(opts.Address) == 0 {
+ opts.Address = ":80"
+ }
+
+ if opts.Conn == nil {
srv.conn = &http.Server{
- Addr: ":80",
+ Addr: opts.Address,
Handler: srv,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
MaxHeaderBytes: 1 << 20,
}
} else {
- conn.Handler = srv
- srv.conn = conn
+ opts.Conn.Handler = srv
+ srv.conn = opts.Conn
}
- srv.mfs, e = memfs.New(nil, nil)
+ memfs.Development = opts.Development
+
+ srv.mfs, e = memfs.New(opts.Includes, opts.Excludes)
if e != nil {
return nil, e
}
- e = srv.mfs.Mount(root)
+ e = srv.mfs.Mount(opts.Root)
if e != nil {
return nil, e
}
diff --git a/lib/http/serveroptions.go b/lib/http/serveroptions.go
new file mode 100644
index 00000000..4e071563
--- /dev/null
+++ b/lib/http/serveroptions.go
@@ -0,0 +1,40 @@
+// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package http
+
+import (
+ "net/http"
+)
+
+//
+// ServerOptions define an options to initialize HTTP server.
+//
+type ServerOptions struct {
+ // Root contains path to file system to be served.
+ // This field is required.
+ Root string
+
+ // Address define listen address, using ip:port format.
+ // This field is optional, default to ":80".
+ Address string
+
+ // Conn contains custom HTTP server connection.
+ // 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
+
+ // Development if its true, the Root file system is served by reading
+ // the content directly instead of using memory file system.
+ Development bool
+}