Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit f41317f

Browse files
author
Chandra Pratap
authored
added repo for go template (#268)
* added repo for go template * updated twitter link
1 parent 1173e35 commit f41317f

25 files changed

+763
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM golang:latest
2+
RUN mkdir /app
3+
ADD . /app/
4+
WORKDIR /app
5+
RUN go get -d
6+
RUN go build -o main .
7+
CMD ["/app/main"]
8+
EXPOSE 80
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Microsoft Corporation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
| Language | Platform | Author |
2+
| -------- | --------|--------|
3+
| Go Lang | Azure Web App, Virtual Machine, AKS| |
4+
5+
# Sample Go web application
6+
7+
This is a sample GO Lang web application that you can deploy to Azure.
8+
9+
10+
## License:
11+
12+
See [LICENSE](LICENSE).
13+
14+
## Contributing
15+
16+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
17+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"html/template"
6+
"log"
7+
"net/http"
8+
"os"
9+
"github.com/Microsoft/ApplicationInsights-Go/appinsights"
10+
)
11+
12+
type PageVars struct {
13+
Message string
14+
Language string
15+
}
16+
17+
func main() {
18+
client := appinsights.NewTelemetryClient(os.Getenv("APPINSIGHTS_INSTRUMENTATIONKEY"))
19+
request := appinsights.NewRequestTelemetry("GET", "https://myapp.azurewebsites.net/", 1 , "Success")
20+
client.Track(request)
21+
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
22+
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("img"))))
23+
http.Handle("/fonts/", http.StripPrefix("/fonts/", http.FileServer(http.Dir("fonts"))))
24+
http.HandleFunc("/", Home)
25+
log.Fatal(http.ListenAndServe(getPort(), nil))
26+
}
27+
28+
func getPort() string {
29+
p := os.Getenv("HTTP_PLATFORM_PORT")
30+
if p != "" {
31+
return ":" + p
32+
}
33+
return ":80"
34+
}
35+
36+
func render(w http.ResponseWriter, tmpl string, pageVars PageVars) {
37+
38+
tmpl = fmt.Sprintf("views/%s", tmpl)
39+
t, err := template.ParseFiles(tmpl)
40+
41+
if err != nil { // if there is an error
42+
log.Print("template parsing error: ", err) // log it
43+
}
44+
45+
err = t.Execute(w, pageVars) //execute the template and pass in the variables to fill the gaps
46+
47+
if err != nil { // if there is an error
48+
log.Print("template executing error: ", err) //log it
49+
}
50+
}
51+
52+
func Home(w http.ResponseWriter, req *http.Request) {
53+
pageVars := PageVars{
54+
Message: "Success!",
55+
Language: "Go Lang",
56+
}
57+
render(w, "index.html", pageVars)
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
description: A Helm chart for Kubernetes
3+
name: sampleapp
4+
version: v0.2.0
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1. Get the application URL by running these commands:
2+
{{- if .Values.ingress.enabled }}
3+
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $.Values.ingress.hostname }}{{ $.Values.ingress.path }}
4+
{{- else }}
5+
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
6+
You can watch the status of by running 'kubectl get svc -w {{ template "sampleapp.fullname" . }}'
7+
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "sampleapp.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
8+
echo http://$SERVICE_IP:{{ .Values.service.port }}
9+
{{- end }}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "sampleapp.name" -}}
6+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7+
{{- end -}}
8+
9+
{{/*
10+
Create a default fully qualified app name.
11+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12+
If release name contains chart name it will be used as a full name.
13+
*/}}
14+
{{- define "sampleapp.fullname" -}}
15+
{{- if .Values.fullnameOverride -}}
16+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17+
{{- else -}}
18+
{{- $name := default .Chart.Name .Values.nameOverride -}}
19+
{{- if contains $name .Release.Name -}}
20+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21+
{{- else -}}
22+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23+
{{- end -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{/*
28+
Create chart name and version as used by the chart label.
29+
*/}}
30+
{{- define "sampleapp.chart" -}}
31+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32+
{{- end -}}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ template "sampleapp.fullname" . }}
5+
labels:
6+
app: {{ template "sampleapp.name" . }}
7+
chart: {{ template "sampleapp.chart" . }}
8+
release: {{ .Release.Name }}
9+
heritage: {{ .Release.Service }}
10+
spec:
11+
replicas: {{ .Values.replicaCount }}
12+
selector:
13+
matchLabels:
14+
app: {{ template "sampleapp.name" . }}
15+
release: {{ .Release.Name }}
16+
template:
17+
metadata:
18+
labels:
19+
app: {{ template "sampleapp.name" . }}
20+
release: {{ .Release.Name }}
21+
spec:
22+
containers:
23+
- name: {{ .Chart.Name }}
24+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
25+
imagePullPolicy: {{ .Values.image.pullPolicy }}
26+
ports:
27+
- name: http
28+
containerPort: {{ .Values.service.port }}
29+
protocol: TCP
30+
livenessProbe:
31+
httpGet:
32+
path: /
33+
port: http
34+
initialDelaySeconds: 30
35+
readinessProbe:
36+
httpGet:
37+
path: /
38+
port: http
39+
initialDelaySeconds: 30
40+
env:
41+
- name: APPINSIGHTS_INSTRUMENTATIONKEY
42+
value: {{ .Values.applicationInsights.InstrumentationKey }}
43+
{{- $root := . }}
44+
{{- range $ref, $values := .Values.secrets }}
45+
{{- range $key, $value := $values }}
46+
- name: {{ $ref | upper }}_{{ $key | upper }}
47+
valueFrom:
48+
secretKeyRef:
49+
name: {{ template "sampleapp.fullname" $root }}-{{ $ref }}
50+
key: {{ $key }}
51+
{{- end }}
52+
{{- end }}
53+
resources:
54+
{{ toYaml .Values.resources | indent 12 }}
55+
{{- if .Values.imagePullSecrets }}
56+
imagePullSecrets:
57+
{{- range .Values.imagePullSecrets }}
58+
- name: {{ . }}
59+
{{- end}}
60+
{{- end }}
61+
{{- with .Values.nodeSelector }}
62+
nodeSelector:
63+
{{ toYaml . | indent 8 }}
64+
{{- end }}
65+
{{- with .Values.affinity }}
66+
affinity:
67+
{{ toYaml . | indent 8 }}
68+
{{- end }}
69+
{{- with .Values.tolerations }}
70+
tolerations:
71+
{{ toYaml . | indent 8 }}
72+
{{- end }}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{{- if .Values.ingress.enabled -}}
2+
{{- $fullName := include "sampleapp.fullname" . -}}
3+
{{- $serviceName := include "sampleapp.name" . -}}
4+
{{- $servicePort := .Values.service.port -}}
5+
apiVersion: extensions/v1beta1
6+
kind: Ingress
7+
metadata:
8+
name: {{ $fullName }}
9+
labels:
10+
app: {{ $serviceName }}
11+
release: {{ .Release.Name }}
12+
heritage: {{ .Release.Service }}
13+
{{- with .Values.ingress.annotations }}
14+
annotations:
15+
{{ toYaml . | indent 4 }}
16+
{{- end }}
17+
spec:
18+
{{- if .Values.ingress.tls }}
19+
tls:
20+
{{- range .Values.ingress.tls }}
21+
- hosts:
22+
{{- range .hosts }}
23+
- {{ . }}
24+
{{- end }}
25+
secretName: {{ .secretName }}
26+
{{- end }}
27+
{{- end }}
28+
rules:
29+
- host: {{ .Values.ingress.hostname | quote }}
30+
http:
31+
paths:
32+
- path: /
33+
backend:
34+
serviceName: {{ $serviceName }}
35+
servicePort: {{ $servicePort }}
36+
{{- end }}

0 commit comments

Comments
 (0)