Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions cmd/dbos/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func runInit(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
projectName = args[0]
} else {
projectName = "dbos-toolbox"
projectName = "dbos-go-starter"
}

// Check if directory already exists
Expand All @@ -45,9 +45,10 @@ func runInit(cmd *cobra.Command, args []string) error {

// Process and write each template file
templates := map[string]string{
"templates/dbos-toolbox/go.mod.tmpl": "go.mod",
"templates/dbos-toolbox/main.go.tmpl": "main.go",
"templates/dbos-toolbox/dbos-config.yaml.tmpl": "dbos-config.yaml",
"templates/dbos-go-starter/go.mod.tmpl": "go.mod",
"templates/dbos-go-starter/main.go.tmpl": "main.go",
"templates/dbos-go-starter/dbos-config.yaml.tmpl": "dbos-config.yaml",
"templates/dbos-go-starter/app.html": "html/app.html",
}

for tmplPath, outputFile := range templates {
Expand All @@ -70,6 +71,9 @@ func runInit(cmd *cobra.Command, args []string) error {

// Write output file
outputPath := filepath.Join(projectName, outputFile)
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
return fmt.Errorf("failed to create directory for %s: %w", outputFile, err)
}
Comment on lines +74 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? os.Write will create the folders is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in templates map, we have this entry:

"templates/dbos-go-starter/app.html": "html/app.html",

when outputFile is "html/app.html", filepath.Dir(outputPath) will resolve to "dbos-go-starter/html". The os.MkdirAll call then ensures that the html subdirectory is created inside the main project directory before os.WriteFile attempts to create app.html within it

so without the os.MkdirAll, we will get:

./dbos init
Error: failed to write html/app.html: open dbos-go-starter/html/app.html: no such file or directory

if err := os.WriteFile(outputPath, buf.Bytes(), 0644); err != nil {
return fmt.Errorf("failed to write %s: %w", outputFile, err)
}
Expand All @@ -79,7 +83,7 @@ func runInit(cmd *cobra.Command, args []string) error {
fmt.Println("To get started:")
fmt.Printf(" cd %s\n", projectName)
fmt.Println(" go mod tidy")
fmt.Println(" export DBOS_SYSTEM_DATABASE_URL=<your-database-url>")
fmt.Println(" export DBOS_SYSTEM_DATABASE_URL=\"postgres://<user>:<password>@<host>:<port>/<db>\"")
fmt.Println(" go run main.go")

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/dbos/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import (
"embed"
)

//go:embed templates/dbos-toolbox/*
var templateFS embed.FS
//go:embed templates/dbos-go-starter/*
var templateFS embed.FS
340 changes: 340 additions & 0 deletions cmd/dbos/templates/dbos-go-starter/app.html

Large diffs are not rendered by default.

155 changes: 155 additions & 0 deletions cmd/dbos/templates/dbos-go-starter/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/dbos-inc/dbos-transact-golang/dbos"
"github.com/gin-gonic/gin"
)

const STEPS_EVENT = "steps_event"

var dbosCtx dbos.DBOSContext

/*****************************/
/**** WORKFLOWS AND STEPS ****/
/*****************************/

func ExampleWorkflow(ctx dbos.DBOSContext, _ string) (string, error) {
_, err := dbos.RunAsStep(ctx, func(stepCtx context.Context) (string, error) {
return stepOne(stepCtx)
})
if err != nil {
return "", err
}
err = dbos.SetEvent(ctx, STEPS_EVENT, 1)
if err != nil {
return "", err
}
_, err = dbos.RunAsStep(ctx, func(stepCtx context.Context) (string, error) {
return stepTwo(stepCtx)
})
if err != nil {
return "", err
}
err = dbos.SetEvent(ctx, STEPS_EVENT, 2)
if err != nil {
return "", err
}
_, err = dbos.RunAsStep(ctx, func(stepCtx context.Context) (string, error) {
return stepThree(stepCtx)
})
if err != nil {
return "", err
}
err = dbos.SetEvent(ctx, STEPS_EVENT, 3)
if err != nil {
return "", err
}
return "Workflow completed", nil
}

func stepOne(ctx context.Context) (string, error) {
time.Sleep(5 * time.Second)
fmt.Println("Step one completed!")
return "Step 1 completed", nil
}

func stepTwo(ctx context.Context) (string, error) {
time.Sleep(5 * time.Second)
fmt.Println("Step two completed!")
return "Step 2 completed", nil
}

func stepThree(ctx context.Context) (string, error) {
time.Sleep(5 * time.Second)
fmt.Println("Step three completed!")
return "Step 3 completed", nil
}

/*****************************/
/**** Main Function **********/
/*****************************/

func main() {
// Create DBOS context
var err error
dbosCtx, err = dbos.NewDBOSContext(context.Background(), dbos.Config{
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
AppName: "{{.ProjectName}}",
AdminServer: true,
})
if err != nil {
panic(err)
}

// Register workflows
dbos.RegisterWorkflow(dbosCtx, ExampleWorkflow)

// Launch DBOS
err = dbosCtx.Launch()
if err != nil {
panic(err)
}
defer dbosCtx.Shutdown(10 * time.Second)

// Initialize Gin router
router := gin.Default()

// HTTP Handlers
router.StaticFile("/", "./html/app.html")
router.GET("/workflow/:taskid", workflowHandler)
router.GET("/last_step/:taskid", lastStepHandler)
router.POST("/crash", crashHandler)

fmt.Println("Server starting on http://localhost:8080")
err = router.Run(":8080")
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}

/*****************************/
/**** HTTP HANDLERS **********/
/*****************************/

func workflowHandler(c *gin.Context) {
taskID := c.Param("taskid")

if taskID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Task ID is required"})
return
}

_, err := dbos.RunWorkflow(dbosCtx, ExampleWorkflow, "", dbos.WithWorkflowID(taskID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}

func lastStepHandler(c *gin.Context) {
taskID := c.Param("taskid")

if taskID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Task ID is required"})
return
}

step, err := dbos.GetEvent[int](dbosCtx, taskID, STEPS_EVENT, 0)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.String(http.StatusOK, fmt.Sprintf("%d", step))
}

// This endpoint crashes the application. For demonstration purposes only :)
func crashHandler(c *gin.Context) {
os.Exit(1)
}
Loading
Loading