49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package web
|
|
|
|
import (
|
|
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go/pkg/loic"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func Start() {
|
|
r := gin.Default()
|
|
r.LoadHTMLGlob("pkg/web/templates/*")
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
status := "stopped"
|
|
if loic.IsTestRunning() {
|
|
status = "running"
|
|
}
|
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
|
"Status": status,
|
|
})
|
|
})
|
|
|
|
r.POST("/start", func(c *gin.Context) {
|
|
if c.Request.Method != http.MethodPost {
|
|
http.Error(c.Writer, "Invalid request method", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
targetURL := c.PostForm("target_url")
|
|
concurrency, _ := strconv.Atoi(c.PostForm("concurrency"))
|
|
duration, _ := time.ParseDuration(c.PostForm("duration"))
|
|
rampUp, _ := time.ParseDuration(c.PostForm("ramp_up"))
|
|
|
|
go loic.StartTest(targetURL, concurrency, duration, rampUp)
|
|
c.Redirect(http.StatusSeeOther, "/")
|
|
})
|
|
|
|
r.POST("/stop", func(c *gin.Context) {
|
|
if c.Request.Method != http.MethodPost {
|
|
http.Error(c.Writer, "Invalid request method", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
loic.StopTest()
|
|
c.Redirect(http.StatusSeeOther, "/")
|
|
})
|
|
|
|
r.Run(":8080")
|
|
}
|