2020-11-12 14:03:20 +03:00
## Message Gateway Transport Library
2020-11-26 17:11:11 +03:00
[](https://github.com/retailcrm/mg-transport-core/actions?query=workflow%3Aci)
2020-12-15 13:35:47 +03:00
[](https://codecov.io/gh/retailcrm/mg-transport-core)
[](https://github.com/retailcrm/mg-transport-core/releases)
2019-12-13 10:44:06 +03:00
[](https://goreportcard.com/report/github.com/retailcrm/mg-transport-core)
2020-12-15 13:35:47 +03:00
[](https://golang.org/dl/)
[](https://pkg.go.dev/github.com/retailcrm/mg-transport-core/core)
This library provides different functions like error-reporting, logging, localization, etc. in order to make it easier to create transports.
2020-11-12 14:03:20 +03:00
2019-09-18 14:05:06 +03:00
Usage:
```go
package main
import (
"os"
"fmt"
"html/template"
"github.com/gin-gonic/gin"
"github.com/retailcrm/mg-transport-core/core"
)
func main() {
2020-05-18 13:37:24 +03:00
// Create new core.Engine instance
2022-04-12 17:11:52 +03:00
app := core.New(core.AppInfo{
Version: "v1.0",
Commit: "bcef82e",
Build: "v1.0-bcef82e",
BuildDate: "1649766442",
})
2020-05-18 13:37:24 +03:00
// Load configuration
2019-09-18 14:05:06 +03:00
app.Config = core.NewConfig("config.yml")
2020-05-18 13:37:24 +03:00
// Set default error translation key (will be returned if something goes wrong)
2019-09-18 14:05:06 +03:00
app.DefaultError = "unknown_error"
2020-05-18 13:37:24 +03:00
// Set translations path
2019-09-18 14:05:06 +03:00
app.TranslationsPath = "./translations"
2020-05-18 13:37:24 +03:00
// Preload some translations so they will not be loaded for every request
app.PreloadLanguages = core.DefaultLanguages
2019-09-18 14:05:06 +03:00
2020-05-18 13:37:24 +03:00
// Configure gin.Engine inside core.Engine
2019-09-18 14:05:06 +03:00
app.ConfigureRouter(func(engine *gin.Engine) {
engine.Static("/static", "./static")
engine.HTMLRender = app.CreateRenderer(
2020-05-18 13:37:24 +03:00
] // Insert templates here. Custom functions also can be provided.
// Default transl function will be injected automatically
2019-09-18 14:05:06 +03:00
func(renderer *core.Renderer) {
2020-05-18 13:37:24 +03:00
// Push method will load template from FS or from binary
2019-09-18 14:05:06 +03:00
r.Push("home", "templates/layout.html", "templates/home.html")
},
template.FuncMap{},
)
})
2020-05-18 13:37:24 +03:00
// Start application or fail if something gone wrong (e.g. port is already in use)
2019-09-18 14:05:06 +03:00
if err := app.Prepare().Run(); err != nil {
fmt.Printf("Fatal error: %s", err.Error())
os.Exit(1)
}
}
```
### Resource embedding
2021-07-29 11:29:31 +03:00
[embed ](https://golang.org/pkg/embed/ ) can be used to provide resource embedding. Go source files that import "embed" can use the //go:embed directive to initialize a variable of type string, []byte, or FS with the contents of files read from the package directory or subdirectories at compile time.
2020-12-15 13:35:47 +03:00
Example:
2019-09-18 14:05:06 +03:00
```go
package main
import (
"os"
"fmt"
"html/template"
2021-07-29 11:29:31 +03:00
"io/fs"
"net/http"
2019-09-18 14:05:06 +03:00
"github.com/gin-gonic/gin"
"github.com/retailcrm/mg-transport-core/core"
)
2021-07-29 11:29:31 +03:00
//go:embed static
var Static fs.FS
//go:embed translations
var Translate fs.FS
//go:embed templates
var Templates fs.FS
2019-09-18 14:05:06 +03:00
func main() {
2021-07-29 11:29:31 +03:00
staticFS, err := fs.Sub(Static, "static")
if err != nil {
panic(err)
}
translateFS, err := fs.Sub(Translate, "translate")
if err != nil {
panic(err)
}
templatesFS, err := fs.Sub(Templates, "templates")
if err != nil {
panic(err)
}
2019-09-18 14:05:06 +03:00
app := core.New()
app.Config = core.NewConfig("config.yml")
app.DefaultError = "unknown_error"
2020-05-18 13:37:24 +03:00
2021-07-29 11:29:31 +03:00
// Now translations will be loaded from embedded files in Go program
app.TranslationsFS = translateFS
2020-05-18 13:37:24 +03:00
app.PreloadLanguages = core.DefaultLanguages
2019-09-18 14:05:06 +03:00
app.ConfigureRouter(func(engine *gin.Engine) {
2021-07-29 11:29:31 +03:00
// fs.FS should be converted to the http.FileSystem
// FS implements the io/fs package's FS interface,
// so it can be used with any package that understands file systems,
// including net/http, text/template, and html/template.
engine.StaticFS("/static", http.FS(staticFS))
2019-09-18 14:05:06 +03:00
engine.HTMLRender = app.CreateRendererFS(
2021-07-29 11:29:31 +03:00
templatesFS,
2019-09-18 14:05:06 +03:00
func(renderer *core.Renderer) {
2020-05-18 13:37:24 +03:00
// Same Push method here, but without relative directory.
2019-09-18 14:05:06 +03:00
r.Push("home", "layout.html", "home.html")
},
template.FuncMap{},
)
})
if err := app.Prepare().Run(); err != nil {
fmt.Printf("Fatal error: %s", err.Error())
os.Exit(1)
}
}
2019-10-17 12:45:28 +03:00
```
### Migration generator
This library contains helper tool for transports. You can install it via go:
```sh
$ go get -u github.com/retailcrm/mg-transport-core/cmd/transport-core-tool
```
2020-11-12 14:03:20 +03:00
Currently, it only can generate new migrations for your transport.