Examples & Tutorials

Learn how to use Egg with practical code examples.

Hello World Service

The simplest possible Egg service:

go
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
}

Configuration Management

Using configx for environment-based configuration:

go
type AppConfig struct {
    configx.BaseConfig
    APIKey string `env:"API_KEY"`
    Timeout int `env:"TIMEOUT" default:"30"`
}

Database Integration

Using GORM with auto-migration:

go
servicex.Run(ctx,
    servicex.WithAppConfig(cfg),
    servicex.WithAutoMigrate(&model.User{}, &model.Order{}),
    servicex.WithRegister(register),
)

Connect RPC Handler

Creating a Connect RPC service:

go
func register(app *servicex.App) error {
    path, handler := userv1connect.NewUserServiceHandler(
        &userService{},
        connect.WithInterceptors(app.Interceptors()...),
    )
    app.Mux().Handle(path, handler)
    return nil
}