Skip to content

Commit 71e45cd

Browse files
authored
use go 1.23 (#41)
1 parent 38c5727 commit 71e45cd

File tree

5 files changed

+20
-39
lines changed

5 files changed

+20
-39
lines changed

.github/workflows/checks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ concurrency:
1515
cancel-in-progress: true
1616

1717
env:
18-
GO: "1.20"
18+
GO: "1.23"
1919

2020
jobs:
2121
CodeQL:

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ permissions:
99
contents: write
1010

1111
env:
12-
GO: "1.20"
12+
GO: "1.23"
1313

1414
jobs:
1515
release-cli:

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/sv-tools/mock-http-server
22

3-
go 1.20
3+
go 1.23.0
44

55
require (
66
github.com/go-chi/chi/v5 v5.1.0
77
github.com/spf13/pflag v1.0.5
8-
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9
8+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
99
gopkg.in/yaml.v3 v3.0.1
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
22
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
33
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
44
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
5-
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6ZucTZatCCEm9D47sZ2zo=
6-
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
5+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
6+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
77
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
88
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
99
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

log.go

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"os"
@@ -12,15 +13,15 @@ import (
1213

1314
func LogHandler() *slog.JSONHandler {
1415
// Setup a JSON handler for the new log/slog library
15-
return slog.HandlerOptions{
16+
return slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
1617
// Remove default time slog.Attr, we create our own later
1718
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
1819
if a.Key == slog.TimeKey {
1920
return slog.Attr{}
2021
}
2122
return a
2223
},
23-
}.NewJSONHandler(os.Stdout)
24+
})
2425
}
2526

2627
// StructuredLogger is a simple, but powerful implementation of a custom structured
@@ -37,6 +38,8 @@ type StructuredLogger struct {
3738
}
3839

3940
func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry {
41+
ctx := r.Context()
42+
4043
var logFields []slog.Attr
4144
logFields = append(logFields, slog.String("ts", time.Now().UTC().Format(time.RFC1123)))
4245

@@ -58,54 +61,32 @@ func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry {
5861
slog.String("uri", fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)),
5962
))
6063

61-
entry := StructuredLoggerEntry{Logger: slog.New(handler)}
64+
entry := StructuredLoggerEntry{
65+
ctx: ctx,
66+
Logger: slog.New(handler),
67+
}
6268

63-
entry.Logger.LogAttrs(slog.LevelInfo, "request started")
69+
entry.Logger.LogAttrs(ctx, slog.LevelInfo, "request started")
6470

6571
return &entry
6672
}
6773

6874
type StructuredLoggerEntry struct {
75+
ctx context.Context
6976
Logger *slog.Logger
7077
}
7178

7279
func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
73-
l.Logger.LogAttrs(slog.LevelInfo, "request complete",
80+
l.Logger.LogAttrs(l.ctx, slog.LevelInfo, "request complete",
7481
slog.Int("resp_status", status),
7582
slog.Int("resp_byte_length", bytes),
7683
slog.Float64("resp_elapsed_ms", float64(elapsed.Nanoseconds())/1000000.0),
7784
)
7885
}
7986

80-
func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) {
81-
l.Logger.LogAttrs(slog.LevelInfo, "",
87+
func (l *StructuredLoggerEntry) Panic(v any, stack []byte) {
88+
l.Logger.LogAttrs(l.ctx, slog.LevelInfo, "",
8289
slog.String("stack", string(stack)),
8390
slog.String("panic", fmt.Sprintf("%+v", v)),
8491
)
8592
}
86-
87-
// Helper methods used by the application to get the request-scoped
88-
// logger entry and set additional fields between handlers.
89-
//
90-
// This is a useful pattern to use to set state on the entry as it
91-
// passes through the handler chain, which at any point can be logged
92-
// with a call to .Print(), .Info(), etc.
93-
94-
func GetLogEntry(r *http.Request) *slog.Logger {
95-
entry := middleware.GetLogEntry(r).(*StructuredLoggerEntry)
96-
return entry.Logger
97-
}
98-
99-
func LogEntrySetField(r *http.Request, key string, value interface{}) {
100-
if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok {
101-
entry.Logger = entry.Logger.With(key, value)
102-
}
103-
}
104-
105-
func LogEntrySetFields(r *http.Request, fields map[string]interface{}) {
106-
if entry, ok := r.Context().Value(middleware.LogEntryCtxKey).(*StructuredLoggerEntry); ok {
107-
for k, v := range fields {
108-
entry.Logger = entry.Logger.With(k, v)
109-
}
110-
}
111-
}

0 commit comments

Comments
 (0)