Skip to content

Commit c9eca0b

Browse files
committed
fix: circular dep
1 parent 57c7495 commit c9eca0b

5 files changed

Lines changed: 44 additions & 20 deletions

File tree

contract/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/studiolambda/cosmos/contract
22

33
go 1.25.0
44

5-
require github.com/stretchr/testify v1.11.1
5+
require (
6+
github.com/stretchr/testify v1.11.1
7+
github.com/studiolambda/cosmos/problem v0.2.0
8+
)
69

710
require (
811
github.com/brunoga/deep v1.2.4 // indirect

contract/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9L
1313
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
1414
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
1515
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
16+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1617
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
1718
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
1819
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
@@ -71,6 +72,9 @@ github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
7172
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
7273
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
7374
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
75+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
76+
github.com/studiolambda/cosmos/problem v0.2.0 h1:TgGSrxwmCuZfOoMJaXkQVnuGxLvHyRddZYcsuCBsZ2o=
77+
github.com/studiolambda/cosmos/problem v0.2.0/go.mod h1:JUlbThLa0CVpO+E3xfKBgxSQA7FLQ6gyvdwXgcKreb4=
7478
github.com/vektra/mockery/v3 v3.6.1 h1:YyqAXihdNML8y6SJnvPKYr+2HAHvBjdvqFu/fMYlX8g=
7579
github.com/vektra/mockery/v3 v3.6.1/go.mod h1:Oti3Df0WP8wwT31yuVri3QNsDeMUQU5Q4QEg8EabaBw=
7680
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
@@ -82,14 +86,20 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ
8286
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
8387
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
8488
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
89+
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
8590
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
91+
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
8692
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8793
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8894
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8995
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
96+
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
9097
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
98+
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
9199
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
100+
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
92101
golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ=
102+
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
93103
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
94104
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
95105
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

contract/hooks.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package contract
22

3+
import "net/http"
4+
35
type hooksKey struct{}
46

57
var HooksKey = hooksKey{}
8+
9+
type AfterResponseHook func(err error)
10+
type BeforeWriteHeaderHook func(w http.ResponseWriter, status int)
11+
type BeforeWriteHook func(w http.ResponseWriter, content []byte)
12+
13+
type Hooks interface {
14+
AfterResponse(callbacks ...AfterResponseHook)
15+
AfterResponseFuncs() []AfterResponseHook
16+
BeforeWrite(callbacks ...BeforeWriteHook)
17+
BeforeWriteFuncs() []BeforeWriteHook
18+
BeforeWriteHeader(callbacks ...BeforeWriteHeaderHook)
19+
BeforeWriteHeaderFuncs() []BeforeWriteHeaderHook
20+
}

contract/request/hooks.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/http"
55

66
"github.com/studiolambda/cosmos/contract"
7-
"github.com/studiolambda/cosmos/framework"
87
"github.com/studiolambda/cosmos/problem"
98
)
109

@@ -18,8 +17,8 @@ var ErrNoHooksMiddleware = problem.Problem{
1817
// to certain lifecycle events. It panics if no context
1918
// value is found. Make sure you use the hooks middleware
2019
// before using this method.
21-
func Hooks(r *http.Request) *framework.Hooks {
22-
if hooks, ok := r.Context().Value(contract.HooksKey).(*framework.Hooks); ok {
20+
func Hooks(r *http.Request) contract.Hooks {
21+
if hooks, ok := r.Context().Value(contract.HooksKey).(contract.Hooks); ok {
2322
return hooks
2423
}
2524

framework/hooks.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package framework
22

33
import (
4-
"net/http"
54
"slices"
65
"sync"
6+
7+
"github.com/studiolambda/cosmos/contract"
78
)
89

910
// Hooks is a structure that can be used to
@@ -13,31 +14,27 @@ import (
1314
// It is safe for concurrent use.
1415
type Hooks struct {
1516
mutex sync.Mutex
16-
afterResponseHooks []AfterResponseHook
17-
beforeWriteHeaderHooks []BeforeWriteHeaderHook
18-
beforeWriteHooks []BeforeWriteHook
17+
afterResponseHooks []contract.AfterResponseHook
18+
beforeWriteHeaderHooks []contract.BeforeWriteHeaderHook
19+
beforeWriteHooks []contract.BeforeWriteHook
1920
}
2021

21-
type AfterResponseHook func(err error)
22-
type BeforeWriteHeaderHook func(w http.ResponseWriter, status int)
23-
type BeforeWriteHook func(w http.ResponseWriter, content []byte)
24-
2522
func NewHooks() *Hooks {
2623
return &Hooks{
2724
mutex: sync.Mutex{},
28-
beforeWriteHeaderHooks: []BeforeWriteHeaderHook{},
29-
beforeWriteHooks: []BeforeWriteHook{},
25+
beforeWriteHeaderHooks: []contract.BeforeWriteHeaderHook{},
26+
beforeWriteHooks: []contract.BeforeWriteHook{},
3027
}
3128
}
3229

33-
func (h *Hooks) BeforeWriteHeader(callbacks ...BeforeWriteHeaderHook) {
30+
func (h *Hooks) BeforeWriteHeader(callbacks ...contract.BeforeWriteHeaderHook) {
3431
h.mutex.Lock()
3532
defer h.mutex.Unlock()
3633

3734
h.beforeWriteHeaderHooks = append(h.beforeWriteHeaderHooks, callbacks...)
3835
}
3936

40-
func (h *Hooks) BeforeWriteHeaderFuncs() []BeforeWriteHeaderHook {
37+
func (h *Hooks) BeforeWriteHeaderFuncs() []contract.BeforeWriteHeaderHook {
4138
h.mutex.Lock()
4239
defer h.mutex.Unlock()
4340

@@ -47,14 +44,14 @@ func (h *Hooks) BeforeWriteHeaderFuncs() []BeforeWriteHeaderHook {
4744
return clone
4845
}
4946

50-
func (h *Hooks) BeforeWrite(callbacks ...BeforeWriteHook) {
47+
func (h *Hooks) BeforeWrite(callbacks ...contract.BeforeWriteHook) {
5148
h.mutex.Lock()
5249
defer h.mutex.Unlock()
5350

5451
h.beforeWriteHooks = append(h.beforeWriteHooks, callbacks...)
5552
}
5653

57-
func (h *Hooks) BeforeWriteFuncs() []BeforeWriteHook {
54+
func (h *Hooks) BeforeWriteFuncs() []contract.BeforeWriteHook {
5855
h.mutex.Lock()
5956
defer h.mutex.Unlock()
6057

@@ -64,14 +61,14 @@ func (h *Hooks) BeforeWriteFuncs() []BeforeWriteHook {
6461
return clone
6562
}
6663

67-
func (h *Hooks) AfterResponse(callbacks ...AfterResponseHook) {
64+
func (h *Hooks) AfterResponse(callbacks ...contract.AfterResponseHook) {
6865
h.mutex.Lock()
6966
defer h.mutex.Unlock()
7067

7168
h.afterResponseHooks = append(h.afterResponseHooks, callbacks...)
7269
}
7370

74-
func (h *Hooks) AfterResponseFuncs() []AfterResponseHook {
71+
func (h *Hooks) AfterResponseFuncs() []contract.AfterResponseHook {
7572
h.mutex.Lock()
7673
defer h.mutex.Unlock()
7774

0 commit comments

Comments
 (0)