| 142 | } |
| 143 | |
| 144 | func (opt *Plugin) registerHandler() { |
| 145 | t := reflect.TypeOf(opt.Config) |
| 146 | v := reflect.ValueOf(opt.Config) |
| 147 | // 注册http响应 |
| 148 | for i, j := 0, t.NumMethod(); i < j; i++ { |
| 149 | name := t.Method(i).Name |
| 150 | if name == "ServeHTTP" { |
| 151 | continue |
| 152 | } |
| 153 | switch handler := v.Method(i).Interface().(type) { |
| 154 | case func(http.ResponseWriter, *http.Request): |
| 155 | patten := strings.ToLower(strings.ReplaceAll(name, "_", "/")) |
| 156 | opt.handle(patten, http.HandlerFunc(handler)) |
| 157 | } |
| 158 | } |
| 159 | if rootHandler, ok := opt.Config.(http.Handler); ok { |
| 160 | opt.handle("/", rootHandler) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func (opt *Plugin) settingPath() string { |
| 165 | return filepath.Join(SettingDir, strings.ToLower(opt.Name)+".yaml") |