From 9a1234fa663a534df56b16bb08418640ef4fae32 Mon Sep 17 00:00:00 2001 From: Kamesh Sampath Date: Wed, 1 Feb 2023 15:58:58 +0530 Subject: [PATCH] chore: add kube manifests and revert using echo --- Taskfile.yaml | 26 ++++++++++++++++++++++++++ config/serverfull/deployment.yaml | 24 ++++++++++++++++++++++++ config/serverfull/kustomization.yaml | 4 ++++ config/serverfull/sa.yaml | 4 ++++ config/serverfull/service.yaml | 10 ++++++++++ config/serverless/kustomization.yaml | 3 +++ config/serverless/sa.yaml | 4 ++++ config/serverless/service.yaml | 13 +++++++++++++ go.mod | 3 +++ go.sum | 6 ++++++ server.go | 27 ++++++++++++++++++++++++--- 11 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 Taskfile.yaml create mode 100644 config/serverfull/deployment.yaml create mode 100644 config/serverfull/kustomization.yaml create mode 100644 config/serverfull/sa.yaml create mode 100644 config/serverfull/service.yaml create mode 100644 config/serverless/kustomization.yaml create mode 100644 config/serverless/sa.yaml create mode 100644 config/serverless/service.yaml diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..2878eb5 --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,26 @@ +# https://taskfile.dev + +version: '3' + +dotenv: + - .env + +tasks: + clean: + cmds: + - git reset --soft HEAD~1 + - git tag --delete v0.0.100-next + silent: true + ignore_error: true + tag: + cmds: + - git add . + - git commit -a -m "test and check for kos" + - git tag -a v0.0.100-next -m "test and check for kos" + silent: true + release: + cmds: + - ./goreleaser release --clean --debug + deps: + - clean + - tag \ No newline at end of file diff --git a/config/serverfull/deployment.yaml b/config/serverfull/deployment.yaml new file mode 100644 index 0000000..debef25 --- /dev/null +++ b/config/serverfull/deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hello-world +spec: + selector: + matchLabels: + app: hello-world + template: + metadata: + labels: + app: hello-world + spec: + serviceAccountName: hello-world + containers: + - name: hello-world + image: docker.io/kameshsampath/go-hello-world + resources: + limits: + memory: "50Mi" + cpu: "50m" + ports: + - containerPort: 8080 + name: http diff --git a/config/serverfull/kustomization.yaml b/config/serverfull/kustomization.yaml new file mode 100644 index 0000000..cf0011f --- /dev/null +++ b/config/serverfull/kustomization.yaml @@ -0,0 +1,4 @@ +resources: +- sa.yaml +- service.yaml +- deployment.yaml \ No newline at end of file diff --git a/config/serverfull/sa.yaml b/config/serverfull/sa.yaml new file mode 100644 index 0000000..a380582 --- /dev/null +++ b/config/serverfull/sa.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: hello-world \ No newline at end of file diff --git a/config/serverfull/service.yaml b/config/serverfull/service.yaml new file mode 100644 index 0000000..8cbd5e7 --- /dev/null +++ b/config/serverfull/service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: hello-world +spec: + selector: + app: hello-world + ports: + - port: 8080 + targetPort: http diff --git a/config/serverless/kustomization.yaml b/config/serverless/kustomization.yaml new file mode 100644 index 0000000..4035e12 --- /dev/null +++ b/config/serverless/kustomization.yaml @@ -0,0 +1,3 @@ +resources: +- sa.yaml +- service.yaml \ No newline at end of file diff --git a/config/serverless/sa.yaml b/config/serverless/sa.yaml new file mode 100644 index 0000000..a380582 --- /dev/null +++ b/config/serverless/sa.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: hello-world \ No newline at end of file diff --git a/config/serverless/service.yaml b/config/serverless/service.yaml new file mode 100644 index 0000000..6b39d2b --- /dev/null +++ b/config/serverless/service.yaml @@ -0,0 +1,13 @@ +apiVersion: serving.knative.dev/v1 +kind: Service +metadata: + name: hello-world +spec: + template: + spec: + serviceAccountName: hello-world + containers: + - image: docker.io/kameshsampath/go-hello-world + env: + - name: GREETING_PREFIX + value: "Hello" diff --git a/go.mod b/go.mod index 164b25c..574efb4 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,14 @@ module github.com/kameshsampath/go-hello-world go 1.18 require ( + github.com/caarlos0/env/v7 v7.0.0 github.com/labstack/echo/v4 v4.7.2 github.com/stretchr/testify v1.7.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/labstack/gommon v0.3.1 // indirect github.com/mattn/go-colorable v0.1.11 // indirect github.com/mattn/go-isatty v0.0.14 // indirect @@ -19,5 +21,6 @@ require ( golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect golang.org/x/text v0.3.7 // indirect + golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index c2b0970..fe94ee5 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ +github.com/caarlos0/env/v7 v7.0.0 h1:cyczlTd/zREwSr9ch/mwaDl7Hse7kJuUY8hvHfXu5WI= +github.com/caarlos0/env/v7 v7.0.0/go.mod h1:LPPWniDUq4JaO6Q41vtlyikhMknqymCLBw0eX4dcH1E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/labstack/echo/v4 v4.7.2 h1:Kv2/p8OaQ+M6Ex4eGimg9b9e6icoxA42JSlOR3msKtI= github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks= github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o= @@ -28,6 +32,8 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1U golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE= +golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/server.go b/server.go index 46cf7a5..d974856 100644 --- a/server.go +++ b/server.go @@ -1,20 +1,41 @@ package main import ( + "fmt" "net/http" + "github.com/caarlos0/env/v7" "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) -const greeting = "Hello, World!" +type Greeting struct { + Prefix string `env:"GREETING_PREFIX" envDefault:"Hello"` + Name string `query:"name"` + Message string +} func main() { e := echo.New() - + e.Use(middleware.Logger()) + e.Use(middleware.Recover()) e.GET("/", sayHello) e.Logger.Fatal(e.Start(":8080")) } func sayHello(c echo.Context) error { - return c.String(http.StatusOK, greeting) + var greeting Greeting + err := env.Parse(&greeting) + if err != nil { + return err + } + + err = c.Bind(&greeting) + if err != nil { + greeting.Name = "Anonymous" + } + + greeting.Message = fmt.Sprintf("%s,%s", greeting.Prefix, greeting.Name) + + return c.JSON(http.StatusOK, greeting) }