Skip to content

Instantly share code, notes, and snippets.

@selfboot
Created August 7, 2023 03:19
Show Gist options
  • Save selfboot/7d45051f39785adc6f46a92eb585af43 to your computer and use it in GitHub Desktop.
Save selfboot/7d45051f39785adc6f46a92eb585af43 to your computer and use it in GitHub Desktop.
A simple gin server reads the requested package content and outputs random content.
package main
import (
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
"time"
)
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func MeshCall(meshPath string, c *gin.Context) {
start := time.Now()
uinStr := c.Query("uin")
uin, err := strconv.ParseUint(uinStr, 10, 64)
if err != nil {
c.Status(http.StatusBadRequest)
return
}
body, _ := ioutil.ReadAll(c.Request.Body)
log.Println("Request Body: ", string(body))
c.Status(http.StatusOK)
c.Writer.Header().Add("code", strconv.FormatInt(int64(uin), 10))
// Generate a 1MB random string
randomStr := StringWithCharset(1024*1024, charset)
c.Writer.Write([]byte(randomStr))
log.Printf("Request processed in %s\n", time.Since(start))
}
func main() {
router := gin.Default()
router.POST("/meshcall", func(c *gin.Context) {
MeshCall("dummy", c)
})
router.Run(":8089")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment