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

Commit 21c6c6d

Browse files
committed
added Dockerfile and docker compose demo to get started quickly
1 parent 6a62b29 commit 21c6c6d

File tree

8 files changed

+96
-11
lines changed

8 files changed

+96
-11
lines changed

.demo.env

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
APP_ENV=dev
2+
DATABASE_URL=mongodb://mongo:27017
3+
JWT_SECRET=changeMe
4+
MAIL_PROVIDER=dev
5+
STORAGE_PROVIDER=local
6+
7+
FROM_NAME=Your company
8+
REDIS_HOST=redis:6379
9+
REDIS_PASSWORD=
10+
LOCAL_STORAGE_URL=http://localhost:8099

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.16
2+
3+
WORKDIR /app
4+
5+
COPY go.mod ./
6+
COPY go.sum ./
7+
COPY .env .env
8+
9+
RUN go mod download
10+
11+
COPY . .
12+
13+
RUN cd cmd && go build -o ../server
14+
15+
CMD [ "/app/server" ]

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build:
55
@cd cmd && rm -rf staticbackend && go build -o staticbackend
66

77
start: build
8-
@./cmd/staticbackend -host localhost
8+
@./cmd/staticbackend
99

1010
deploy:
1111
CGO_ENABLED=0 go build
@@ -14,6 +14,9 @@ deploy:
1414
test:
1515
@JWT_SECRET=okdevmode go test --race --cover ./...
1616

17+
docker:
18+
docker build . -t staticbackend:latest
19+
1720
pkg: build
1821
@rm -rf dist/*
1922
@echo "building linux binaries"

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,44 @@ This is one example of your typical day-to-day workflow using StaticBackend.
110110

111111
Please refer to this [guide here](https://staticbackend.com/getting-started/self-hosting/).
112112

113+
If you have Docker & Docker Compose ready, here's how you can have your server
114+
up and running in dev mode in 30 seconds:
115+
116+
```shell
117+
$> git clone [email protected]:staticbackendhq/core.git
118+
$> cd core
119+
$> docker build . -t staticbackend:latest
120+
$> docker-compuse -f docker-compose-demo.yml up
121+
```
122+
123+
Test your instance:
124+
125+
```shell
126+
$> curl -v http://localhost:8099/db/test
127+
```
128+
129+
You should get an error as follow:
130+
131+
```shell
132+
< HTTP/1.1 401 Unauthorized
133+
< Content-Type: text/plain; charset=utf-8
134+
< Vary: Origin
135+
< Vary: Access-Control-Request-Method
136+
< Vary: Access-Control-Request-Headers
137+
< X-Content-Type-Options: nosniff
138+
< Date: Tue, 03 Aug 2021 11:40:15 GMT
139+
< Content-Length: 33
140+
<
141+
invalid StaticBackend public key
142+
```
143+
144+
This is normal, as you're trying to request protected API, but you're all set.
145+
146+
The next step is to visit [http://localhost:8099](http://localhost:8099) and
147+
create your first app. Please note that in dev mode you'll have to look at your
148+
docker compose output terminal to see the content of the email after creating
149+
your app.
150+
113151
## Documentation
114152

115153
We're trying to have the best experience possible reading our documentation.

cmd/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package main
22

33
import (
4-
"flag"
4+
"os"
55
backend "staticbackend"
66
)
77

88
func main() {
9-
dbHost := flag.String("host", "localhost", "Hostname for mongodb")
10-
port := flag.String("port", "8099", "HTTP port to listen on")
11-
flag.Parse()
9+
dbHost := os.Getenv("DATABASE_URL")
10+
port := os.Getenv("PORT")
11+
if len(port) == 0 {
12+
port = "8099"
13+
}
1214

13-
backend.Start(*dbHost, *port)
15+
backend.Start(dbHost, port)
1416
}

docker-compose-demo.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: "3"
2+
services:
3+
sb:
4+
image: staticbackend:latest
5+
ports:
6+
- "8099:8099"
7+
env_file:
8+
- ./.demo.env
9+
10+
mongo:
11+
image: mongo:3-stretch
12+
volumes:
13+
- ./mongodata:/data/db/mongo
14+
ports:
15+
- "27017:27017"
16+
17+
redis:
18+
image: "redis:alpine"
19+
ports:
20+
- "6379:6379"

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
)
3737

3838
func TestMain(m *testing.M) {
39-
if err := openDatabase("localhost"); err != nil {
39+
if err := openDatabase("mongodb://localhost:27017"); err != nil {
4040
log.Fatal(err)
4141
}
4242

server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ func initServices(dbHost string) {
174174
}
175175
}
176176
func openDatabase(dbHost string) error {
177-
uri := os.Getenv("DATABASE_URL")
178-
if dbHost == "localhost" {
179-
uri = "mongodb://localhost:27017"
180-
}
177+
uri := dbHost
181178

182179
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
183180
cl, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))

0 commit comments

Comments
 (0)