diff options
| author | Shulhan <ms@kilabit.info> | 2026-02-12 00:17:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-02-12 01:29:15 +0700 |
| commit | ff5a7ecdb8aa0ccfe4bd4f71648e7254e470c930 (patch) | |
| tree | 20f64fff336b5881b3db03db2052b209fefd23c3 | |
| parent | 2686a70d0eeb14c0eba68f449658827e144e75a2 (diff) | |
| download | awwan-ff5a7ecdb8aa0ccfe4bd4f71648e7254e470c930.tar.xz | |
all: change Serve parameters to struct `ServeOptions`
In case we needs to add another parameter, which will do later, the
argument will be too long.
Using parameters is acceptable only for 2 to 3 arguments.
| -rw-r--r-- | CHANGELOG.adoc | 10 | ||||
| -rw-r--r-- | awwan.go | 9 | ||||
| -rw-r--r-- | cmd/awwan/main.go | 25 | ||||
| -rw-r--r-- | http_server.go | 20 | ||||
| -rw-r--r-- | http_server_test.go | 48 | ||||
| -rw-r--r-- | serve_options.go | 24 |
6 files changed, 81 insertions, 55 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 996c0c3..1a0cf80 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -15,14 +15,20 @@ Legend, * 💧: Chores -[#v0_13_2] -== awwan v0.13.2 (2026-xx-xx) +[#v0_14_0] +== awwan v0.14.0 (2026-xx-xx) **🌼 internal/cmd/www-awwan: add option to set shutdown idle duration** The `-shutdown-idle` option set the duration when server will stop accepting new connections and shutting down. +**🪵 all: change Serve parameters to struct `ServeOptions`** + +In case we needs to add another parameter, which will do later, the +argument will be too long. +Using parameters is acceptable only for 2 to 3 arguments. + [#v0_13_1] == awwan v0.13.1 (2026-02-09) @@ -7,7 +7,6 @@ import ( "context" "fmt" "log" - "net" "os" "path/filepath" "strconv" @@ -21,7 +20,7 @@ import ( ) // Version current version of this module (library and program). -var Version = `0.13.2` +var Version = `0.14.0` // osGetwd define the handler to get current working directory. // @@ -485,14 +484,14 @@ out: } // Serve start the web-user interface that serve awwan through HTTP. -func (aww *Awwan) Serve(listener net.Listener, address string, isDev bool) (err error) { +func (aww *Awwan) Serve(serveOpts ServeOptions) (err error) { var logp = `Serve` - if isDev { + if serveOpts.IsDevelopment { go internal.Watch() } - aww.httpd, err = newHTTPServer(aww, listener, address) + aww.httpd, err = newHTTPServer(aww, serveOpts) if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } diff --git a/cmd/awwan/main.go b/cmd/awwan/main.go index 5411515..da0429c 100644 --- a/cmd/awwan/main.go +++ b/cmd/awwan/main.go @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> // awwan is command line interface to configure and manage remote system // through SSH connection. @@ -125,11 +125,13 @@ Run the web-user interface using the current directory as workspace, } func main() { - var ( - logp = `awwan` - isDev = flag.Bool(`dev`, false, `run the "serve" command in development mode`) - serveAddress = flag.String(`address`, awwan.DefListenAddress, `HTTP server address to serve WUI.`) - ) + var logp = `awwan` + var serveOpts = awwan.ServeOptions{} + + flag.StringVar(&serveOpts.Address, `address`, awwan.DefListenAddress, + `HTTP server address to serve web user interface.`) + flag.BoolVar(&serveOpts.IsDevelopment, `dev`, false, + `Turn on development mode.`) flag.Parse() @@ -264,16 +266,15 @@ func main() { if len(listeners) > 1 { log.Fatal(`too many listeners received for activation`) } - var listener net.Listener if len(listeners) == 1 { - listener = listeners[0] - gotAddr := listener.Addr().String() - if gotAddr != *serveAddress { + serveOpts.Listener = listeners[0] + gotAddr := serveOpts.Listener.Addr().String() + if gotAddr != serveOpts.Address { log.Fatalf(`invalid Listener address, got %s, want %s`, - gotAddr, *serveAddress) + gotAddr, serveOpts.Address) } } - err = aww.Serve(listener, *serveAddress, *isDev) + err = aww.Serve(serveOpts) } if err != nil { log.Fatalf(`%s: %s`, logp, err) diff --git a/http_server.go b/http_server.go index e0935c5..cc88798 100644 --- a/http_server.go +++ b/http_server.go @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2021 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2021 M. Shulhan <ms@kilabit.info> package awwan @@ -11,7 +11,6 @@ import ( "fmt" "io/fs" "log" - "net" "net/http" "os" "path" @@ -41,9 +40,6 @@ const ( paramNameID = `id` ) -// DefListenAddress default HTTP server address to serve WUI. -const DefListenAddress = `127.0.0.1:17600` - // httpServer awwan HTTP server for HTTP API and web user interface feature. type httpServer struct { *libhttp.Server @@ -63,10 +59,8 @@ type httpServer struct { // newHTTPServer create and initialize HTTP server to serve awwan HTTP API // and web user interface. -func newHTTPServer(aww *Awwan, listener net.Listener, address string) (httpd *httpServer, err error) { - var ( - logp = `newHTTPServer` - ) +func newHTTPServer(aww *Awwan, serveOpts ServeOptions) (httpd *httpServer, err error) { + var logp = `newHTTPServer` httpd = &httpServer{ idExecRes: make(map[string]*ExecResponse), @@ -92,13 +86,9 @@ func newHTTPServer(aww *Awwan, listener net.Listener, address string) (httpd *ht return nil, fmt.Errorf(`%s: %w`, logp, err) } - var serverOpts = libhttp.ServerOptions{ - Listener: listener, - Memfs: internal.MemfsWui, - Address: address, - } + serveOpts.Memfs = internal.MemfsWui - httpd.Server, err = libhttp.NewServer(serverOpts) + httpd.Server, err = libhttp.NewServer(serveOpts.ServerOptions) if err != nil { return nil, fmt.Errorf(`%s: %w`, logp, err) } diff --git a/http_server_test.go b/http_server_test.go index d5cb270..3ad9bd8 100644 --- a/http_server_test.go +++ b/http_server_test.go @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2023 M. Shulhan <ms@kilabit.info> package awwan @@ -48,8 +48,9 @@ func TestHttpServer_Decrypt(t *testing.T) { } var httpd *httpServer + var serveOpts = ServeOptions{} - httpd, err = newHTTPServer(aww, nil, ``) + httpd, err = newHTTPServer(aww, serveOpts) if err != nil { t.Fatal(err) } @@ -132,8 +133,9 @@ func TestHttpServer_Encrypt(t *testing.T) { } var httpd *httpServer + var serveOpts = ServeOptions{} - httpd, err = newHTTPServer(aww, nil, ``) + httpd, err = newHTTPServer(aww, serveOpts) if err != nil { t.Fatal(err) } @@ -211,13 +213,14 @@ func TestHttpServer_Execute(t *testing.T) { t.Fatal(err) } - var ( - address = testGenerateServerAddress() - isDev = false - ) + var serveOpts = ServeOptions{ + ServerOptions: libhttp.ServerOptions{ + Address: testGenerateServerAddress(), + }, + } go func() { - err = aww.Serve(nil, address, isDev) + err = aww.Serve(serveOpts) if err != nil { log.Fatal(err) } @@ -226,14 +229,14 @@ func TestHttpServer_Execute(t *testing.T) { aww.Stop() }) - err = libnet.WaitAlive(`tcp`, address, 10*time.Second) + err = libnet.WaitAlive(`tcp`, serveOpts.Address, 10*time.Second) if err != nil { t.Fatal(err) } var ( clientOpts = libhttp.ClientOptions{ - ServerURL: `http://` + address, + ServerURL: `http://` + serveOpts.Address, } reqJSON = tdata.Input[`local:/local.aww:1-`] @@ -284,7 +287,8 @@ func TestHttpServer_Execute(t *testing.T) { // Tail the execution output. var ssec = sseclient.Client{ - Endpoint: fmt.Sprintf(`http://%s%s?id=%s`, address, pathAwwanAPIExecuteTail, execResp.ID), + Endpoint: fmt.Sprintf(`http://%s%s?id=%s`, serveOpts.Address, + pathAwwanAPIExecuteTail, execResp.ID), } err = ssec.Connect(nil) @@ -339,13 +343,13 @@ func TestHttpServer_ExecuteCancel(t *testing.T) { t.Fatal(err) } - var ( - address = testGenerateServerAddress() - isDev = false - ) - + var serveOpts = ServeOptions{ + ServerOptions: libhttp.ServerOptions{ + Address: testGenerateServerAddress(), + }, + } go func() { - err = aww.Serve(nil, address, isDev) + err = aww.Serve(serveOpts) if err != nil { log.Fatal(err) } @@ -354,14 +358,14 @@ func TestHttpServer_ExecuteCancel(t *testing.T) { aww.Stop() }) - err = libnet.WaitAlive(`tcp`, address, 10*time.Second) + err = libnet.WaitAlive(`tcp`, serveOpts.Address, 10*time.Second) if err != nil { t.Fatal(err) } var ( clientOpts = libhttp.ClientOptions{ - ServerURL: `http://` + address, + ServerURL: `http://` + serveOpts.Address, } reqJSON = tdata.Input[`local:/cancel.aww:1-`] @@ -412,7 +416,8 @@ func TestHttpServer_ExecuteCancel(t *testing.T) { // Tail the execution output. var ssec = sseclient.Client{ - Endpoint: fmt.Sprintf(`http://%s%s?id=%s`, address, pathAwwanAPIExecuteTail, execResp.ID), + Endpoint: fmt.Sprintf(`http://%s%s?id=%s`, serveOpts.Address, + pathAwwanAPIExecuteTail, execResp.ID), } err = ssec.Connect(nil) @@ -490,8 +495,9 @@ func TestHttpServer_FSGet(t *testing.T) { } var httpd *httpServer + var serveOpts = ServeOptions{} - httpd, err = newHTTPServer(aww, nil, ``) + httpd, err = newHTTPServer(aww, serveOpts) if err != nil { t.Fatal(err) } diff --git a/serve_options.go b/serve_options.go new file mode 100644 index 0000000..fdc2b8d --- /dev/null +++ b/serve_options.go @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + +package awwan + +import "git.sr.ht/~shulhan/pakakeh.go/lib/http" + +// DefListenAddress default HTTP server address to serve WUI. +const DefListenAddress = `127.0.0.1:17600` + +// 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 { + http.ServerOptions + + // IsDevelopment enable development mode. + IsDevelopment bool +} + +func (opts *ServeOptions) init() { + if len(opts.Address) == 0 { + opts.Address = DefListenAddress + } +} |
