-
Notifications
You must be signed in to change notification settings - Fork 41
Starter examples: replace dbos-toolbox with dbos-go-starter #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: "dbos-toolbox", | ||
ngnhng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Writewill create the folders is neededThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in
templatesmap, we have this entry:when
outputFileis "html/app.html",filepath.Dir(outputPath)will resolve to "dbos-go-starter/html". Theos.MkdirAllcall then ensures that the html subdirectory is created inside the main project directory beforeos.WriteFileattempts to createapp.htmlwithin itso without the
os.MkdirAll, we will get: