Skip to content

Commit

Permalink
coremain: register http endpoint for plugins automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Jun 24, 2022
1 parent 09a1320 commit d393fb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 12 additions & 3 deletions coremain/mosdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func RunMosdns(cfg *Config) error {
if len(pc.Type) == 0 || len(pc.Tag) == 0 {
continue
}
if _, ok := dupTag[pc.Tag]; ok {
if _, dup := dupTag[pc.Tag]; dup {
return fmt.Errorf("duplicated plugin tag %s", pc.Tag)
}
dupTag[pc.Tag] = struct{}{}
Expand All @@ -107,7 +107,12 @@ func RunMosdns(cfg *Config) error {
if err != nil {
return fmt.Errorf("failed to init plugin #%d, %w", i, err)
}

m.addPlugin(p)
// Also add it to api mux if plugin implements http.Handler.
if h, ok := p.(http.Handler); ok {
m.httpAPIMux.Handle(fmt.Sprintf("/plugins/%s/", p.Tag()), h)
}
}

if len(cfg.Servers) == 0 {
Expand All @@ -119,6 +124,7 @@ func RunMosdns(cfg *Config) error {
}
}

// Start http api server
if httpAddr := cfg.API.HTTP; len(httpAddr) > 0 {
httpServer := &http.Server{
Addr: httpAddr,
Expand Down Expand Up @@ -176,8 +182,11 @@ func (m *Mosdns) GetMatchers() map[string]executable_seq.Matcher {
return m.matchers
}

// GetHTTPAPIMux returns the api http.ServeMux. Plugin caller should
// register path "/plugins/plugin_tag"
// GetHTTPAPIMux returns the api http.ServeMux.
// The pattern "/plugins/plugin_tag/" has been registered if
// Plugin implements http.Handler interface.
// Plugin caller should register path that has "/plugins/plugin_tag/"
// prefix only.
func (m *Mosdns) GetHTTPAPIMux() *http.ServeMux {
return m.httpAPIMux
}
2 changes: 0 additions & 2 deletions plugin/executable/reverse_lookup/reverse_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ func newReverseLookup(bp *coremain.BP, args *Args) coremain.Plugin {
args: args,
store: newStore(),
}

bp.M().GetHTTPAPIMux().Handle("/plugins/"+bp.Tag(), p)
return p
}

Expand Down

0 comments on commit d393fb4

Please # to comment.