fix package

This commit is contained in:
fancl 2023-04-26 16:08:54 +08:00
parent e734ab2ab4
commit cdc1af1b37
2 changed files with 32 additions and 20 deletions

View File

@ -122,13 +122,11 @@ func (app *application) httpServe() (err error) {
app.waitGroup.Go(func() { app.waitGroup.Go(func() {
select { select {
case errChan <- app.http.Serve(l): case errChan <- app.http.Serve(l):
log.Infof("http server closed")
} }
}) })
select { select {
case err = <-errChan: case err = <-errChan:
case <-timer.C: case <-timer.C:
log.Infof("http server started")
} }
return return
} }
@ -149,13 +147,11 @@ func (app *application) commandServe() (err error) {
app.waitGroup.Go(func() { app.waitGroup.Go(func() {
select { select {
case errChan <- app.command.Serve(l): case errChan <- app.command.Serve(l):
log.Infof("command server closed")
} }
}) })
select { select {
case err = <-errChan: case err = <-errChan:
case <-timer.C: case <-timer.C:
log.Infof("command server started")
} }
return return
} }

View File

@ -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) { func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
var ( var (
uri *url.URL 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) { func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
var ( var (
buf []byte
uri *url.URL uri *url.URL
req *http.Request req *http.Request
contentType string contentType string
@ -90,22 +109,10 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
uri.RawQuery = qs.Encode() uri.RawQuery = qs.Encode()
} }
if opts.Data != nil { if opts.Data != nil {
switch v := opts.Data.(type) { if reader, contentType, err = encode(opts.Data); err != nil {
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 return
} }
} }
}
if req, err = http.NewRequest(http.MethodPost, uri.String(), reader); err != nil { if req, err = http.NewRequest(http.MethodPost, uri.String(), reader); err != nil {
return return
} }
@ -127,6 +134,7 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
res *http.Response res *http.Response
req *http.Request req *http.Request
contentType string contentType string
reader io.Reader
) )
opts := newOptions() opts := newOptions()
for _, cb := range cbs { for _, cb := range cbs {
@ -142,7 +150,12 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
} }
uri.RawQuery = qs.Encode() 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 return
} }
if opts.Header != nil { if opts.Header != nil {
@ -150,6 +163,9 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
req.Header.Set(k, v) req.Header.Set(k, v)
} }
} }
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if res, err = do(ctx, req, opts); err != nil { if res, err = do(ctx, req, opts); err != nil {
return return
} }