From 29343294a596fd74f6f9cb917c8aac74ccb78094 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 8 Mar 2024 02:52:16 +0700 Subject: lib/http: refactoring NewServer and NewClient The NewServer and NewClient now accept non-pointer options, so the caller unable to modify the options once the server or client has been created. --- api/slack/webhook_client.go | 2 +- api/telegram/bot/bot.go | 4 ++-- cmd/httpdfs/main.go | 2 +- lib/http/client.go | 6 +++--- lib/http/client_test.go | 2 +- lib/http/endpoint_example_test.go | 22 ++++++++++++++-------- lib/http/endpoint_response_example_test.go | 19 +++++++++++++------ lib/http/http_test.go | 29 ++++++++++++++--------------- lib/http/server.go | 16 ++++++++-------- lib/http/server_example_test.go | 13 +++++++------ lib/http/server_test.go | 14 +++++--------- lib/http/sse_endpoint_test.go | 7 +++---- lib/http/sseclient/sseclient_test.go | 2 +- lib/xmlrpc/client.go | 2 +- 14 files changed, 74 insertions(+), 66 deletions(-) diff --git a/api/slack/webhook_client.go b/api/slack/webhook_client.go index 4e7683db..5a749779 100644 --- a/api/slack/webhook_client.go +++ b/api/slack/webhook_client.go @@ -31,7 +31,7 @@ func NewWebhookClient(webhookURL, user, channel string) (wcl *WebhookClient, err return nil, fmt.Errorf("NewWebhookClient: %w", err) } - clientOpts := &libhttp.ClientOptions{ + var clientOpts = libhttp.ClientOptions{ ServerURL: fmt.Sprintf("%s://%s", wurl.Scheme, wurl.Host), } wcl = &WebhookClient{ diff --git a/api/telegram/bot/bot.go b/api/telegram/bot/bot.go index c87546b3..2ac3f0a0 100644 --- a/api/telegram/bot/bot.go +++ b/api/telegram/bot/bot.go @@ -110,7 +110,7 @@ func New(opts Options) (bot *Bot, err error) { return nil, fmt.Errorf("bot.New: %w", err) } - clientOpts := &http.ClientOptions{ + var clientOpts = http.ClientOptions{ ServerURL: defURL + opts.Token + `/`, } bot = &Bot{ @@ -365,7 +365,7 @@ func (bot *Bot) startWebhook() (err error) { // createServer start the HTTP server for receiving Update. func (bot *Bot) createServer() (err error) { - serverOpts := &http.ServerOptions{ + var serverOpts = http.ServerOptions{ Address: bot.opts.Webhook.ListenAddress, } diff --git a/cmd/httpdfs/main.go b/cmd/httpdfs/main.go index ed62c07f..41549ddd 100644 --- a/cmd/httpdfs/main.go +++ b/cmd/httpdfs/main.go @@ -90,7 +90,7 @@ func main() { httpd *libhttp.Server ) - httpd, err = libhttp.NewServer(&serverOpts) + httpd, err = libhttp.NewServer(serverOpts) if err != nil { log.Fatalf(`%s: %s`, cmdName, err) } diff --git a/lib/http/client.go b/lib/http/client.go index eeb26e44..78f3d65a 100644 --- a/lib/http/client.go +++ b/lib/http/client.go @@ -40,9 +40,9 @@ type Client struct { flateReader io.ReadCloser gzipReader *gzip.Reader - opts *ClientOptions - *http.Client + + opts ClientOptions } // NewClient create and initialize new [Client]. @@ -50,7 +50,7 @@ type Client struct { // The client will have [net.Dialer.KeepAlive] set to 30 seconds, with one // [http.Transport.MaxIdleConns], and 90 seconds // [http.Transport.IdleConnTimeout]. -func NewClient(opts *ClientOptions) (client *Client) { +func NewClient(opts ClientOptions) (client *Client) { opts.init() httpTransport := &http.Transport{ diff --git a/lib/http/client_test.go b/lib/http/client_test.go index 56ff4333..021d309e 100644 --- a/lib/http/client_test.go +++ b/lib/http/client_test.go @@ -18,7 +18,7 @@ func TestClient_Download(t *testing.T) { clientOpts = ClientOptions{ ServerURL: `http://` + testServer.Options.Address, } - client = NewClient(&clientOpts) + client = NewClient(clientOpts) out bytes.Buffer err error diff --git a/lib/http/endpoint_example_test.go b/lib/http/endpoint_example_test.go index 3549f488..15b667d3 100644 --- a/lib/http/endpoint_example_test.go +++ b/lib/http/endpoint_example_test.go @@ -16,10 +16,14 @@ import ( ) func ExampleEndpoint_errorHandler() { - serverOpts := &ServerOptions{ - Address: "127.0.0.1:8123", - } - server, _ := NewServer(serverOpts) + var ( + serverOpts = ServerOptions{ + Address: `127.0.0.1:8123`, + } + server *Server + ) + + server, _ = NewServer(serverOpts) var endpointError = Endpoint{ Method: RequestMethodGet, @@ -53,10 +57,12 @@ func ExampleEndpoint_errorHandler() { }() time.Sleep(1 * time.Second) - clientOpts := &ClientOptions{ - ServerURL: `http://` + serverOpts.Address, - } - client := NewClient(clientOpts) + var ( + clientOpts = ClientOptions{ + ServerURL: `http://` + serverOpts.Address, + } + client = NewClient(clientOpts) + ) params := url.Values{} params.Set("error", "400:error with status code") diff --git a/lib/http/endpoint_response_example_test.go b/lib/http/endpoint_response_example_test.go index 40e9d6d7..2e78c66f 100644 --- a/lib/http/endpoint_response_example_test.go +++ b/lib/http/endpoint_response_example_test.go @@ -18,7 +18,12 @@ func ExampleEndpointResponse() { ID string } - server, err := NewServer(&ServerOptions{ + var ( + server *Server + err error + ) + + server, err = NewServer(ServerOptions{ Address: "127.0.0.1:7016", }) if err != nil { @@ -67,11 +72,13 @@ func ExampleEndpointResponse() { }() time.Sleep(1 * time.Second) - clientOpts := &ClientOptions{ - ServerURL: `http://127.0.0.1:7016`, - } - cl := NewClient(clientOpts) - params := url.Values{} + var ( + clientOpts = ClientOptions{ + ServerURL: `http://127.0.0.1:7016`, + } + cl = NewClient(clientOpts) + params = url.Values{} + ) // Test call endpoint without "id" parameter. _, resBody, err := cl.Get("/", nil, params) //nolint: bodyclose diff --git a/lib/http/http_test.go b/lib/http/http_test.go index c26746e3..c6872fe4 100644 --- a/lib/http/http_test.go +++ b/lib/http/http_test.go @@ -52,23 +52,22 @@ var ( func TestMain(m *testing.M) { var ( - serverAddress = "127.0.0.1:14832" - err error - ) - - opts := &ServerOptions{ - Memfs: &memfs.MemFS{ - Opts: &memfs.Options{ - Root: "./testdata", - MaxFileSize: 30, - TryDirect: true, + opts = ServerOptions{ + Memfs: &memfs.MemFS{ + Opts: &memfs.Options{ + Root: `./testdata`, + MaxFileSize: 30, + TryDirect: true, + }, }, - }, - HandleFS: handleFS, - Address: serverAddress, - } + HandleFS: handleFS, + Address: `127.0.0.1:14832`, + } + + err error + ) - testServerURL = `http://` + serverAddress + testServerURL = `http://` + opts.Address testServer, err = NewServer(opts) if err != nil { diff --git a/lib/http/server.go b/lib/http/server.go index d2e69ecc..b47ddba3 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -33,24 +33,24 @@ const ( type Server struct { *http.Server - // Options for server, set by calling NewServer. - // This field is exported only for reference, for example logging in - // the Options when server started. - // Modifying the value of Options after server has been started may - // cause undefined effects. - Options *ServerOptions - evals []Evaluator routeDeletes []*route routeGets []*route routePatches []*route routePosts []*route routePuts []*route + + // Options for server, set by calling NewServer. + // This field is exported only for reference, for example logging in + // the Options when server started. + // Modifying the value of Options after server has been started may + // cause undefined effects. + Options ServerOptions } // NewServer create and initialize new HTTP server that serve root directory // with custom connection. -func NewServer(opts *ServerOptions) (srv *Server, err error) { +func NewServer(opts ServerOptions) (srv *Server, err error) { opts.init() srv = &Server{ diff --git a/lib/http/server_example_test.go b/lib/http/server_example_test.go index 74a60f2a..460a78fd 100644 --- a/lib/http/server_example_test.go +++ b/lib/http/server_example_test.go @@ -21,8 +21,7 @@ func ExampleServer_customHTTPStatusCode() { exp = CustomResponse{ Status: http.StatusBadRequest, } - - opts = &ServerOptions{ + opts = ServerOptions{ Address: "127.0.0.1:8123", } @@ -67,10 +66,12 @@ func ExampleServer_customHTTPStatusCode() { // Wait for the server fully started. time.Sleep(1 * time.Second) - clientOpts := &ClientOptions{ - ServerURL: `http://127.0.0.1:8123`, - } - client := NewClient(clientOpts) + var ( + clientOpts = ClientOptions{ + ServerURL: `http://127.0.0.1:8123`, + } + client = NewClient(clientOpts) + ) httpRes, resBody, err := client.PostJSON(epCustom.Path, nil, nil) //nolint: bodyclose if err != nil { diff --git a/lib/http/server_test.go b/lib/http/server_test.go index f18b27c6..95995de5 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -950,7 +950,7 @@ func TestServer_Options_HandleFS(t *testing.T) { func TestServer_handleRange(t *testing.T) { var ( - clOpts = &ClientOptions{ + clOpts = ClientOptions{ ServerURL: testServerURL, } cl = NewClient(clOpts) @@ -1026,7 +1026,7 @@ func TestServer_handleRange(t *testing.T) { func TestServer_handleRange_HEAD(t *testing.T) { var ( - clOpts = &ClientOptions{ + clOpts = ClientOptions{ ServerURL: testServerURL, } cl = NewClient(clOpts) @@ -1129,15 +1129,11 @@ func TestServerHandleRangeBig(t *testing.T) { } var ( - clOpts = &ClientOptions{ + clOpts = ClientOptions{ ServerURL: `http://` + serverAddress, } - cl *Client - ) - - cl = NewClient(clOpts) + cl = NewClient(clOpts) - var ( tag = `HEAD /big` skipHeaders = []string{HeaderDate} @@ -1225,7 +1221,7 @@ func runServerFS(t *testing.T, address, dir string) (srv *Server) { nodeBig.SetModTime(pathBigModTime) var ( - srvOpts = &ServerOptions{ + srvOpts = ServerOptions{ Memfs: mfs, Address: address, } diff --git a/lib/http/sse_endpoint_test.go b/lib/http/sse_endpoint_test.go index 2a9e28f2..ebc2036a 100644 --- a/lib/http/sse_endpoint_test.go +++ b/lib/http/sse_endpoint_test.go @@ -11,11 +11,10 @@ import ( ) func TestSSEEndpoint(t *testing.T) { - var opts = &ServerOptions{ - Address: `127.0.0.1:24168`, - } - var ( + opts = ServerOptions{ + Address: `127.0.0.1:24168`, + } httpd *Server err error ) diff --git a/lib/http/sseclient/sseclient_test.go b/lib/http/sseclient/sseclient_test.go index d1ed4e1f..116ddbb5 100644 --- a/lib/http/sseclient/sseclient_test.go +++ b/lib/http/sseclient/sseclient_test.go @@ -423,7 +423,7 @@ func testRunSSEServer(t *testing.T, cb libhttp.SSECallback) (srv *libhttp.Server var address = testGenerateAddress() var ( - serverOpts = &libhttp.ServerOptions{ + serverOpts = libhttp.ServerOptions{ Address: address, } ) diff --git a/lib/xmlrpc/client.go b/lib/xmlrpc/client.go index 47af5c44..e01e09ce 100644 --- a/lib/xmlrpc/client.go +++ b/lib/xmlrpc/client.go @@ -42,7 +42,7 @@ func NewClient(url *url.URL, timeout time.Duration) (client *Client, err error) url: url, timeout: timeout, } - clientOpts := &libhttp.ClientOptions{ + var clientOpts = libhttp.ClientOptions{ Timeout: timeout, } -- cgit v1.3