Skip to content

Commit 700c5e6

Browse files
committed
add go-beego
1 parent 6c07408 commit 700c5e6

File tree

9 files changed

+336
-0
lines changed

9 files changed

+336
-0
lines changed

go-beego/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

go-beego/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
main-bin

go-beego/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM golang:1.8.5-jessie
2+
3+
# install required debian packages
4+
# add any package that is required after `build-essential`, end the line with \
5+
RUN apt-get update && apt-get install -y \
6+
build-essential \
7+
&& rm -rf /var/lib/apt/lists/*
8+
9+
# install glide and gin
10+
RUN go get github.com/Masterminds/glide
11+
RUN go get github.com/codegangsta/gin
12+
13+
# setup the working directory
14+
WORKDIR /go/src/app
15+
ADD glide.yaml glide.yaml
16+
ADD glide.lock glide.lock
17+
RUN mkdir /scripts
18+
ADD run-local-server.sh /scripts/run-local-server.sh
19+
RUN chmod +x /scripts/run-local-server.sh
20+
21+
# install dependencies
22+
RUN glide install --skip-test
23+
24+
# add source code
25+
ADD src src
26+
27+
# build the source
28+
RUN go build src/main.go
29+
30+
# command to be executed on running the container
31+
CMD ["./main"]
32+

go-beego/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# go-beego
2+
3+
[Beego](https://github.com/astaxie/beego) (a powerful web framework for [Golang](https://golang.org/)) microservice with [Glide](https://glide.sh/) for package management [codegangsta/gin](https://github.com/codegangsta/gin) for live reloading in local development.
4+
5+
## Managing dependencies
6+
7+
### Adding a new Golang package
8+
9+
If you need to install a package, say `github.com/gin-contrib/authz`
10+
```bash
11+
# with docker
12+
13+
$ cd microservices/app
14+
$ docker build -t hello-golang-beego-app .
15+
$ docker run --rm -it -v $(pwd):/go/src/app \
16+
hello-golang-beego-app \
17+
glide get github.com/gin-contrib/authz
18+
19+
20+
# without docker
21+
22+
$ cd microservices/app
23+
$ glide get github.com/gin-contrib/authz
24+
```
25+
This will update `glide.yaml` and `glide.lock`.
26+
27+
### Adding a new system package
28+
29+
The base image used in this boilerplate is [golang:1.8.5-jessie](https://hub.docker.com/_/golang/). Hence, all debian packages are available for installation. You can add a package by mentioning it in the `Dockerfile` among the existing apt-get install packages.
30+
31+
```dockerfile
32+
FROM golang:1.8.5-jessie
33+
34+
# install required debian packages
35+
# add any package that is required after `build-essential`, end the line with \
36+
RUN apt-get update && apt-get install -y \
37+
build-essential \
38+
&& rm -rf /var/lib/apt/lists/*
39+
40+
...
41+
```
42+
43+
## Local development
44+
45+
### With Docker
46+
47+
- Install [Docker CE](https://docs.docker.com/engine/installation/)
48+
49+
```bash
50+
# go to app directory
51+
$ cd microservices/app
52+
53+
# build the docker image
54+
$ docker build -t hello-golang-beego-app .
55+
56+
# run the image using either 1 or 2
57+
58+
# 1) without live reloading
59+
$ docker run --rm -it -p 8080:8080 hello-golang-beego-app
60+
61+
# 2) with live reloading
62+
# any change you make to your source code will be immediately updated on the running app
63+
# set CLUSTER_NAME
64+
$ docker run --rm -it -p 8080:8080 \
65+
-v $(pwd):/go/src/app \
66+
-e CLUSTER_NAME=[your-cluster-name]
67+
hello-golang-beego-app \
68+
/scripts/run-local-server.sh
69+
70+
# app will be available at http://localhost:8080
71+
# press Ctrl+C to stop the server
72+
```
73+
74+
### Without Docker
75+
76+
- Install [Golang](https://golang.org/doc/install)
77+
- Move the `hello-golang-beego` directory to your `GOPATH` and cd into the directory
78+
79+
```bash
80+
# change to app directory
81+
$ cd mircoservices/app
82+
83+
# install glide for package management
84+
$ go get github.com/Masterminds/glide
85+
# install gin for live reloading
86+
$ go get github.com/codegangsta/gin
87+
88+
# set CLUSTER_NAME
89+
$ export CLUSTER_NAME=[your-cluster-name]
90+
91+
# run the local server script
92+
# windows users: run this in git bash
93+
$ ./run-local-server.sh
94+
95+
# app will be available at http://localhost:8080
96+
# any change you make to your source code will be immediately updated on the running app
97+
# press Ctrl+C to stop the server
98+
```

go-beego/glide.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go-beego/glide.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package: .
2+
import:
3+
- package: github.com/astaxie/beego
4+
version: ~1.9.2
5+
subpackages:
6+
- context
7+
- package: github.com/levigross/grequests
8+
version: ~0.9.0

go-beego/k8s.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
apiVersion: v1
2+
items:
3+
- apiVersion: extensions/v1beta1
4+
kind: Deployment
5+
metadata:
6+
creationTimestamp: null
7+
labels:
8+
app: app
9+
hasuraService: custom
10+
name: app
11+
namespace: '{{ cluster.metadata.namespaces.user }}'
12+
spec:
13+
replicas: 1
14+
strategy: {}
15+
template:
16+
metadata:
17+
creationTimestamp: null
18+
labels:
19+
app: app
20+
spec:
21+
containers:
22+
- image: hasura/base-git-image:0.7
23+
imagePullPolicy: IfNotPresent
24+
name: app
25+
env:
26+
- name: POSTGRES_HOSTNAME
27+
value: postgres.{{ cluster.metadata.namespaces.hasura }}
28+
- name: POSTGRES_PORT
29+
value: "5432"
30+
- name: POSTGRES_USERNAME
31+
valueFrom:
32+
secretKeyRef:
33+
name: hasura-secrets
34+
key: postgres.user
35+
- name: POSTGRES_PASSWORD
36+
valueFrom:
37+
secretKeyRef:
38+
name: hasura-secrets
39+
key: postgres.password
40+
ports:
41+
- containerPort: 8080
42+
protocol: TCP
43+
resources: {}
44+
securityContext: {}
45+
terminationGracePeriodSeconds: 0
46+
status: {}
47+
- apiVersion: v1
48+
kind: Service
49+
metadata:
50+
creationTimestamp: null
51+
labels:
52+
app: app
53+
hasuraService: custom
54+
name: app
55+
namespace: '{{ cluster.metadata.namespaces.user }}'
56+
spec:
57+
ports:
58+
- port: 80
59+
protocol: TCP
60+
targetPort: 8080
61+
selector:
62+
app: app
63+
type: ClusterIP
64+
status:
65+
loadBalancer: {}
66+
kind: List
67+
metadata: {}

go-beego/run-local-server.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
glide install
4+
gin --path src --bin main-bin --port 8080 run main.go

go-beego/src/main.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
9+
"github.com/astaxie/beego"
10+
"github.com/astaxie/beego/context"
11+
"github.com/levigross/grequests"
12+
)
13+
14+
func sayHello(ctx *context.Context) {
15+
ctx.Output.Body([]byte("Hello from Beego on Hasura"))
16+
}
17+
18+
// func sayPongJSON(ctx *context.Context) {
19+
// ctx.Output.JSON(map[string]string{"message": "pong"}, true, true)
20+
// }
21+
22+
func main() {
23+
beego.Get("/", sayHello)
24+
// beego.Get("/ping", sayPongJSON)
25+
26+
beego.Get("/get_articles", getArticles)
27+
beego.Get("/profile", getProfile)
28+
29+
// get port env var
30+
port := "8080"
31+
portEnv := os.Getenv("PORT")
32+
if len(portEnv) > 0 {
33+
port = portEnv
34+
}
35+
36+
beego.Run(fmt.Sprintf("0.0.0.0:%s", port)) // listen and serve on 0.0.0.0:8080 by default
37+
// set environment variable PORT if you want to change port
38+
}
39+
40+
// Endpoints demonstrating Hasura Backend Features:
41+
func getArticles(ctx *context.Context) {
42+
dataUrl := "http://data.hasura/v1/query"
43+
// set data url as external one if CLUSTER_NAME is set
44+
clusterName := os.Getenv("CLUSTER_NAME")
45+
if len(clusterName) > 0 {
46+
dataUrl = fmt.Sprintf("https://data.%s.hasura-app.io/v1/query", clusterName)
47+
}
48+
resp, err := grequests.Post(dataUrl,
49+
&grequests.RequestOptions{
50+
JSON: map[string]interface{}{
51+
"type": "select",
52+
"args": map[string]interface{}{
53+
"table": "article",
54+
"columns": []string{"*"},
55+
},
56+
},
57+
},
58+
)
59+
if err != nil {
60+
log.Printf("error: %s", err)
61+
ctx.Abort(http.StatusInternalServerError, err.Error())
62+
return
63+
}
64+
if !resp.Ok {
65+
err := fmt.Errorf("code: %d, data: %s", resp.StatusCode, string(resp.Bytes()))
66+
log.Printf("error: %s", err)
67+
ctx.Abort(http.StatusInternalServerError, err.Error())
68+
return
69+
}
70+
71+
var data interface{}
72+
err = resp.JSON(&data)
73+
if err != nil {
74+
log.Printf("error: %s", err)
75+
ctx.Abort(http.StatusInternalServerError, err.Error())
76+
return
77+
}
78+
ctx.Output.JSON(map[string]interface{}{"response": data}, true, true)
79+
return
80+
}
81+
82+
func getProfile(ctx *context.Context) {
83+
baseDomain := ctx.Input.Header("X-Hasura-Base-Domain")
84+
if len(baseDomain) == 0 {
85+
ctx.Abort(http.StatusInternalServerError, "This URL works only on Hasura clusters")
86+
return
87+
}
88+
role := ctx.Input.Header("X-Hasura-Role")
89+
if role == "user" {
90+
userId := ctx.Input.Header("X-Hasura-User-Id")
91+
ctx.Output.JSON(map[string]string{"userId": userId}, true, true)
92+
} else {
93+
redirectUrl := fmt.Sprintf(
94+
"http://auth.%s/ui?redirect_to=http://app.%s/profile",
95+
baseDomain, baseDomain,
96+
)
97+
ctx.Redirect(http.StatusTemporaryRedirect, redirectUrl)
98+
}
99+
}

0 commit comments

Comments
 (0)