Quickstart
๐ฆ Installation
go get github.com/BeamFoundry/rays๐ The Minimal Application
Here is a complete example of a Rays application. It’s not really functional but demonstrate the following :
Beamare defined by markerStereotypeinterfaces likeComponent,ServiceandApplication.- Dependency injection is done via struct tag.
- A
PostConstructmethod can be defined on anyBeam. They will be called once an instance is injected. - A custom
Errortype. - A
Runmethod can be defined on anyBeam. It does not need to be on anApplication.
Notice how UserService does not need a constructor taking *Database as an argument.
package main
import (
"github.com/BeamFoundry/rays/pkg/core"
"github.com/BeamFoundry/rays/pkg/lang"
"github.com/BeamFoundry/rays"
)
// 1. Define a Repository
type Database struct {
core.Component // Stereotype
dsn string
}
func (db *Database) New() *Database {
db.dsn = "postgres://localhost/mydb"
return db
}
// 2. Define a Service
type UserService struct {
core.Service //
db *Database `@:"Inject"` // Private field injection!
logger core.Logging `@:"Inject"`
}
func (s *UserService) PostConstruct() lang.Error {
s.logger.Info("UserService ready!","connection", s.db.dsn)
return nil
}
// 3. Define an Application, hosting a Run method.
type BatchUserApplication struct {
core.Application
userService *UserService `@:"Inject"`
logger core.Logging `@:"Inject"`
}
// 4. Define a Run method
func (b *BatchUserApplication) Run(wg *sync.WaitGroup) {
wg.Add(1) // 1. Register this worker with the global WaitGroup
b.logger.Info("BatchUserApplication Run started...")
go func() {
defer wg.Done() // 2. Ensure we decrement the WaitGroup when finished
b.logger.Info("BatchUserApplication is running...")
// Background loop logic goes here...
for {
time.Sleep(10 * time.Second)
}
}()
}
// 5. Boot the Container
func main() {
ctx := rays.ApplicationContext()
if err := ctx.Initialize(); err != nil {
err.LogErrorWithStackTrace()
return
}
ctx.Run() // Runs application lifecycle
}