Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some logs in debug mode #37

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions kid.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kid

import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"
"sync"

Expand Down Expand Up @@ -40,6 +43,9 @@ type (
}
)

// Version is the Kid version.
const Version string = "0.1.0"

// New returns a new instance of Kid.
func New() *Kid {
kid := Kid{
Expand All @@ -51,6 +57,7 @@ func New() *Kid {
jsonSerializer: serializer.NewJSONSerializer(),
xmlSerializer: serializer.NewXMLSerializer(),
htmlRenderer: htmlrenderer.Default(false),
debug: true,
}

kid.pool.New = func() any {
Expand All @@ -65,6 +72,11 @@ func New() *Kid {
// Specifying an address is optional. Default address is :2376.
func (k *Kid) Run(address ...string) error {
addr := resolveAddress(address)

k.printDebug(os.Stdout, "Kid version %s\n", Version)
k.printDebug(os.Stdout, "Starting server at %s\n", addr)
k.printDebug(os.Stdout, "Quit the server with CONTROL-C\n")

return http.ListenAndServe(addr, k)
}

Expand Down Expand Up @@ -230,6 +242,13 @@ func (k *Kid) ApplyOptions(opts ...Option) {
}
}

// printDebug prints logs only in debug mode.
func (k *Kid) printDebug(w io.Writer, format string, values ...any) {
if k.Debug() {
fmt.Fprintf(w, "[DEBUG] "+format, values...)
}
}

// getPath returns request's path.
func getPath(u *url.URL) string {
if u.RawPath != "" {
Expand Down
17 changes: 16 additions & 1 deletion kid_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kid

import (
"bytes"
"fmt"
"io"
"net/http"
Expand All @@ -25,6 +26,7 @@ func TestNew(t *testing.T) {
assert.True(t, funcsAreEqual(defaultErrorHandler, k.errorHandler))
assert.True(t, funcsAreEqual(defaultNotFoundHandler, k.notFoundHandler))
assert.True(t, funcsAreEqual(defaultMethodNotAllowedHandler, k.methodNotAllowedHandler))
assert.True(t, k.Debug())
}

func TestKid_Use(t *testing.T) {
Expand Down Expand Up @@ -559,7 +561,6 @@ func TestKid_Static(t *testing.T) {
assert.Equal(t, testCase.expectedContent, testCase.res.Body.String())
})
}

}

func TestKid_StaticFS(t *testing.T) {
Expand Down Expand Up @@ -598,7 +599,21 @@ func TestKid_StaticFS(t *testing.T) {
assert.Equal(t, testCase.expectedContent, testCase.res.Body.String())
})
}
}

func TestKid_printDebug(t *testing.T) {
k := New()

var w bytes.Buffer

k.printDebug(&w, "hello %s\n", "Kid")
assert.Equal(t, "[DEBUG] hello Kid\n", w.String())

w.Reset()
k.debug = false

k.printDebug(&w, "hello %s\n", "Kid")
assert.Empty(t, w.String())
}

func TestResolveAddress(t *testing.T) {
Expand Down