RegisterModule registers a module by receiving a plain/empty value of the module. For registration to be properly recorded, this should be called in the init phase of runtime. Typically, the module package will do this as a side-effect of being imported. This function panics if the module's info is
(instance Module)
| 136 | // incomplete or invalid, or if the module is already |
| 137 | // registered. |
| 138 | func RegisterModule(instance Module) { |
| 139 | mod := instance.CaddyModule() |
| 140 | |
| 141 | if mod.ID == "" { |
| 142 | panic("module ID missing") |
| 143 | } |
| 144 | if mod.ID == "caddy" || mod.ID == "admin" { |
| 145 | panic(fmt.Sprintf("module ID '%s' is reserved", mod.ID)) |
| 146 | } |
| 147 | if mod.New == nil { |
| 148 | panic("missing ModuleInfo.New") |
| 149 | } |
| 150 | if val := mod.New(); val == nil { |
| 151 | panic("ModuleInfo.New must return a non-nil module instance") |
| 152 | } |
| 153 | |
| 154 | modulesMu.Lock() |
| 155 | defer modulesMu.Unlock() |
| 156 | |
| 157 | if _, ok := modules[string(mod.ID)]; ok { |
| 158 | panic(fmt.Sprintf("module already registered: %s", mod.ID)) |
| 159 | } |
| 160 | modules[string(mod.ID)] = mod |
| 161 | } |
| 162 | |
| 163 | // GetModule returns module information from its ID (full name). |
| 164 | func GetModule(name string) (ModuleInfo, error) { |