Created
January 19, 2018 21:16
-
-
Save elliotforbes/b627447fc2a94024a3fba1e9068a6b35 to your computer and use it in GitHub Desktop.
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 ( | |
"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