Egg Framework
Learn how to use Egg with practical code examples.
The simplest possible Egg service:
package main
import (
"context"
"go.eggybyte.com/egg/servicex"
)
func main() {
ctx := context.Background()
servicex.Run(ctx,
servicex.WithService("hello-service", "1.0.0"),
servicex.WithRegister(register),
)
}
func register(app *servicex.App) error {
app.Mux().HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, Egg!"))
})
return nil
}
Using configx for environment-based configuration:
type AppConfig struct {
configx.BaseConfig
APIKey string `env:"API_KEY"`
Timeout int `env:"TIMEOUT" default:"30"`
}
Using GORM with auto-migration:
servicex.Run(ctx,
servicex.WithAppConfig(cfg),
servicex.WithAutoMigrate(&model.User{}, &model.Order{}),
servicex.WithRegister(register),
)
Creating a Connect RPC service:
func register(app *servicex.App) error {
path, handler := userv1connect.NewUserServiceHandler(
&userService{},
connect.WithInterceptors(app.Interceptors()...),
)
app.Mux().Handle(path, handler)
return nil
}