Skip to content

Instantly share code, notes, and snippets.

@slayerjain
Created August 3, 2022 13:06
Show Gist options
  • Save slayerjain/08e4df24e261f9d16f3c8355a373a2b3 to your computer and use it in GitHub Desktop.
Save slayerjain/08e4df24e261f9d16f3c8355a373a2b3 to your computer and use it in GitHub Desktop.
keploy fasthttp sample server
package main
import (
"flag"
"fmt"
"log"
"github.com/keploy/go-sdk/integrations/kfasthttp"
"github.com/keploy/go-sdk/keploy"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)
func main() {
flag.Parse()
k := keploy.New(keploy.Config{
App: keploy.AppConfig{
Name: "fasthttp-app",
Port: "8080",
},
Server: keploy.ServerConfig{
URL: "http://localhost:8081/api",
},
})
mw := kfasthttp.FastHttpMiddlware(k)
h := requestHandler
if *compress {
h = fasthttp.CompressHandler(h)
}
if err := fasthttp.ListenAndServe(*addr, mw(h)); err != nil {
log.Fatalf("Error in ListenAndServe: %v", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!\n\n")
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)
ctx.SetContentType("text/plain; charset=utf8")
// Set arbitrary headers
ctx.Response.Header.Set("X-My-Header", "my-header-value")
// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
ctx.Response.Header.SetCookie(&c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment