Skip to content

Commit

Permalink
add log option, update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed May 11, 2020
1 parent b17f946 commit 545b68c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 41 deletions.
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
Expand All @@ -27,6 +28,8 @@ type Client struct {
APIHeaderHost string

httpClient *http.Client

Log func(...interface{})
}

// Send response back to the public request
Expand All @@ -41,6 +44,9 @@ func New(subdomain string) *Client {
APIHeaderHost: "digto.org",
Subdomain: subdomain,
httpClient: &http.Client{},
Log: func(s ...interface{}) {
log.Println(s...)
},
}
}

Expand Down
13 changes: 6 additions & 7 deletions client/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"log"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -47,7 +46,7 @@ func (c *Client) Serve(addr, overrideHost, scheme string) {
for {
req, send, err := c.Next()
if err != nil {
log.Println(err)
c.Log(err)
continue
}

Expand All @@ -56,7 +55,7 @@ func (c *Client) Serve(addr, overrideHost, scheme string) {
}

func (c *Client) serve(addr, overrideHost, scheme string, req *http.Request, send Send) {
log.Println("[access log]", kit.C(req.Method, "green"), req.URL.String())
c.Log("[access log]", kit.C(req.Method, "green"), req.URL.String())

req.URL.Scheme = scheme
req.URL.Host = addr
Expand All @@ -67,20 +66,20 @@ func (c *Client) serve(addr, overrideHost, scheme string, req *http.Request, sen
httpClient := &http.Client{}
res, err := httpClient.Do(req)
if err != nil {
resErr(send, err.Error())
c.resErr(send, err.Error())
return
}

err = send(res.StatusCode, res.Header, res.Body)
if err != nil {
log.Println(err)
c.Log(err)
}
}

func resErr(send Send, msg string) {
func (c *Client) resErr(send Send, msg string) {
err := send(http.StatusInternalServerError, nil, bytes.NewBufferString(msg))
if err != nil {
log.Println(err)
c.Log(err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/ysmood/digto
go 1.14

require (
github.com/gin-gonic/gin v1.6.2
github.com/go-acme/lego/v3 v3.5.0
github.com/gin-gonic/gin v1.6.3
github.com/go-acme/lego/v3 v3.7.0
github.com/stretchr/testify v1.5.1
github.com/ysmood/ddns v0.2.3
github.com/ysmood/kit v0.22.2
Expand Down
Loading

0 comments on commit 545b68c

Please sign in to comment.