/* * Micro Service * Package: gitlab.hexacode.org/go-libs/microservice * Maintainer: Azzis Arswendo * * Copyright (C) 2023 Hexacode Teknologi Indonesia * All Rights Reserved */ package config import ( "errors" "plugin" "gitlab.hexacode.org/go-libs/hctypes" ) var ( plugins map[string]*plugin.Plugin = map[string]*plugin.Plugin{} ) type ConfigCallback struct { Plugin string `json:"plugin,omitempty"` Function string `json:"function,omitempty"` plugin *plugin.Plugin `json:"-"` function func(*ConfigContext) (int, hctypes.Dict, *ReturnHTTPError) `json:"-"` } func (config *Config) LoadPlugin() error { apis := config.APIs if apis != nil { for _, api := range apis { for _, param := range api.Params { if param.Name == "" { panic(errors.New("api.params[].name is required")) } } callback := api.Callback if callback == nil { api.Callback = &ConfigCallback{ function: DefaultCallbackAPIFunction, } } else { plg, err := plugin.Open(callback.Plugin) if err != nil { panic(err) return err } callback.plugin = plg function, err := plg.Lookup(callback.Function) if err != nil { panic(err) return err } callback.function = function.(func(*ConfigContext) (int, hctypes.Dict, *ReturnHTTPError)) } } } templates := config.Templates if templates != nil { for _, template := range templates { callback := template.Callback if callback == nil { template.Callback = &ConfigTemplateCallback{ function: DefaultCallbackTemplateFunction, } } else { plg, err := plugin.Open(callback.Plugin) if err != nil { panic(err) return err } callback.plugin = plg function, err := plg.Lookup(callback.Function) if err != nil { panic(err) return err } callback.function = function.(func(*ConfigTemplateContext) hctypes.Dict) } } } return nil }