Skip to content

Commit

Permalink
add webapp+registry sample
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgav committed Aug 14, 2018
1 parent 11cbf88 commit 04b90c5
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/basic_web_app/.env.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMAGE_NAME=go-sample
IMAGE_TAG=latest

AZURE_BASE_NAME=go-webapp-tester
AZURE_DEFAULT_LOCATION=westus2

# required for continous container build setup
GH_TOKEN=
2 changes: 2 additions & 0 deletions apps/basic_web_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out/
.env
13 changes: 13 additions & 0 deletions apps/basic_web_app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.10.3-stretch
ARG PACKAGE=github.com/Azure-Samples/azure-sdk-for-go-samples/apps/basic_web_app

RUN mkdir /app # for built artifacts
RUN mkdir -p $GOPATH/src/${PACKAGE}
WORKDIR $GOPATH/src/${PACKAGE}

# better for build cache to specify necessary files
ADD ["Gopkg.*","main.go", "./"]
RUN go get -u -v github.com/golang/dep/cmd/dep && dep ensure -v
RUN go build -v -o /app/server .

CMD ["/app/server"]
20 changes: 20 additions & 0 deletions apps/basic_web_app/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions apps/basic_web_app/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "golang.org/x/net"
20 changes: 20 additions & 0 deletions apps/basic_web_app/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions apps/basic_web_app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
PACKAGE = github.com/Azure-Samples/azure-sdk-for-go-samples/apps/basic_web_app

REGISTRY = local
IMAGE = go-sample
TAG = latest

PORT = 8080
IMAGE_URI = $(REGISTRY)/$(IMAGE):$(TAG)
C = $(IMAGE)-tester
HOST = localhost:$(PORT)

binary:
mkdir -p out
go build -o out/server .
./out/server &
curl http://$(HOST)/?name=josh && echo ""
pkill --euid $(USER) --newest --exact server

container:
docker build -t $(IMAGE_URI) .
# docker push $(IMAGE_URI)
docker run -d --rm \
--name $C \
--publish "$(PORT):8080" \
$(IMAGE_URI)
curl "http://$(HOST)/?name=josh" && echo ""
docker container logs $C
docker container stop $C
echo "start a new container with"
echo " \`docker run [-d|-it] -p $(PORT):8080 $(IMAGE_URI)\`"

.PHONY: binary container
64 changes: 64 additions & 0 deletions apps/basic_web_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Web App

Continuously build and deploy a Go web app using Azure App Service and Azure
Container Registry.

This package includes a basic Go web app and scripts to set up infrastructure
for it in Azure.

Requires [Azure CLI][].

## Try It!

Set configuration in a local .env file in the package root by copying
`.env.tpl` to `.env`. Change `AZURE_BASE_NAME` to something relatively unique
to you; for example you might include your name. Then run ./[setup.sh][] to
build and deploy your container to Container Registry and App Service.

If run within the Azure Go SDK samples repo, this will carry out a one-off
build and push to your registry, which will trigger refresh of the App Service
Web App. If you stick with the script defaults, you can visit your app at
`https://${AZURE_BASE_NAME}-webapp.azurewebsites.net/`.

### Continuous Build and Deploy

Follow these steps to set up continuous build and deploy:

1. Copy the contents of this package to your own fresh git repo and push it to GitHub.
2. Specify an image name in env var `IMAGE_NAME` (e.g. in `.env`) that matches
your GitHub 'org/repo' structure.
3. Run `./setup.sh`. It will arrange continuous build and deploy for you from
the specified repo/image name.

**NOTE**: Container Registry Build requires a [GitHub personal access
token](https://github.com/settings/tokens); you need to get one from the linked
page and set it in a local environment variable `GH_TOKEN`, e.g. `export
GH_TOKEN=mylongtokenstring`. You can also add it to your local `.env` file for
persistence.

To test continuous integration, now make a change and `git push` it to your
repo. The Container Registry build task should detect the change, rebuild your
container, and notify App Service; which should then refresh and reload your
container image and app.

## More Details

[setup.sh][] ensures an Azure resource group, container registry, app service
plan, and container-based web app are provisioned and connected in the
subscription currently logged in to [Azure CLI][].

It uses the following environment variables to choose names:

* IMAGE\_NAME: Name of container image (aka "repo").
* IMAGE\_TAG: Tag for container image.
* AZURE\_BASE\_NAME: Prefix for Azure resources.
* AZURE\_DEFAULT\_LOCATION: Location for Azure resources.

These names can be specified in a .env file in the root of the package. If a
`.env` file isn't found, `.env.tpl` is copied to `.env` and used.

Explicit parameters can also be passed, see comments at beginning of
[setup.sh][] for details.

[Azure CLI]: https://github.com/Azure/azure-cli
[setup.sh]: ./setup.sh
33 changes: 33 additions & 0 deletions apps/basic_web_app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"golang.org/x/net/html"
"log"
"net/http"
"os"
)

func main() {
// find port selected by service, default to 8080
var port string
var found bool
port, found = os.LookupEnv("PORT")
if found == true {
// prepend a colon
port = fmt.Sprintf(":%s", port)
} else {
port = ":8080"
}

http.HandleFunc("/", tester)

log.Fatal(http.ListenAndServe(port, nil))
}

// tester
func tester(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s!", html.EscapeString(
r.URL.Query().Get("name")))
log.Printf("request received, details follow:\n%+v\n", r)
}
15 changes: 15 additions & 0 deletions apps/basic_web_app/scripts/rm_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function ensure_group () {
local group_name=$1
local group_id

group_id=$(az group show --name $group_name --query 'id' --output tsv 2> /dev/null)

if [[ -z $group_id ]]; then
group_id=$(az group create \
--name $group_name \
--location $location \
--query 'id' --output tsv)
fi
echo $group_id
}

Loading

0 comments on commit 04b90c5

Please sign in to comment.