goten-core
goten-core is the runtime library for Go microservices generated by ssgo. It provides production-ready components for logging, metrics, tracing, and more.
Installation
bash
go get github.com/ssgohq/goten-corePackages
app
Application lifecycle management for graceful startup and shutdown.
go
import "github.com/ssgohq/goten-core/app"
func main() {
application := app.New()
application.Run()
}lifecycle
Service lifecycle adapters for registering components that need startup/shutdown coordination.
go
import "github.com/ssgohq/goten-core/lifecycle"
// Register an adapter (e.g., HTTP server)
lifecycle.Register(myHTTPServer)
// Start all registered services
lifecycle.StartAll()
// Graceful shutdown on SIGTERM
lifecycle.WaitForShutdown()logx
Structured logging based on Zap with context propagation.
go
import "github.com/ssgohq/goten-core/logx"
// Initialize logger
logx.Init(logx.Config{
Level: "info",
Format: "json",
})
// Use logger
logx.Info("user created", logx.String("userId", "123"))
logx.Error("failed to process", logx.Error(err))
// Context-aware logging
logx.WithContext(ctx).Info("request processed")metric
Prometheus metrics for monitoring.
go
import "github.com/ssgohq/goten-core/metric"
// Start metrics server
metric.StartServer(metric.Config{
Host: "0.0.0.0",
Port: 9090,
Path: "/metrics",
})
// Define counters
requestCounter := metric.NewCounter("http_requests_total", "Total HTTP requests")
requestCounter.Inc()
// Define histograms
latencyHistogram := metric.NewHistogram("http_request_duration_seconds", "Request duration")
latencyHistogram.Observe(0.123)middleware
HTTP middleware for Hertz servers.
go
import "github.com/ssgohq/goten-core/middleware"
// CORS middleware
h.Use(middleware.Cors(middleware.CorsConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
}))
// Request logging
h.Use(middleware.Logging())
// JWT authentication
h.Use(middleware.JWT(middleware.JWTConfig{
SecretKey: "your-secret-key",
}))srpc
RPC client and server configuration for Kitex.
go
import "github.com/ssgohq/goten-core/srpc"
// Server configuration
serverConf := srpc.ServerConfig{
Name: "user-rpc",
Host: "0.0.0.0",
Port: 9000,
Registry: srpc.RegistryConfig{
Type: "consul",
Address: "localhost:8500",
},
}
// Client configuration
clientConf := srpc.ClientConfig{
ServiceName: "user-rpc",
Registry: srpc.RegistryConfig{
Type: "consul",
Address: "localhost:8500",
},
}stores
Database connection utilities.
PostgreSQL
go
import "github.com/ssgohq/goten-core/stores/postgres"
db, err := postgres.NewDB(postgres.Config{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "password",
DBName: "mydb",
SSLMode: "disable",
})MySQL
go
import "github.com/ssgohq/goten-core/stores/mysql"
db, err := mysql.NewDB(mysql.Config{
Host: "localhost",
Port: 3306,
User: "root",
Password: "password",
DBName: "mydb",
})Redis
go
import "github.com/ssgohq/goten-core/stores/redis"
rdb := redis.NewClient(redis.Config{
Host: "localhost",
Port: 6379,
Password: "",
DB: 0,
})trace
OpenTelemetry tracing integration.
go
import "github.com/ssgohq/goten-core/trace"
// Initialize tracer
trace.Init(trace.Config{
ServiceName: "my-service",
Exporter: "otlp",
Endpoint: "localhost:4318",
})
defer trace.Shutdown()
// Create spans
ctx, span := trace.Start(ctx, "operation-name")
defer span.End()Configuration
Generated services use YAML configuration that maps to goten-core types:
yaml
# etc/config.yaml (RPC service)
name: user-rpc
host: 0.0.0.0
port: 9000
registry:
type: consul
address: localhost:8500
log:
level: info
format: json
metric:
enabled: true
port: 9090
trace:
enabled: true
exporter: otlp
endpoint: localhost:4318
postgres:
host: localhost
port: 5432
user: postgres
password: password
database: mydb
redis:
host: localhost
port: 6379yaml
# etc/api.yaml (HTTP service)
name: api-gateway
host: 0.0.0.0
port: 8080
log:
level: info
format: json
metric:
enabled: true
port: 9091
trace:
enabled: true
exporter: otlp
endpoint: localhost:4318
jwt:
secret_key: your-secret-key
expire: 24hRelated Documentation
- API Commands - Generate HTTP services
- RPC Commands - Generate RPC services
- Database Commands - Generate database code