docs(example): added new example

This commit is contained in:
SoXX 2024-08-27 11:43:22 +02:00
parent 969bc17737
commit da6eda19ea

150
README.md
View File

@ -13,36 +13,140 @@ go get git.anthrove.art/Anthrove/plug-sdk/v3
Below is a basic example of how to use the SDK: Below is a basic example of how to use the SDK:
````go ### proposed Plug structure
import "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/plug"
// Define what Source this Plug is used for ````
source := models.Source{ your-project/
DisplayName: "e621",
Domain: "e621.net", ├── cmd
Icon: "e621.net/icon.png", │ └── your-plug
│ └── main.go
├── config
├── internal
│ ├── service
│ └── utils
└── go.mod
````
### confog.go
````go
package config
import (
"fmt"
"github.com/caarlos0/env/v10"
"github.com/go-playground/validator/v10"
)
type CoreConfig struct {
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO" validate:"eq_ignore_case=FATAL|eq_ignore_case=ERROR|eq_ignore_case=WARN|eq_ignore_case=INFO|eq_ignore_case=DEBUG|eq_ignore_case=TRACE"`
LogFormat string `env:"LOG_FORMAT" envDefault:"PLAIN" validate:"eq_ignore_case=PLAIN|eq_ignore_case=JSON"`
} }
// Create a new Plug instance type PlugConfig struct {
p := plug.NewPlug(ctx, "localhost", "50051", source) PlugDomain string `env:"PLUG_DOMAIN" envDefault:"e621.net"`
PlugIcon string `env:"PLUG_ICON" envDefault:"https://e621.net/safari-pinned-tab.svg"`
PlugDisplayName string `env:"PLUG_DISPLAY_NAME" envDefault:"e621.net"`
PlugAPIKey string `env:"PLUG_API_KEY,required"`
PlugUsername string `env:"PLUG_USERNAME,required"`
}
// Set the OtterSpace database // LoadConfig loads the configuration from environment variables and validates it.
p.WithOtterSpace(database) func LoadConfig[T any](cfg T) (T, error) {
// Set the task execution function if err := env.Parse(&cfg); err != nil {
p.TaskExecutionFunction(func(ctx context.Context, database database.OtterSpace, sourceUsername string, anthroveUser models.User, deepScrape bool, apiKey string, cancelFunction func()) error { return cfg, fmt.Errorf("config: error parsing configuration: %w", err)
// Your task execution logic here }
})
// Set the send message execution function validate := validator.New()
p.SendMessageExecution(func(ctx context.Context, userSourceID string, message string) error { if err := validate.Struct(cfg); err != nil {
// Your message sending logic here return cfg, fmt.Errorf("config: validation error: %w", err)
}) }
// Start the server return cfg, nil
err := p.Listen() }
if err != nil {
log.Fatalf("Failed to start server: %v", err) ````
### main.go
````go
package main
import (
"context"
log "github.com/sirupsen/logrus"
"your-repo-path/config"
"your-repo-path/internal/service"
"your-repo-path/internal/utils"
"your-repo-path/pkg/database"
"your-repo-path/pkg/models"
"your-repo-path/pkg/plug"
)
// plugConfig holds the orchestrator configuration
var coreConfig config.CoreConfig
var plugConfig config.PlugConfig
var databaseConfig models.DatabaseConfig
// init is used to load the orchestrator configuration
func init() {
// Load the orchestrator configuration
localCoreConfig, err := config.LoadConfig[config.CoreConfig](coreConfig)
if err != nil {
log.Panic(err)
}
coreConfig = localCoreConfig
// Load the plug configuration
localPlugConfig, err := config.LoadConfig[config.PlugConfig](plugConfig)
if err != nil {
log.Panic(err)
}
plugConfig = localPlugConfig
// Load the database configuration
localDatabaseConfig, err := config.LoadConfig[models.DatabaseConfig](databaseConfig)
if err != nil {
log.Panic(err)
}
databaseConfig = localDatabaseConfig
}
func main() {
var err error
var ctx = context.Background()
// Initiate logging setup
utils.SetupLogger(coreConfig.LogLevel, coreConfig.LogFormat)
// Initiate database connection
err = database.Connect(ctx, databaseConfig)
if err != nil {
log.Fatal(err)
}
// Setup Source
source := models.Source{
DisplayName: plugConfig.DisplayName,
Domain: models.SourceDomain(plugConfig.Domain),
Icon: plugConfig.Icon,
}
err = plug.SetupOpenTelemetry(ctx, "your-plug")
if err != nil {
log.Fatal(err)
}
plug.SetTaskExecutionFunction(service.YourTaskFunction)
plug.SetGetMessageExecutionFunction(service.YourMessageFunction)
err = plug.Listen(ctx, ":8080", source)
if err != nil {
panic(err)
}
} }