Skip to content

Commit c95211a

Browse files
authored
merge: PR #4 from feature/roundtrip
feature/roundtrip
2 parents f654081 + 6ef37cd commit c95211a

File tree

10 files changed

+414
-54
lines changed

10 files changed

+414
-54
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0
1+
0.3.0

cmd/api/main.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"log"
6+
"log/slog"
77
"net/http"
8+
"os"
89
"os/signal"
910
"syscall"
1011
"time"
1112

1213
"github.com/quyxishi/whitebox/internal/api"
14+
mlog "github.com/quyxishi/whitebox/internal/log"
1315
)
1416

1517
func gracefulShutdown(apiServer *http.Server, done chan bool) {
@@ -20,24 +22,34 @@ func gracefulShutdown(apiServer *http.Server, done chan bool) {
2022
// Listen for the interrupt signal.
2123
<-ctx.Done()
2224

23-
log.Println("Shutting down gracefully, press Ctrl+C again to force")
25+
slog.Info("Shutting down gracefully, press Ctrl+C again to force")
2426
stop() // Allow Ctrl+C to force shutdown
2527

2628
// The context is used to inform the server it has 5 seconds to finish
2729
// the request it is currently handling
2830
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2931
defer cancel()
3032
if err := apiServer.Shutdown(ctx); err != nil {
31-
log.Printf("Server forced to shutdown with error: %v", err)
33+
slog.Error("Server forced to shutdown", "due", err.Error())
3234
}
3335

34-
log.Println("Server exiting")
36+
slog.Info("Server exiting")
3537

3638
// Notify the main goroutine that the shutdown is complete
3739
done <- true
3840
}
3941

4042
func main() {
43+
opts := mlog.ModuleHandlerOptions{
44+
SlogOpts: slog.HandlerOptions{
45+
Level: slog.LevelDebug,
46+
},
47+
}
48+
handler := mlog.NewModuleHandler(os.Stdout, &opts)
49+
slog.SetDefault(slog.New(handler))
50+
51+
// *
52+
4153
server := api.NewServer()
4254

4355
// Create a done channel to signal when the shutdown is complete
@@ -53,5 +65,5 @@ func main() {
5365

5466
// Wait for the graceful shutdown to complete
5567
<-done
56-
log.Println("Graceful shutdown complete.")
68+
slog.Info("Graceful shutdown complete.")
5769
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ require (
7878
golang.org/x/arch v0.23.0 // indirect
7979
golang.org/x/crypto v0.45.0 // indirect
8080
golang.org/x/mod v0.29.0 // indirect
81-
golang.org/x/net v0.47.0 // indirect
81+
golang.org/x/net v0.47.0
8282
golang.org/x/sync v0.18.0 // indirect
8383
golang.org/x/sys v0.38.0 // indirect
8484
golang.org/x/text v0.31.0 // indirect

internal/api/v1/error_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package v1
22

33
import (
4-
"log"
4+
"log/slog"
55
"net/http"
66
"runtime/debug"
77

@@ -12,7 +12,7 @@ func GlobalErrorHandler() gin.HandlerFunc {
1212
return func(c *gin.Context) {
1313
defer func() {
1414
if err := recover(); err != nil {
15-
log.Printf("[ERROR] %v\n%s\n", err, string(debug.Stack()))
15+
slog.Error("unexpected", "error", err, "stackTrace", string(debug.Stack()))
1616
c.String(http.StatusInternalServerError, "Unexpected error: %v", err)
1717
c.Abort()
1818
}

internal/api/v1/probe/counter.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package probe
2+
3+
import (
4+
"io"
5+
"net/http"
6+
)
7+
8+
type ByteCounter struct {
9+
io.ReadCloser
10+
n int64
11+
}
12+
13+
func (h *ByteCounter) Read(p []byte) (int, error) {
14+
n, err := h.ReadCloser.Read(p)
15+
h.n += int64(n)
16+
return n, err
17+
}
18+
19+
type RedirectCounter struct {
20+
Total int
21+
Max int
22+
}
23+
24+
func (h *RedirectCounter) CheckRedirect(req *http.Request, via []*http.Request) error {
25+
if h.Total = len(via); h.Total > h.Max {
26+
return http.ErrUseLastResponse
27+
}
28+
return nil
29+
}

0 commit comments

Comments
 (0)