Skip to content

Commit c8282f0

Browse files
authored
feat: add some logs when running server (#37)
1 parent bca391d commit c8282f0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

kid.go

+19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package kid
22

33
import (
4+
"fmt"
5+
"io"
46
"net/http"
57
"net/url"
8+
"os"
69
"reflect"
710
"sync"
811

@@ -40,6 +43,9 @@ type (
4043
}
4144
)
4245

46+
// Version is the Kid version.
47+
const Version string = "0.1.0"
48+
4349
// New returns a new instance of Kid.
4450
func New() *Kid {
4551
kid := Kid{
@@ -51,6 +57,7 @@ func New() *Kid {
5157
jsonSerializer: serializer.NewJSONSerializer(),
5258
xmlSerializer: serializer.NewXMLSerializer(),
5359
htmlRenderer: htmlrenderer.Default(false),
60+
debug: true,
5461
}
5562

5663
kid.pool.New = func() any {
@@ -65,6 +72,11 @@ func New() *Kid {
6572
// Specifying an address is optional. Default address is :2376.
6673
func (k *Kid) Run(address ...string) error {
6774
addr := resolveAddress(address)
75+
76+
k.printDebug(os.Stdout, "Kid version %s\n", Version)
77+
k.printDebug(os.Stdout, "Starting server at %s\n", addr)
78+
k.printDebug(os.Stdout, "Quit the server with CONTROL-C\n")
79+
6880
return http.ListenAndServe(addr, k)
6981
}
7082

@@ -230,6 +242,13 @@ func (k *Kid) ApplyOptions(opts ...Option) {
230242
}
231243
}
232244

245+
// printDebug prints logs only in debug mode.
246+
func (k *Kid) printDebug(w io.Writer, format string, values ...any) {
247+
if k.Debug() {
248+
fmt.Fprintf(w, "[DEBUG] "+format, values...)
249+
}
250+
}
251+
233252
// getPath returns request's path.
234253
func getPath(u *url.URL) string {
235254
if u.RawPath != "" {

kid_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kid
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"net/http"
@@ -25,6 +26,7 @@ func TestNew(t *testing.T) {
2526
assert.True(t, funcsAreEqual(defaultErrorHandler, k.errorHandler))
2627
assert.True(t, funcsAreEqual(defaultNotFoundHandler, k.notFoundHandler))
2728
assert.True(t, funcsAreEqual(defaultMethodNotAllowedHandler, k.methodNotAllowedHandler))
29+
assert.True(t, k.Debug())
2830
}
2931

3032
func TestKid_Use(t *testing.T) {
@@ -559,7 +561,6 @@ func TestKid_Static(t *testing.T) {
559561
assert.Equal(t, testCase.expectedContent, testCase.res.Body.String())
560562
})
561563
}
562-
563564
}
564565

565566
func TestKid_StaticFS(t *testing.T) {
@@ -598,7 +599,21 @@ func TestKid_StaticFS(t *testing.T) {
598599
assert.Equal(t, testCase.expectedContent, testCase.res.Body.String())
599600
})
600601
}
602+
}
603+
604+
func TestKid_printDebug(t *testing.T) {
605+
k := New()
606+
607+
var w bytes.Buffer
608+
609+
k.printDebug(&w, "hello %s\n", "Kid")
610+
assert.Equal(t, "[DEBUG] hello Kid\n", w.String())
611+
612+
w.Reset()
613+
k.debug = false
601614

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

604619
func TestResolveAddress(t *testing.T) {

0 commit comments

Comments
 (0)