Skip to content

Commit b877255

Browse files
committed
add webapp+registry sample
1 parent 11cbf88 commit b877255

File tree

11 files changed

+407
-0
lines changed

11 files changed

+407
-0
lines changed

apps/webapp-registry/.env.tpl

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
IMAGE_NAME=go-sample
2+
IMAGE_TAG=latest
3+
4+
AZURE_BASE_NAME=go-webapp-tester
5+
AZURE_DEFAULT_LOCATION=westus2
6+
7+
# required for continous container build setup
8+
GH_TOKEN=

apps/webapp-registry/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
out/
2+
.env

apps/webapp-registry/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM golang:1.10.3-stretch
2+
ARG PACKAGE=github.com/Azure-Samples/azure-sdk-for-go-samples/apps/basic_web_app
3+
4+
RUN mkdir /app # for built artifacts
5+
RUN mkdir -p $GOPATH/src/${PACKAGE}
6+
WORKDIR $GOPATH/src/${PACKAGE}
7+
8+
# better for build cache to specify necessary files
9+
ADD ["Gopkg.*","main.go", "./"]
10+
RUN go get -u -v github.com/golang/dep/cmd/dep && dep ensure -v
11+
RUN go build -v -o /app/server .
12+
13+
CMD ["/app/server"]

apps/webapp-registry/Gopkg.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/webapp-registry/Gopkg.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[prune]
2+
go-tests = true
3+
unused-packages = true
4+
5+
[[constraint]]
6+
branch = "master"
7+
name = "golang.org/x/net"

apps/webapp-registry/LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright Microsoft Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a
4+
copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to permit
8+
persons to whom the Software is furnished to do so, subject to the
9+
following conditions:
10+
11+
The above copyright notice and this permission notice shall be included
12+
in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
USE OR OTHER DEALINGS IN THE SOFTWARE.

apps/webapp-registry/Makefile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
PACKAGE = github.com/Azure-Samples/azure-sdk-for-go-samples/apps/basic_web_app
2+
3+
REGISTRY = local
4+
IMAGE = go-sample
5+
TAG = latest
6+
7+
PORT = 8080
8+
IMAGE_URI = $(REGISTRY)/$(IMAGE):$(TAG)
9+
C = $(IMAGE)-tester
10+
HOST = localhost:$(PORT)
11+
12+
binary:
13+
mkdir -p out
14+
go build -o out/server .
15+
./out/server &
16+
curl http://$(HOST)/?name=josh && echo ""
17+
pkill --euid $(USER) --newest --exact server
18+
19+
container:
20+
docker build -t $(IMAGE_URI) .
21+
# docker push $(IMAGE_URI)
22+
docker run -d --rm \
23+
--name $C \
24+
--publish "$(PORT):8080" \
25+
$(IMAGE_URI)
26+
curl "http://$(HOST)/?name=josh" && echo ""
27+
docker container logs $C
28+
docker container stop $C
29+
echo "start a new container with"
30+
echo " \`docker run [-d|-it] -p $(PORT):8080 $(IMAGE_URI)\`"
31+
32+
.PHONY: binary container

apps/webapp-registry/README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Web App
2+
3+
Continuously build and deploy a Go web app using Azure App Service and Azure
4+
Container Registry.
5+
6+
This package includes a basic Go web app and scripts to set up infrastructure
7+
for it in Azure.
8+
9+
Requires [Azure CLI][].
10+
11+
## Try It!
12+
13+
Set configuration in a local .env file in the package root by copying
14+
`.env.tpl` to `.env`. Change `AZURE_BASE_NAME` to something relatively unique
15+
to you; for example you might include your name. Then run ./[setup.sh][] to
16+
build and deploy your container to Container Registry and App Service.
17+
18+
If run within the Azure Go SDK samples repo, this will carry out a one-off
19+
build and push to your registry, which will trigger refresh of the App Service
20+
Web App. If you stick with the script defaults, you can visit your app at
21+
`https://${AZURE_BASE_NAME}-webapp.azurewebsites.net/`.
22+
23+
### Continuous Build and Deploy
24+
25+
Follow these steps to set up continuous build and deploy:
26+
27+
1. Copy the contents of this package to your own fresh git repo and push it to GitHub.
28+
2. Specify an image name in env var `IMAGE_NAME` (e.g. in `.env`) that matches
29+
your GitHub 'org/repo' structure.
30+
3. Run `./setup.sh`. It will arrange continuous build and deploy for you from
31+
the specified repo/image name.
32+
33+
**NOTE**: Container Registry Build requires a [GitHub personal access
34+
token](https://github.com/settings/tokens); you need to get one from the linked
35+
page and set it in a local environment variable `GH_TOKEN`, e.g. `export
36+
GH_TOKEN=mylongtokenstring`. You can also add it to your local `.env` file for
37+
persistence.
38+
39+
To test continuous integration, now make a change and `git push` it to your
40+
repo. The Container Registry build task should detect the change, rebuild your
41+
container, and notify App Service; which should then refresh and reload your
42+
container image and app.
43+
44+
## More Details
45+
46+
[setup.sh][] ensures an Azure resource group, container registry, app service
47+
plan, and container-based web app are provisioned and connected in the
48+
subscription currently logged in to [Azure CLI][].
49+
50+
It uses the following environment variables to choose names:
51+
52+
* IMAGE\_NAME: Name of container image (aka "repo").
53+
* IMAGE\_TAG: Tag for container image.
54+
* AZURE\_BASE\_NAME: Prefix for Azure resources.
55+
* AZURE\_DEFAULT\_LOCATION: Location for Azure resources.
56+
57+
These names can be specified in a .env file in the root of the package. If a
58+
`.env` file isn't found, `.env.tpl` is copied to `.env` and used.
59+
60+
Explicit parameters can also be passed, see comments at beginning of
61+
[setup.sh][] for details.
62+
63+
[Azure CLI]: https://github.com/Azure/azure-cli
64+
[setup.sh]: ./setup.sh

apps/webapp-registry/main.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"golang.org/x/net/html"
6+
"log"
7+
"net/http"
8+
"os"
9+
)
10+
11+
func main() {
12+
// find port selected by service, default to 8080
13+
var port string
14+
var found bool
15+
port, found = os.LookupEnv("PORT")
16+
if found == true {
17+
// prepend a colon
18+
port = fmt.Sprintf(":%s", port)
19+
} else {
20+
port = ":8080"
21+
}
22+
23+
http.HandleFunc("/", tester)
24+
25+
log.Fatal(http.ListenAndServe(port, nil))
26+
}
27+
28+
// tester
29+
func tester(w http.ResponseWriter, r *http.Request) {
30+
fmt.Fprintf(w, "Hello %s!", html.EscapeString(
31+
r.URL.Query().Get("name")))
32+
log.Printf("request received, details follow:\n%+v\n", r)
33+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function ensure_group () {
2+
local group_name=$1
3+
local group_id
4+
5+
group_id=$(az group show --name $group_name --query 'id' --output tsv 2> /dev/null)
6+
7+
if [[ -z $group_id ]]; then
8+
group_id=$(az group create \
9+
--name $group_name \
10+
--location $location \
11+
--query 'id' --output tsv)
12+
fi
13+
echo $group_id
14+
}
15+

0 commit comments

Comments
 (0)