From 4e35c509a41cecb207bf6f52e7c9a529fa9f71fb Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 8 Mar 2024 02:19:18 +0700 Subject: lib/http: rename files for consistency If the type is in CamelCase the file should be using snake_case. --- lib/http/example_server_test.go | 86 ----------------------------------------- lib/http/request_method.go | 50 ++++++++++++++++++++++++ lib/http/request_method_test.go | 42 ++++++++++++++++++++ lib/http/request_type.go | 37 ++++++++++++++++++ lib/http/request_type_test.go | 28 ++++++++++++++ lib/http/requestmethod.go | 50 ------------------------ lib/http/requestmethod_test.go | 42 -------------------- lib/http/requesttype.go | 37 ------------------ lib/http/requesttype_test.go | 28 -------------- lib/http/response_type.go | 39 +++++++++++++++++++ lib/http/response_type_test.go | 35 +++++++++++++++++ lib/http/responsetype.go | 39 ------------------- lib/http/responsetype_test.go | 35 ----------------- lib/http/server_example_test.go | 86 +++++++++++++++++++++++++++++++++++++++++ lib/http/server_options.go | 74 +++++++++++++++++++++++++++++++++++ lib/http/serveroptions.go | 74 ----------------------------------- 16 files changed, 391 insertions(+), 391 deletions(-) delete mode 100644 lib/http/example_server_test.go create mode 100644 lib/http/request_method.go create mode 100644 lib/http/request_method_test.go create mode 100644 lib/http/request_type.go create mode 100644 lib/http/request_type_test.go delete mode 100644 lib/http/requestmethod.go delete mode 100644 lib/http/requestmethod_test.go delete mode 100644 lib/http/requesttype.go delete mode 100644 lib/http/requesttype_test.go create mode 100644 lib/http/response_type.go create mode 100644 lib/http/response_type_test.go delete mode 100644 lib/http/responsetype.go delete mode 100644 lib/http/responsetype_test.go create mode 100644 lib/http/server_example_test.go create mode 100644 lib/http/server_options.go delete mode 100644 lib/http/serveroptions.go diff --git a/lib/http/example_server_test.go b/lib/http/example_server_test.go deleted file mode 100644 index 74a60f2a..00000000 --- a/lib/http/example_server_test.go +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2020, Shulhan . 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 ( - "encoding/json" - "fmt" - "log" - "net/http" - "time" -) - -func ExampleServer_customHTTPStatusCode() { - type CustomResponse struct { - Status int `json:"status"` - } - - var ( - exp = CustomResponse{ - Status: http.StatusBadRequest, - } - - opts = &ServerOptions{ - Address: "127.0.0.1:8123", - } - - srv *Server - err error - ) - - srv, err = NewServer(opts) - if err != nil { - log.Fatal(err) - } - - go func() { - err = srv.Start() - if err != nil { - log.Println(err) - } - }() - - defer func() { - _ = srv.Stop(5 * time.Second) - }() - - epCustom := &Endpoint{ - Path: "/error/custom", - RequestType: RequestTypeJSON, - ResponseType: ResponseTypeJSON, - Call: func(epr *EndpointRequest) ( - resbody []byte, err error, - ) { - epr.HTTPWriter.WriteHeader(exp.Status) - return json.Marshal(exp) - }, - } - - err = srv.registerPost(epCustom) - if err != nil { - log.Println(err) - return - } - - // Wait for the server fully started. - time.Sleep(1 * time.Second) - - 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 { - log.Println(err) - return - } - - fmt.Printf("%d\n", httpRes.StatusCode) - fmt.Printf("%s\n", resBody) - // Output: - // 400 - // {"status":400} -} diff --git a/lib/http/request_method.go b/lib/http/request_method.go new file mode 100644 index 00000000..120fd55e --- /dev/null +++ b/lib/http/request_method.go @@ -0,0 +1,50 @@ +// Copyright 2018, Shulhan . 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" +) + +// RequestMethod define type of HTTP method. +type RequestMethod int + +// List of known HTTP methods. +const ( + RequestMethodGet RequestMethod = iota + RequestMethodConnect + RequestMethodDelete + RequestMethodHead + RequestMethodOptions + RequestMethodPatch + RequestMethodPost + RequestMethodPut + RequestMethodTrace +) + +// String return the string representation of request method. +func (rm RequestMethod) String() string { + switch rm { + case RequestMethodGet: + return http.MethodGet + case RequestMethodConnect: + return http.MethodConnect + case RequestMethodDelete: + return http.MethodDelete + case RequestMethodHead: + return http.MethodHead + case RequestMethodOptions: + return http.MethodOptions + case RequestMethodPatch: + return http.MethodPatch + case RequestMethodPost: + return http.MethodPost + case RequestMethodPut: + return http.MethodPut + case RequestMethodTrace: + return http.MethodTrace + } + return "" +} diff --git a/lib/http/request_method_test.go b/lib/http/request_method_test.go new file mode 100644 index 00000000..3723c6c4 --- /dev/null +++ b/lib/http/request_method_test.go @@ -0,0 +1,42 @@ +// Copyright 2021, Shulhan . 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 ( + "testing" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestRequestMethod_String(t *testing.T) { + cases := []struct { + exp string + m RequestMethod + }{ + {m: 0, exp: "GET"}, + {m: 1, exp: "CONNECT"}, + {m: 2, exp: "DELETE"}, + {m: 3, exp: "HEAD"}, + {m: 4, exp: "OPTIONS"}, + {m: 5, exp: "PATCH"}, + {m: 6, exp: "POST"}, + {m: 7, exp: "PUT"}, + {m: 8, exp: "TRACE"}, + {m: 9, exp: ""}, + {m: RequestMethodGet, exp: "GET"}, + {m: RequestMethodConnect, exp: "CONNECT"}, + {m: RequestMethodDelete, exp: "DELETE"}, + {m: RequestMethodHead, exp: "HEAD"}, + {m: RequestMethodOptions, exp: "OPTIONS"}, + {m: RequestMethodPatch, exp: "PATCH"}, + {m: RequestMethodPost, exp: "POST"}, + {m: RequestMethodPut, exp: "PUT"}, + {m: RequestMethodTrace, exp: "TRACE"}, + } + + for _, c := range cases { + test.Assert(t, "RequestMethod.String", c.exp, c.m.String()) + } +} diff --git a/lib/http/request_type.go b/lib/http/request_type.go new file mode 100644 index 00000000..db578e55 --- /dev/null +++ b/lib/http/request_type.go @@ -0,0 +1,37 @@ +// Copyright 2018, Shulhan . 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 + +// RequestType define type of HTTP request. +type RequestType int + +// List of valid request type. +const ( + RequestTypeNone RequestType = iota + RequestTypeQuery + RequestTypeForm + RequestTypeMultipartForm + RequestTypeJSON + RequestTypeXML +) + +// String return the string representation of request type as in +// "Content-Type" header. +// For RequestTypeNone or RequestTypeQuery it will return an empty string. +func (rt RequestType) String() string { + switch rt { + case RequestTypeNone, RequestTypeQuery: + return `` + case RequestTypeForm: + return ContentTypeForm + case RequestTypeMultipartForm: + return ContentTypeMultipartForm + case RequestTypeJSON: + return ContentTypeJSON + case RequestTypeXML: + return ContentTypeXML + } + return `` +} diff --git a/lib/http/request_type_test.go b/lib/http/request_type_test.go new file mode 100644 index 00000000..a593ff5f --- /dev/null +++ b/lib/http/request_type_test.go @@ -0,0 +1,28 @@ +// Copyright 2021, Shulhan . 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 ( + "testing" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestRequestType_String(t *testing.T) { + cases := []struct { + exp string + rt RequestType + }{ + {rt: 0, exp: ""}, + {rt: 1, exp: ""}, + {rt: 2, exp: ContentTypeForm}, + {rt: 3, exp: ContentTypeMultipartForm}, + {rt: 4, exp: ContentTypeJSON}, + } + + for _, c := range cases { + test.Assert(t, "RequestMethod.String", c.exp, c.rt.String()) + } +} diff --git a/lib/http/requestmethod.go b/lib/http/requestmethod.go deleted file mode 100644 index 120fd55e..00000000 --- a/lib/http/requestmethod.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2018, Shulhan . 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" -) - -// RequestMethod define type of HTTP method. -type RequestMethod int - -// List of known HTTP methods. -const ( - RequestMethodGet RequestMethod = iota - RequestMethodConnect - RequestMethodDelete - RequestMethodHead - RequestMethodOptions - RequestMethodPatch - RequestMethodPost - RequestMethodPut - RequestMethodTrace -) - -// String return the string representation of request method. -func (rm RequestMethod) String() string { - switch rm { - case RequestMethodGet: - return http.MethodGet - case RequestMethodConnect: - return http.MethodConnect - case RequestMethodDelete: - return http.MethodDelete - case RequestMethodHead: - return http.MethodHead - case RequestMethodOptions: - return http.MethodOptions - case RequestMethodPatch: - return http.MethodPatch - case RequestMethodPost: - return http.MethodPost - case RequestMethodPut: - return http.MethodPut - case RequestMethodTrace: - return http.MethodTrace - } - return "" -} diff --git a/lib/http/requestmethod_test.go b/lib/http/requestmethod_test.go deleted file mode 100644 index 3723c6c4..00000000 --- a/lib/http/requestmethod_test.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2021, Shulhan . 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 ( - "testing" - - "git.sr.ht/~shulhan/pakakeh.go/lib/test" -) - -func TestRequestMethod_String(t *testing.T) { - cases := []struct { - exp string - m RequestMethod - }{ - {m: 0, exp: "GET"}, - {m: 1, exp: "CONNECT"}, - {m: 2, exp: "DELETE"}, - {m: 3, exp: "HEAD"}, - {m: 4, exp: "OPTIONS"}, - {m: 5, exp: "PATCH"}, - {m: 6, exp: "POST"}, - {m: 7, exp: "PUT"}, - {m: 8, exp: "TRACE"}, - {m: 9, exp: ""}, - {m: RequestMethodGet, exp: "GET"}, - {m: RequestMethodConnect, exp: "CONNECT"}, - {m: RequestMethodDelete, exp: "DELETE"}, - {m: RequestMethodHead, exp: "HEAD"}, - {m: RequestMethodOptions, exp: "OPTIONS"}, - {m: RequestMethodPatch, exp: "PATCH"}, - {m: RequestMethodPost, exp: "POST"}, - {m: RequestMethodPut, exp: "PUT"}, - {m: RequestMethodTrace, exp: "TRACE"}, - } - - for _, c := range cases { - test.Assert(t, "RequestMethod.String", c.exp, c.m.String()) - } -} diff --git a/lib/http/requesttype.go b/lib/http/requesttype.go deleted file mode 100644 index db578e55..00000000 --- a/lib/http/requesttype.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018, Shulhan . 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 - -// RequestType define type of HTTP request. -type RequestType int - -// List of valid request type. -const ( - RequestTypeNone RequestType = iota - RequestTypeQuery - RequestTypeForm - RequestTypeMultipartForm - RequestTypeJSON - RequestTypeXML -) - -// String return the string representation of request type as in -// "Content-Type" header. -// For RequestTypeNone or RequestTypeQuery it will return an empty string. -func (rt RequestType) String() string { - switch rt { - case RequestTypeNone, RequestTypeQuery: - return `` - case RequestTypeForm: - return ContentTypeForm - case RequestTypeMultipartForm: - return ContentTypeMultipartForm - case RequestTypeJSON: - return ContentTypeJSON - case RequestTypeXML: - return ContentTypeXML - } - return `` -} diff --git a/lib/http/requesttype_test.go b/lib/http/requesttype_test.go deleted file mode 100644 index a593ff5f..00000000 --- a/lib/http/requesttype_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2021, Shulhan . 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 ( - "testing" - - "git.sr.ht/~shulhan/pakakeh.go/lib/test" -) - -func TestRequestType_String(t *testing.T) { - cases := []struct { - exp string - rt RequestType - }{ - {rt: 0, exp: ""}, - {rt: 1, exp: ""}, - {rt: 2, exp: ContentTypeForm}, - {rt: 3, exp: ContentTypeMultipartForm}, - {rt: 4, exp: ContentTypeJSON}, - } - - for _, c := range cases { - test.Assert(t, "RequestMethod.String", c.exp, c.rt.String()) - } -} diff --git a/lib/http/response_type.go b/lib/http/response_type.go new file mode 100644 index 00000000..75fb7146 --- /dev/null +++ b/lib/http/response_type.go @@ -0,0 +1,39 @@ +// Copyright 2018, Shulhan . 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 + +// ResponseType define type for HTTP response. +type ResponseType int + +// List of valid response type. +const ( + ResponseTypeNone ResponseType = iota + ResponseTypeBinary + ResponseTypeHTML + ResponseTypeJSON + ResponseTypePlain + ResponseTypeXML +) + +// String return the string representation of ResponseType as in +// "Content-Type" header. +// For ResponseTypeNone it will return an empty string. +func (restype ResponseType) String() string { + switch restype { + case ResponseTypeNone: + return `` + case ResponseTypeBinary: + return ContentTypeBinary + case ResponseTypeHTML: + return ContentTypeHTML + case ResponseTypeJSON: + return ContentTypeJSON + case ResponseTypePlain: + return ContentTypePlain + case ResponseTypeXML: + return ContentTypeXML + } + return `` +} diff --git a/lib/http/response_type_test.go b/lib/http/response_type_test.go new file mode 100644 index 00000000..f95726c2 --- /dev/null +++ b/lib/http/response_type_test.go @@ -0,0 +1,35 @@ +// Copyright 2021, Shulhan . 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 ( + "testing" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestResponseType_String(t *testing.T) { + cases := []struct { + exp string + restype ResponseType + }{ + {restype: 0, exp: ""}, + {restype: 1, exp: ContentTypeBinary}, + {restype: 2, exp: ContentTypeHTML}, + {restype: 3, exp: ContentTypeJSON}, + {restype: 4, exp: ContentTypePlain}, + {restype: 5, exp: ContentTypeXML}, + {restype: ResponseTypeNone, exp: ""}, + {restype: ResponseTypeBinary, exp: ContentTypeBinary}, + {restype: ResponseTypeHTML, exp: ContentTypeHTML}, + {restype: ResponseTypeJSON, exp: ContentTypeJSON}, + {restype: ResponseTypePlain, exp: ContentTypePlain}, + {restype: ResponseTypeXML, exp: ContentTypeXML}, + } + + for _, c := range cases { + test.Assert(t, "ResponseType.String", c.exp, c.restype.String()) + } +} diff --git a/lib/http/responsetype.go b/lib/http/responsetype.go deleted file mode 100644 index 75fb7146..00000000 --- a/lib/http/responsetype.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2018, Shulhan . 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 - -// ResponseType define type for HTTP response. -type ResponseType int - -// List of valid response type. -const ( - ResponseTypeNone ResponseType = iota - ResponseTypeBinary - ResponseTypeHTML - ResponseTypeJSON - ResponseTypePlain - ResponseTypeXML -) - -// String return the string representation of ResponseType as in -// "Content-Type" header. -// For ResponseTypeNone it will return an empty string. -func (restype ResponseType) String() string { - switch restype { - case ResponseTypeNone: - return `` - case ResponseTypeBinary: - return ContentTypeBinary - case ResponseTypeHTML: - return ContentTypeHTML - case ResponseTypeJSON: - return ContentTypeJSON - case ResponseTypePlain: - return ContentTypePlain - case ResponseTypeXML: - return ContentTypeXML - } - return `` -} diff --git a/lib/http/responsetype_test.go b/lib/http/responsetype_test.go deleted file mode 100644 index f95726c2..00000000 --- a/lib/http/responsetype_test.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2021, Shulhan . 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 ( - "testing" - - "git.sr.ht/~shulhan/pakakeh.go/lib/test" -) - -func TestResponseType_String(t *testing.T) { - cases := []struct { - exp string - restype ResponseType - }{ - {restype: 0, exp: ""}, - {restype: 1, exp: ContentTypeBinary}, - {restype: 2, exp: ContentTypeHTML}, - {restype: 3, exp: ContentTypeJSON}, - {restype: 4, exp: ContentTypePlain}, - {restype: 5, exp: ContentTypeXML}, - {restype: ResponseTypeNone, exp: ""}, - {restype: ResponseTypeBinary, exp: ContentTypeBinary}, - {restype: ResponseTypeHTML, exp: ContentTypeHTML}, - {restype: ResponseTypeJSON, exp: ContentTypeJSON}, - {restype: ResponseTypePlain, exp: ContentTypePlain}, - {restype: ResponseTypeXML, exp: ContentTypeXML}, - } - - for _, c := range cases { - test.Assert(t, "ResponseType.String", c.exp, c.restype.String()) - } -} diff --git a/lib/http/server_example_test.go b/lib/http/server_example_test.go new file mode 100644 index 00000000..74a60f2a --- /dev/null +++ b/lib/http/server_example_test.go @@ -0,0 +1,86 @@ +// Copyright 2020, Shulhan . 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 ( + "encoding/json" + "fmt" + "log" + "net/http" + "time" +) + +func ExampleServer_customHTTPStatusCode() { + type CustomResponse struct { + Status int `json:"status"` + } + + var ( + exp = CustomResponse{ + Status: http.StatusBadRequest, + } + + opts = &ServerOptions{ + Address: "127.0.0.1:8123", + } + + srv *Server + err error + ) + + srv, err = NewServer(opts) + if err != nil { + log.Fatal(err) + } + + go func() { + err = srv.Start() + if err != nil { + log.Println(err) + } + }() + + defer func() { + _ = srv.Stop(5 * time.Second) + }() + + epCustom := &Endpoint{ + Path: "/error/custom", + RequestType: RequestTypeJSON, + ResponseType: ResponseTypeJSON, + Call: func(epr *EndpointRequest) ( + resbody []byte, err error, + ) { + epr.HTTPWriter.WriteHeader(exp.Status) + return json.Marshal(exp) + }, + } + + err = srv.registerPost(epCustom) + if err != nil { + log.Println(err) + return + } + + // Wait for the server fully started. + time.Sleep(1 * time.Second) + + 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 { + log.Println(err) + return + } + + fmt.Printf("%d\n", httpRes.StatusCode) + fmt.Printf("%s\n", resBody) + // Output: + // 400 + // {"status":400} +} diff --git a/lib/http/server_options.go b/lib/http/server_options.go new file mode 100644 index 00000000..5ff15fb6 --- /dev/null +++ b/lib/http/server_options.go @@ -0,0 +1,74 @@ +// Copyright 2019, Shulhan . 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 ( + "io" + "log" + "net/http" + + "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" +) + +// ServerOptions define an options to initialize HTTP server. +type ServerOptions struct { + // Memfs contains the content of file systems to be served in memory. + // The MemFS instance to be served should be already embedded in Go + // file, generated using memfs.MemFS.GoEmbed(). + // Otherwise, it will try to read from file system directly. + // + // See https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/memfs#hdr-Go_embed + Memfs *memfs.MemFS + + // HandleFS inspect each GET request to Memfs. + // Some usage of this handler is to check for authorization on + // specific path, handling redirect, and so on. + // If nil it means all request are allowed. + // See FSHandler for more information. + HandleFS FSHandler + + // 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 + + // ErrorWriter define the writer where output from panic in handler + // will be written. Basically this will create new log.Logger and set + // the default Server.ErrorLog. + // This field is optional, but if its set it will be used only if Conn + // is not set by caller. + ErrorWriter io.Writer + + // The options for Cross-Origin Resource Sharing. + CORS CORSOptions + + // If true, server 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 +} + +func (opts *ServerOptions) init() { + if len(opts.Address) == 0 { + opts.Address = ":80" + } + + if opts.Conn == nil { + opts.Conn = &http.Server{ + ReadTimeout: defRWTimeout, + WriteTimeout: defRWTimeout, + MaxHeaderBytes: 1 << 20, + } + if opts.ErrorWriter != nil { + opts.Conn.ErrorLog = log.New(opts.ErrorWriter, "", 0) + } + } + + opts.CORS.init() +} diff --git a/lib/http/serveroptions.go b/lib/http/serveroptions.go deleted file mode 100644 index 5ff15fb6..00000000 --- a/lib/http/serveroptions.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2019, Shulhan . 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 ( - "io" - "log" - "net/http" - - "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" -) - -// ServerOptions define an options to initialize HTTP server. -type ServerOptions struct { - // Memfs contains the content of file systems to be served in memory. - // The MemFS instance to be served should be already embedded in Go - // file, generated using memfs.MemFS.GoEmbed(). - // Otherwise, it will try to read from file system directly. - // - // See https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/memfs#hdr-Go_embed - Memfs *memfs.MemFS - - // HandleFS inspect each GET request to Memfs. - // Some usage of this handler is to check for authorization on - // specific path, handling redirect, and so on. - // If nil it means all request are allowed. - // See FSHandler for more information. - HandleFS FSHandler - - // 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 - - // ErrorWriter define the writer where output from panic in handler - // will be written. Basically this will create new log.Logger and set - // the default Server.ErrorLog. - // This field is optional, but if its set it will be used only if Conn - // is not set by caller. - ErrorWriter io.Writer - - // The options for Cross-Origin Resource Sharing. - CORS CORSOptions - - // If true, server 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 -} - -func (opts *ServerOptions) init() { - if len(opts.Address) == 0 { - opts.Address = ":80" - } - - if opts.Conn == nil { - opts.Conn = &http.Server{ - ReadTimeout: defRWTimeout, - WriteTimeout: defRWTimeout, - MaxHeaderBytes: 1 << 20, - } - if opts.ErrorWriter != nil { - opts.Conn.ErrorLog = log.New(opts.ErrorWriter, "", 0) - } - } - - opts.CORS.init() -} -- cgit v1.3