Refactor and doc

This commit is contained in:
ycc
2024-09-04 10:53:12 +02:00
parent fba1608edb
commit bb36ac0fb4
9 changed files with 161 additions and 45 deletions

View File

@@ -1,5 +1,61 @@
# oc-lib
oc-lib allows read/write/search operations into the main OpenCloud databases.
It also provides common initialization and configuration utilities for all OpenCloud components
## Usage example in a beego API
```go
const appname = "oc-mycomponent"
func main() {
// Init the oc-lib
oclib.Init(appname)
// Load the right config file
/* The configuration loader will load the configuration from the following sources:
* - the environment variables with the prefix APPNAME_
* - the file /etc/oc/appname.json
* - the file ./appname.json
* The configuration loader will merge the configuration from the different sources
* The configuration loader will give priority to the environment variables
* The configuration loader will give priority to the local file over the default file
*/
o := oclib.GetConfLoader()
// init the local config object
models.GetConfig().Port = o.GetIntDefault("port", 8080)
models.GetConfig().LokiUrl = o.GetStringDefault("lokiurl", "")
models.GetConfig().LogLevel = o.GetStringDefault("loglevel", "info")
models.GetConfig().MongoUrl = o.GetStringDefault("mongourl", "mongodb://127.0.0.1:27017")
models.GetConfig().MongoDatabase = o.GetStringDefault("mongodatabase", "myDb")
models.GetConfig().NatsUrl = o.GetStringDefault("natsurl", "nats://localhost:4222")
models.GetConfig().mycomponentparam1 = o.GetStringDefault("mycomponentparam1", "mycomponentdefault1")
models.GetConfig().mycomponentparam2 = o.GetStringDefault("mycomponentparam2", "mycomponentdefault2")
// feed the library with the loaded config,
// this will also initialize a logger available via oclib.GetLogger()
oclib.SetConfig(
models.GetConfig().MongoUrl
models.GetConfig().MongoDatabase
models.GetConfig().NatsUrl
models.GetConfig().LokiUrl
models.GetConfig().LogLevel
)
// Beego init
beego.BConfig.AppName = appname
beego.BConfig.Listen.HTTPPort = models.GetConfig().Port
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
beego.Run()
}
```
## SPECIAL FLOWS IN OC-LIB RESUME :
### WORKFLOW AS ITS OWN WORKSPACE