Skip to content

Instantly share code, notes, and snippets.

@elliotforbes
Created January 19, 2018 21:16
Show Gist options
  • Save elliotforbes/b627447fc2a94024a3fba1e9068a6b35 to your computer and use it in GitHub Desktop.
Save elliotforbes/b627447fc2a94024a3fba1e9068a6b35 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net"
time "time"
"github.com/aws/aws-lambda-go/lambda"
)
type Request struct {
Url string `json:"url"`
}
type Response struct {
Message string `json:"message"`
Time time.Duration `json:"time"`
}
func calcTime(url string) time.Duration {
conn, err := net.Dial("tcp", url + ":80")
if err != nil {
panic(err)
}
defer conn.Close()
conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
start := time.Now()
oneByte := make([]byte, 1)
_, err = conn.Read(oneByte)
if err != nil {
panic(err)
}
log.Println("First Byte: ", time.Since(start))
return time.Since(start)
}
func Handler(request Request) (Response, error) {
return Response {
Message: "success",
Time: calcTime(request.Url),
}, nil
}
func main() {
lambda.Start(Handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment