diff --git a/service.go b/service.go index 0c16291..0fabe36 100644 --- a/service.go +++ b/service.go @@ -122,13 +122,11 @@ func (app *application) httpServe() (err error) { app.waitGroup.Go(func() { select { case errChan <- app.http.Serve(l): - log.Infof("http server closed") } }) select { case err = <-errChan: case <-timer.C: - log.Infof("http server started") } return } @@ -149,13 +147,11 @@ func (app *application) commandServe() (err error) { app.waitGroup.Go(func() { select { case errChan <- app.command.Serve(l): - log.Infof("command server closed") } }) select { case err = <-errChan: case <-timer.C: - log.Infof("command server started") } return } diff --git a/util/fetch/fetch.go b/util/fetch/fetch.go index a4d6233..b48bda3 100644 --- a/util/fetch/fetch.go +++ b/util/fetch/fetch.go @@ -37,6 +37,26 @@ var ( } ) +func encode(data any) (r io.Reader, contentType string, err error) { + var ( + buf []byte + ) + switch v := data.(type) { + case string: + r = strings.NewReader(v) + contentType = "x-www-form-urlencoded" + case []byte: + r = bytes.NewReader(v) + contentType = "x-www-form-urlencoded" + default: + if buf, err = json.Marshal(v); err == nil { + r = bytes.NewReader(buf) + contentType = "application/json" + } + } + return +} + func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) { var ( uri *url.URL @@ -69,7 +89,6 @@ func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Respon func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) { var ( - buf []byte uri *url.URL req *http.Request contentType string @@ -90,20 +109,8 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo uri.RawQuery = qs.Encode() } if opts.Data != nil { - switch v := opts.Data.(type) { - case string: - reader = strings.NewReader(v) - contentType = "x-www-form-urlencoded" - case []byte: - reader = bytes.NewReader(v) - contentType = "x-www-form-urlencoded" - default: - if buf, err = json.Marshal(v); err == nil { - reader = bytes.NewReader(buf) - contentType = "application/json" - } else { - return - } + if reader, contentType, err = encode(opts.Data); err != nil { + return } } if req, err = http.NewRequest(http.MethodPost, uri.String(), reader); err != nil { @@ -127,6 +134,7 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option) res *http.Response req *http.Request contentType string + reader io.Reader ) opts := newOptions() for _, cb := range cbs { @@ -142,7 +150,12 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option) } uri.RawQuery = qs.Encode() } - if req, err = http.NewRequest(http.MethodGet, uri.String(), nil); err != nil { + if opts.Data != nil { + if reader, contentType, err = encode(opts.Data); err != nil { + return + } + } + if req, err = http.NewRequest(opts.Method, uri.String(), reader); err != nil { return } if opts.Header != nil { @@ -150,6 +163,9 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option) req.Header.Set(k, v) } } + if contentType != "" { + req.Header.Set("Content-Type", contentType) + } if res, err = do(ctx, req, opts); err != nil { return }