diff options
| author | Shulhan <ms@kilabit.info> | 2021-04-03 04:05:01 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-04-03 04:40:27 +0700 |
| commit | 2c1ec60015ed4a29134be4d8ddd1c71d38bf52fa (patch) | |
| tree | 37f30fa6cbe8f3287597808153aa78500b928e7d | |
| parent | 298eb4dff25b4e97b66d49f005c896a88b0df5c6 (diff) | |
| download | ciigo-2c1ec60015ed4a29134be4d8ddd1c71d38bf52fa.tar.xz | |
all: change the Serve signature to ServeOptions
Previously, we pass four parameters to Serve function: the instance
to memfs.MemFS, the root directory, the address to listen, and
path to HTML template.
In case we need to add new parameter in the future, the Serve function
signature will changes and this is not good for consumer of API.
This commit changes the Serve function parameters to ServeOptions
so we can add optional parameter in the future without changes to its
signature.
| -rw-r--r-- | ciigo.go | 24 | ||||
| -rw-r--r-- | cmd/ciigo-example/main.go | 11 | ||||
| -rw-r--r-- | cmd/ciigo/main.go | 9 | ||||
| -rw-r--r-- | serve_options.go | 33 | ||||
| -rw-r--r-- | server.go | 14 |
5 files changed, 71 insertions, 20 deletions
@@ -21,8 +21,6 @@ import ( ) const ( - defAddress = ":8080" - defDir = "." extAsciidoc = ".adoc" internalTemplatePath = "_internal/.template" ) @@ -127,20 +125,24 @@ func Generate(opts *GenerateOptions) (err error) { // Serve the content at directory "dir" using HTTP server at specific // "address". // -func Serve(mfs *memfs.MemFS, dir, address, htmlTemplate string) (err error) { - if len(dir) == 0 { - dir = defDir - } - if len(address) == 0 { - address = defAddress +func Serve(opts *ServeOptions) (err error) { + var ( + logp = "Serve" + srv *server + ) + + if opts == nil { + opts = &ServeOptions{} } - srv, err := newServer(mfs, dir, address, htmlTemplate) + opts.init() + + srv, err = newServer(opts) if err != nil { - return fmt.Errorf("Serve: %w", err) + return fmt.Errorf("%s: %w", logp, err) } err = srv.start() if err != nil { - return fmt.Errorf("Serve: %w", err) + return fmt.Errorf("%s: %w", logp, err) } return nil } diff --git a/cmd/ciigo-example/main.go b/cmd/ciigo-example/main.go index 3ad8a93..3975a75 100644 --- a/cmd/ciigo-example/main.go +++ b/cmd/ciigo-example/main.go @@ -19,7 +19,16 @@ import ( var ciigoFS *memfs.MemFS func main() { - err := ciigo.Serve(ciigoFS, "_example", ":8080", "_example/html.tmpl") + opts := &ciigo.ServeOptions{ + ConvertOptions: ciigo.ConvertOptions{ + Root: "_example", + HtmlTemplate: "_example/html.tmpl", + }, + Mfs: ciigoFS, + Address: ":8080", + } + + err := ciigo.Serve(opts) if err != nil { log.Fatal(err) } diff --git a/cmd/ciigo/main.go b/cmd/ciigo/main.go index f0c2e65..07f6164 100644 --- a/cmd/ciigo/main.go +++ b/cmd/ciigo/main.go @@ -92,7 +92,14 @@ func main() { case "serve": debug.Value = 1 - err = ciigo.Serve(nil, dir, *address, *htmlTemplate) + opts := ciigo.ServeOptions{ + ConvertOptions: ciigo.ConvertOptions{ + Root: dir, + HtmlTemplate: *htmlTemplate, + }, + Address: *address, + } + err = ciigo.Serve(&opts) default: usage() diff --git a/serve_options.go b/serve_options.go new file mode 100644 index 0000000..953bb1f --- /dev/null +++ b/serve_options.go @@ -0,0 +1,33 @@ +// Copyright 2021, 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 ciigo + +import "github.com/shuLhan/share/lib/memfs" + +const ( + defAddress = ":8080" +) + +// +// ServeOptions contains the options to use on Serve function. +// +type ServeOptions struct { + ConvertOptions + + // Mfs contains pointer to variable generated from Generate. + // This option is used to use embedded files for serving on HTTP. + Mfs *memfs.MemFS + + // Address to listen and serve for HTTP request. + Address string +} + +func (opts *ServeOptions) init() { + opts.ConvertOptions.init() + + if len(opts.Address) == 0 { + opts.Address = defAddress + } +} @@ -32,18 +32,18 @@ type server struct { // The htmlTemplate parameter is optional, if not set its default to // embedded HTML template. // -func newServer(mfs *memfs.MemFS, root, address, htmlTemplate string) (srv *server, err error) { +func newServer(opts *ServeOptions) (srv *server, err error) { logp := "newServer" srv = &server{ opts: &libhttp.ServerOptions{ Options: memfs.Options{ - Root: root, + Root: opts.Root, Excludes: defExcludes, Development: debug.Value > 0, }, - Memfs: mfs, - Address: address, + Memfs: opts.Mfs, + Address: opts.Address, }, } @@ -65,18 +65,18 @@ func newServer(mfs *memfs.MemFS, root, address, htmlTemplate string) (srv *serve return nil, fmt.Errorf("%s: %w", logp, err) } - srv.htmlg, err = newHTMLGenerator(mfs, htmlTemplate, srv.opts.Development) + srv.htmlg, err = newHTMLGenerator(opts.Mfs, opts.HtmlTemplate, srv.opts.Development) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } if srv.opts.Development { - srv.watcher, err = newWatcher(srv.htmlg, root) + srv.watcher, err = newWatcher(srv.htmlg, opts.Root) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } - srv.watcher.fileMarkups, err = listFileMarkups(root) + srv.watcher.fileMarkups, err = listFileMarkups(opts.Root) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } |
