Skip to content

Instantly share code, notes, and snippets.

@apzuk3
Last active August 18, 2019 11:39
Show Gist options
  • Save apzuk3/e9f2cf48982fa4aab645e3456b655fc5 to your computer and use it in GitHub Desktop.
Save apzuk3/e9f2cf48982fa4aab645e3456b655fc5 to your computer and use it in GitHub Desktop.
DevOps preparation sample code
package main
import (
"flag"
"fmt"
"os"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
mode := flag.String("mode", "", "server mode")
flag.Parse()
fmt.Println(*mode)
if *mode == "web" {
fmt.Println("Start web")
startWeb()
} else {
fmt.Println("Start lambda")
startLambda()
}
}
func startLambda() {
lambda.Start(func(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: getContent(),
}, nil
})
}
func startWeb() {
port := os.Getenv("port")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
switch request.URL.Path {
default:
writer.WriteHeader(http.StatusNotFound)
case "/":
_, _ = fmt.Fprint(writer, getContent())
case "/health":
writer.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(writer, "healthy")
}
})
_ = http.ListenAndServe(":" + port, nil)
}
func getContent() string {
return "Hello, DevOps!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment