Skip to content

Commit 048af15

Browse files
committed
Add ability to lookup mux handle
1 parent 4c64636 commit 048af15

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

design/validation.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ func (r *ResourceDefinition) Validate() *ValidationErrors {
211211
if r.Params != nil {
212212
verr.Merge(r.Params.Validate("resource parameters", r))
213213
}
214-
if err := CanUse(r, Design); err != nil {
215-
verr.Add(r, "Invalid API version in list")
214+
if !r.SupportsNoVersion() {
215+
if err := CanUse(r, Design); err != nil {
216+
verr.Add(r, "Invalid API version in list")
217+
}
216218
}
217219
return verr.AsError()
218220
}

goagen/gen_app/generator.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ func (g *Generator) Generate(api *design.APIDefinition) (_ []string, err error)
8686

8787
outdir := AppOutputDir()
8888
err = api.IterateVersions(func(v *design.APIVersionDefinition) error {
89-
verdir := filepath.Join(outdir, codegen.VersionPackage(v.Version))
89+
verdir := outdir
90+
if v.Version != "" {
91+
verdir = filepath.Join(verdir, codegen.VersionPackage(v.Version))
92+
}
9093
if err := os.MkdirAll(verdir, 0755); err != nil {
9194
return err
9295
}

mux.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type (
3434
http.Handler
3535
// Handle sets the HandleFunc for a given HTTP method and path.
3636
Handle(method, path string, handle HandleFunc)
37+
// Lookup returns the HandleFunc associated with the given HTTP method and path.
38+
Lookup(method, path string) HandleFunc
3739
}
3840

3941
// HandleFunc provides the implementation for an API endpoint.
@@ -57,15 +59,19 @@ type (
5759

5860
// defaultVersionMux is the default goa API version specific mux.
5961
defaultVersionMux struct {
60-
router *httprouter.Router
62+
router *httprouter.Router
63+
handles map[string]HandleFunc
6164
}
6265
)
6366

6467
// NewMux creates a top level mux using the default goa mux implementation.
6568
func NewMux() ServeMux {
6669
return &DefaultMux{
67-
defaultVersionMux: &defaultVersionMux{router: httprouter.New()},
68-
selectVersion: PathSelectVersionFunc("/:version"),
70+
defaultVersionMux: &defaultVersionMux{
71+
router: httprouter.New(),
72+
handles: make(map[string]HandleFunc),
73+
},
74+
selectVersion: PathSelectVersionFunc("/:version"),
6975
}
7076
}
7177

@@ -173,9 +179,15 @@ func (m *defaultVersionMux) Handle(method, path string, handle HandleFunc) {
173179
}
174180
handle(rw, req, params)
175181
}
182+
m.handles[method+path] = handle
176183
m.router.Handle(method, path, hthandle)
177184
}
178185

186+
// Lookup returns the HandleFunc associated with the given method and path.
187+
func (m *defaultVersionMux) Lookup(method, path string) HandleFunc {
188+
return m.handles[method+path]
189+
}
190+
179191
// ServeHTTP is the function called back by the underlying HTTP server to handle incoming requests.
180192
func (m *defaultVersionMux) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
181193
m.router.ServeHTTP(rw, req)

0 commit comments

Comments
 (0)