Created
June 26, 2023 18:22
-
-
Save pioh/85cfaa6736cdd1b11a2e0e05c7413e6b to your computer and use it in GitHub Desktop.
task runner2
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
package main | |
import ( | |
"github.com/gin-gonic/gin" | |
"log" | |
"time" | |
) | |
func main() { | |
g := gin.Default() | |
g.Handle("GET", "/task", handler) | |
if err := g.Run(":8080"); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func handler(ctx *gin.Context) { | |
q := struct { | |
Duration time.Duration `form:"duration"` | |
Delay time.Duration `form:"delay"` | |
Message string `form:"message"` | |
}{} | |
if err := ctx.BindQuery(&q); err != nil { | |
ctx.String(500, err.Error()) | |
return | |
} | |
time.AfterFunc(q.Delay, func() { | |
log.Printf("task %v started", q.Message) | |
time.Sleep(q.Duration) | |
log.Printf("task %v finished", q.Message) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment