diff options
| author | Shulhan <ms@kilabit.info> | 2026-02-07 16:03:19 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-02-07 16:03:19 +0700 |
| commit | 3714f15bf29cfb79c3623784e391a7a4854f6815 (patch) | |
| tree | 09913bd02652a11dd1b894c0e2093f78f522548c | |
| parent | 759c9b8e542d2ce159228e35c6d4138fbaef25de (diff) | |
| download | ciigo-3714f15bf29cfb79c3623784e391a7a4854f6815.tar.xz | |
all: embed struct [lib/http.ServerOptions] directly to [ServeOptions]
At this point, all of the fields in the struct ServeOptions is the
same with [lib/http.ServerOptions] except IsDevelopment and
ConvertOptions.
For field IsDevelopment we keep in the struct.
For field ConvertOptions we remove it and let the caller pass it on
Serve function or InitHTTPServer method.
| -rw-r--r-- | CHANGELOG.adoc | 10 | ||||
| -rw-r--r-- | ciigo.go | 12 | ||||
| -rw-r--r-- | cmd/ciigo/main.go | 12 | ||||
| -rw-r--r-- | internal/cmd/ciigo-example/main.go | 17 | ||||
| -rw-r--r-- | serve_options.go | 36 | ||||
| -rw-r--r-- | server.go | 68 | ||||
| -rw-r--r-- | server_test.go | 16 |
7 files changed, 90 insertions, 81 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f4d639b..6b209dd 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -15,6 +15,16 @@ Legend, [#v0_16_0] == ciigo v0.16.0 (2026-xx-xx) +**🪵 all: embed struct [lib/http.ServerOptions] directly to [ServeOptions]** + +At this point, all of the fields in the struct ServeOptions is the +same with [lib/http.ServerOptions] except IsDevelopment and +ConvertOptions. + +For field IsDevelopment we keep in the struct. +For field ConvertOptions we remove it and let the caller pass it on +Serve function or InitHTTPServer method. + **🌼 all: add field Listener to ServeOptions** The field Listener allow passing [net.Listener] instance for accepting @@ -50,7 +50,9 @@ type Ciigo struct { HTTPServer *libhttp.Server converter *Converter watcher *watcher - serveOpts ServeOptions + + convertOpts ConvertOptions + serveOpts ServeOptions } // Convert all markup files inside directory "dir" recursively into HTML @@ -77,11 +79,11 @@ func GoEmbed(opts EmbedOptions) (err error) { return ciigo.GoEmbed(opts) } -// Serve the content under directory "[ServeOptions].ConvertOptions.Root" +// Serve the content under directory "ConvertOptions.Root" // using HTTP server at specific "[ServeOptions].Address". -func Serve(opts ServeOptions) (err error) { +func Serve(opts ServeOptions, convertOpts ConvertOptions) (err error) { var ciigo = &Ciigo{} - err = ciigo.InitHTTPServer(opts) + err = ciigo.InitHTTPServer(opts, convertOpts) if err != nil { return err } @@ -254,7 +256,7 @@ func (ciigo *Ciigo) Convert(opts ConvertOptions) (err error) { return fmt.Errorf(`%s: %w`, logp, err) } - ciigo.serveOpts.ConvertOptions = opts + ciigo.convertOpts = opts ciigo.converter, err = NewConverter(opts.HTMLTemplate) if err != nil { diff --git a/cmd/ciigo/main.go b/cmd/ciigo/main.go index 039ebbb..8dcf04b 100644 --- a/cmd/ciigo/main.go +++ b/cmd/ciigo/main.go @@ -13,6 +13,7 @@ import ( "strings" "git.sr.ht/~shulhan/ciigo" + libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" ) @@ -86,12 +87,13 @@ func main() { case cmdServe: var serveOpts = ciigo.ServeOptions{ - ConvertOptions: convertOpts, - Address: *address, - EnableIndexHTML: true, - IsDevelopment: true, + ServerOptions: libhttp.ServerOptions{ + Address: *address, + EnableIndexHTML: true, + }, + IsDevelopment: true, } - err = ciigo.Serve(serveOpts) + err = ciigo.Serve(serveOpts, convertOpts) case cmdVersion: fmt.Println(ciigo.Version) diff --git a/internal/cmd/ciigo-example/main.go b/internal/cmd/ciigo-example/main.go index e1b8c58..7cd8ef9 100644 --- a/internal/cmd/ciigo-example/main.go +++ b/internal/cmd/ciigo-example/main.go @@ -10,6 +10,7 @@ import ( "log" "strings" + libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" "git.sr.ht/~shulhan/ciigo" @@ -23,13 +24,15 @@ var ciigoFS *memfs.MemFS func main() { var ( - opts = ciigo.ServeOptions{ - ConvertOptions: ciigo.ConvertOptions{ - Root: `_example`, - HTMLTemplate: `_example/html.tmpl`, + convertOpts = ciigo.ConvertOptions{ + Root: `_example`, + HTMLTemplate: `_example/html.tmpl`, + } + serveOpts = ciigo.ServeOptions{ + ServerOptions: libhttp.ServerOptions{ + Memfs: ciigoFS, + Address: `127.0.0.1:8080`, }, - Mfs: ciigoFS, - Address: `127.0.0.1:8080`, IsDevelopment: true, } @@ -45,7 +48,7 @@ func main() { return } - err = ciigo.Serve(opts) + err = ciigo.Serve(serveOpts, convertOpts) if err != nil { log.Fatal(err) } diff --git a/serve_options.go b/serve_options.go index a8f2426..2725959 100644 --- a/serve_options.go +++ b/serve_options.go @@ -3,37 +3,16 @@ package ciigo -import ( - "net" - - "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" -) +import "git.sr.ht/~shulhan/pakakeh.go/lib/http" const ( defAddress = `:8080` ) -// ServeOptions contains the options to use on Serve function. +// ServeOptions define the options to use on Serve function. +// See [git.sr.ht/~shulhan/pakakeh.go/lib/http.ServerOptions] for more information. type ServeOptions struct { - // Listener define the network listener to be used for serving HTTP - // connection. - // The Listener can be activated using systemd socket. - Listener net.Listener - - // 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 - - ConvertOptions - - // If true, the serve command generate index.html automatically if its - // not exist in the directory. - // The index.html contains the list of files inside the requested - // path. - EnableIndexHTML bool + http.ServerOptions // IsDevelopment if set to true, it will serve the ConvertOptions.Root // directory directly and watch all asciidoc files for changes and @@ -42,13 +21,8 @@ type ServeOptions struct { IsDevelopment bool } -func (opts *ServeOptions) init() (err error) { - err = opts.ConvertOptions.init() - if err != nil { - return err - } +func (opts *ServeOptions) init() { if len(opts.Address) == 0 { opts.Address = defAddress } - return nil } @@ -19,46 +19,44 @@ import ( // InitHTTPServer create an HTTP server to serve HTML files in directory // defined in "[ConvertOptions].Root". // -// The address parameter is optional, if not set its default to ":8080". -// The htmlTemplate parameter is optional, if not set its default to -// embedded HTML template. -func (ciigo *Ciigo) InitHTTPServer(opts ServeOptions) (err error) { +// This method initialize the [Ciigo.HTTPServer] so one can register +// additional endpoints before running the HTTP server. +func (ciigo *Ciigo) InitHTTPServer(serveOpts ServeOptions, convertOpts ConvertOptions) (err error) { var logp = `InitHTTPServer` - err = opts.init() + err = convertOpts.init() if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } - if opts.Mfs == nil { - opts.IsDevelopment = true + serveOpts.init() + + if serveOpts.Memfs == nil { + serveOpts.IsDevelopment = true var mfsopts = &memfs.Options{ - Root: opts.Root, + Root: convertOpts.Root, Excludes: defExcludes, TryDirect: true, } - opts.Mfs, err = memfs.New(mfsopts) + serveOpts.Memfs, err = memfs.New(mfsopts) if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } } else { - opts.Mfs.Opts.TryDirect = opts.IsDevelopment + serveOpts.Memfs.Opts.TryDirect = serveOpts.IsDevelopment } if staticfs != nil { - staticfs.Opts.TryDirect = opts.IsDevelopment + staticfs.Opts.TryDirect = serveOpts.IsDevelopment } - ciigo.serveOpts = opts - - var httpdOpts = libhttp.ServerOptions{ - Listener: opts.Listener, - Memfs: opts.Mfs, - Address: opts.Address, - EnableIndexHTML: opts.EnableIndexHTML, - HandleFS: ciigo.onGet, + if serveOpts.HandleFS == nil { + serveOpts.HandleFS = ciigo.onGet + } else { + userHandle := serveOpts.HandleFS + serveOpts.HandleFS = ciigo.userHandleFS(userHandle) } - ciigo.HTTPServer, err = libhttp.NewServer(httpdOpts) + ciigo.HTTPServer, err = libhttp.NewServer(serveOpts.ServerOptions) if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } @@ -78,8 +76,8 @@ func (ciigo *Ciigo) InitHTTPServer(opts ServeOptions) (err error) { var pathHTMLTemplate string - if opts.IsDevelopment { - pathHTMLTemplate = opts.HTMLTemplate + if serveOpts.IsDevelopment { + pathHTMLTemplate = convertOpts.HTMLTemplate } ciigo.converter, err = NewConverter(pathHTMLTemplate) @@ -87,10 +85,10 @@ func (ciigo *Ciigo) InitHTTPServer(opts ServeOptions) (err error) { return fmt.Errorf(`%s: %w`, logp, err) } - if !opts.IsDevelopment { + if !serveOpts.IsDevelopment { var tmplNode *memfs.Node - tmplNode, _ = opts.Mfs.Get(internalTemplatePath) + tmplNode, _ = serveOpts.Memfs.Get(internalTemplatePath) if tmplNode != nil { ciigo.converter.tmpl, err = ciigo.converter.tmpl.Parse(string(tmplNode.Content)) if err != nil { @@ -99,8 +97,8 @@ func (ciigo *Ciigo) InitHTTPServer(opts ServeOptions) (err error) { } } - if opts.IsDevelopment { - ciigo.watcher, err = newWatcher(ciigo.converter, opts.ConvertOptions) + if serveOpts.IsDevelopment { + ciigo.watcher, err = newWatcher(ciigo.converter, convertOpts) if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } @@ -108,9 +106,25 @@ func (ciigo *Ciigo) InitHTTPServer(opts ServeOptions) (err error) { ciigo.converter.convertFileMarkups(ciigo.watcher.fileMarkups, false) } + ciigo.serveOpts = serveOpts + ciigo.convertOpts = convertOpts + return nil } +// userHandleFS wrap the user HandleFS with internal onGet. +func (ciigo *Ciigo) userHandleFS(userHandle libhttp.FSHandler) libhttp.FSHandler { + return func(node *memfs.Node, res http.ResponseWriter, req *http.Request) ( + out *memfs.Node, statusCode int, + ) { + out, statusCode = userHandle(node, res, req) + if statusCode != 0 { + return out, statusCode + } + return ciigo.onGet(node, res, req) + } +} + // Serve start the HTTP web server. func (ciigo *Ciigo) Serve() (err error) { var logp = `Serve` @@ -231,7 +245,7 @@ func (ciigo *Ciigo) onGet( return node, 0 } } - out, err = ciigo.serveOpts.Mfs.Get(file) + out, err = ciigo.serveOpts.Memfs.Get(file) if err != nil { log.Printf(`%s: failed to get %q: %s`, logp, file, err) return node, 0 diff --git a/server_test.go b/server_test.go index fab2e4f..06b3edf 100644 --- a/server_test.go +++ b/server_test.go @@ -11,6 +11,7 @@ import ( "testing" "git.sr.ht/~shulhan/ciigo" + libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" "git.sr.ht/~shulhan/pakakeh.go/lib/test" "git.sr.ht/~shulhan/pakakeh.go/lib/test/httptest" ) @@ -37,14 +38,17 @@ func TestCiigoOnGet(t *testing.T) { var cigo = ciigo.Ciigo{} - var serveOpts = ciigo.ServeOptions{ - Address: `127.0.0.1:11083`, - ConvertOptions: ciigo.ConvertOptions{ - Root: dirRoot, - HTMLTemplate: `testdata/onGet_template.gohtml`, + serveOpts := ciigo.ServeOptions{ + ServerOptions: libhttp.ServerOptions{ + Address: `127.0.0.1:11083`, }, } - err = cigo.InitHTTPServer(serveOpts) + convertOpts := ciigo.ConvertOptions{ + Root: dirRoot, + HTMLTemplate: `testdata/onGet_template.gohtml`, + } + + err = cigo.InitHTTPServer(serveOpts, convertOpts) if err != nil { t.Fatal(err) } |
