Merge pull request #2465 from PierreZ/samples/golang
Add a docker-compose go sample (#2404)
This commit is contained in:
commit
41fc724eaf
|
@ -0,0 +1,26 @@
|
|||
# .env
|
||||
#
|
||||
# This source file is part of the FoundationDB open source project
|
||||
#
|
||||
# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
COMPOSE_PROJECT_NAME=fdbgolangsample
|
||||
|
||||
FDB_API_VERSION=620
|
||||
FDB_VERSION=6.2.11
|
||||
FDB_COORDINATOR=fdb-coordinator
|
||||
FDB_NETWORKING_MODE=container
|
||||
FDB_COORDINATOR_PORT=4500
|
|
@ -0,0 +1,33 @@
|
|||
# Golang sample using docker-compose
|
||||
|
||||
This contains a sample `docker-compose.yaml` to run a simple golang application running with FoundationDB as a storage backend.
|
||||
|
||||
All variables are located in `.env` file.
|
||||
|
||||
## Start the golang demo
|
||||
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
This will start:
|
||||
|
||||
* 1 coordinator,
|
||||
* 2 fdbservers,
|
||||
* a golang application.
|
||||
|
||||
You can now head to [http://localhost:8080/counter](http://localhost:8080/counter) and see the counter rising-up after each refresh.
|
||||
|
||||
## Access the FoundationDB cluster
|
||||
|
||||
If you want to access the cluster from your machine, here's a `cluster file` ready for you:
|
||||
|
||||
```bash
|
||||
echo "docker:docker@127.0.0.1:4500" > docker.cluster
|
||||
```
|
||||
|
||||
## Stop the golang demo
|
||||
|
||||
```
|
||||
docker-compose down
|
||||
```
|
|
@ -0,0 +1,46 @@
|
|||
# Dockerfile
|
||||
#
|
||||
# This source file is part of the FoundationDB open source project
|
||||
#
|
||||
# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
# ARG needs to be defined for both FROM instructions,
|
||||
# see https://github.com/moby/moby/issues/34129
|
||||
ARG FDB_VERSION
|
||||
FROM foundationdb/foundationdb:${FDB_VERSION} as fdb
|
||||
FROM golang:1.13.4-stretch
|
||||
ARG FDB_VERSION
|
||||
|
||||
WORKDIR /tmp
|
||||
|
||||
RUN apt update
|
||||
# dnsutils is needed to have dig installed to create cluster file
|
||||
RUN apt install -y dnsutils
|
||||
|
||||
RUN wget "https://www.foundationdb.org/downloads/${FDB_VERSION}/ubuntu/installers/foundationdb-clients_${FDB_VERSION}-1_amd64.deb"
|
||||
RUN dpkg -i foundationdb-clients_${FDB_VERSION}-1_amd64.deb
|
||||
|
||||
COPY --from=fdb /var/fdb/scripts/create_cluster_file.bash /
|
||||
|
||||
WORKDIR /go/src/app
|
||||
COPY . .
|
||||
|
||||
COPY start.bash /start.bash
|
||||
|
||||
RUN go get -d -v ./...
|
||||
RUN go install -v ./...
|
||||
|
||||
CMD ["/start.bash"]
|
|
@ -0,0 +1,24 @@
|
|||
// go.mod
|
||||
//
|
||||
// This source file is part of the FoundationDB open source project
|
||||
//
|
||||
// Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
module foundationdb.org/docker/samples/golang/v0/fdb-demo-golang
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/apple/foundationdb/bindings/go v0.0.0-20191129023120-e16ae7cadf80
|
|
@ -0,0 +1,2 @@
|
|||
github.com/apple/foundationdb/bindings/go v0.0.0-20191129023120-e16ae7cadf80 h1:VKL6OsaB8X91ijz5DEDOw2lBIxmqTUVm5A//EExEyvo=
|
||||
github.com/apple/foundationdb/bindings/go v0.0.0-20191129023120-e16ae7cadf80/go.mod h1:OMVSB21p9+xQUIqlGizHPZfjK+SHws1ht+ZytVDoz9U=
|
|
@ -0,0 +1,80 @@
|
|||
// main.go
|
||||
//
|
||||
// This source file is part of the FoundationDB open source project
|
||||
//
|
||||
// Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/apple/foundationdb/bindings/go/src/fdb"
|
||||
)
|
||||
|
||||
var db fdb.Database
|
||||
|
||||
func main() {
|
||||
|
||||
apiVersion, err := strconv.Atoi(os.Getenv("FDB_API_VERSION"))
|
||||
if err != nil {
|
||||
log.Fatal("cannot parse FDB_API_VERSION from env")
|
||||
}
|
||||
// Different API versions may expose different runtime behaviors.
|
||||
fdb.MustAPIVersion(apiVersion)
|
||||
|
||||
// Open the default database from the system cluster
|
||||
db = fdb.MustOpenDatabase(os.Getenv("FDB_CLUSTER_FILE"))
|
||||
|
||||
http.HandleFunc("/counter", incrementCounter)
|
||||
|
||||
fmt.Println("starting webserver")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
func incrementCounter(w http.ResponseWriter, r *http.Request) {
|
||||
ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) {
|
||||
value := tr.Get(fdb.Key("my-counter")).MustGet()
|
||||
if len(value) == 0 {
|
||||
value = intToBytes(0)
|
||||
}
|
||||
counter := bytesToInt(value)
|
||||
counter++
|
||||
tr.Set(fdb.Key("my-counter"), intToBytes(counter))
|
||||
return intToBytes(counter), nil
|
||||
})
|
||||
|
||||
if e != nil {
|
||||
log.Fatalf("Unable to perform FDB transaction (%v)", e)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "Counter is %d", bytesToInt(ret.([]byte)))
|
||||
}
|
||||
|
||||
func bytesToInt(buf []byte) int {
|
||||
return int(binary.BigEndian.Uint32(buf))
|
||||
}
|
||||
|
||||
func intToBytes(i int) []byte {
|
||||
v := uint32(i)
|
||||
buf := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(buf, v)
|
||||
return buf
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#!/bin/bash
|
||||
|
||||
# start.bash
|
||||
#
|
||||
# This source file is part of the FoundationDB open source project
|
||||
#
|
||||
# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http:#www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
set -eu;
|
||||
|
||||
/create_cluster_file.bash
|
||||
FDB_CLUSTER_FILE="${FDB_CLUSTER_FILE:-/etc/foundationdb/fdb.cluster}"
|
||||
|
||||
# Attempt to connect. Configure the database if necessary.
|
||||
if ! /usr/bin/fdbcli -C $FDB_CLUSTER_FILE --exec status --timeout 3 ; then
|
||||
echo "creating the database"
|
||||
if ! fdbcli -C $FDB_CLUSTER_FILE --exec "configure new single memory ; status" --timeout 10 ; then
|
||||
echo "Unable to configure new FDB cluster."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
/go/bin/fdb-demo-golang
|
|
@ -0,0 +1,64 @@
|
|||
# docker-compose.yaml
|
||||
#
|
||||
# This source file is part of the FoundationDB open source project
|
||||
#
|
||||
# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
version: '3'
|
||||
services:
|
||||
# Specify three fdbserver processes.
|
||||
fdb-coordinator:
|
||||
image: foundationdb/foundationdb:${FDB_VERSION}
|
||||
ports:
|
||||
- 4500:4500/tcp
|
||||
environment:
|
||||
FDB_COORDINATOR: ${FDB_COORDINATOR}
|
||||
FDB_NETWORKING_MODE: ${FDB_NETWORKING_MODE}
|
||||
FDB_COORDINATOR_PORT: ${FDB_COORDINATOR_PORT}
|
||||
|
||||
fdb-server-1:
|
||||
depends_on:
|
||||
- fdb-coordinator
|
||||
image: foundationdb/foundationdb:${FDB_VERSION}
|
||||
environment:
|
||||
FDB_COORDINATOR: ${FDB_COORDINATOR}
|
||||
FDB_NETWORKING_MODE: ${FDB_NETWORKING_MODE}
|
||||
FDB_COORDINATOR_PORT: ${FDB_COORDINATOR_PORT}
|
||||
|
||||
fdb-server-2:
|
||||
depends_on:
|
||||
- fdb-coordinator
|
||||
image: foundationdb/foundationdb:${FDB_VERSION}
|
||||
environment:
|
||||
FDB_COORDINATOR: ${FDB_COORDINATOR}
|
||||
FDB_NETWORKING_MODE: ${FDB_NETWORKING_MODE}
|
||||
FDB_COORDINATOR_PORT: ${FDB_COORDINATOR_PORT}
|
||||
|
||||
# Bring up the application so that it depends on the cluster.
|
||||
app:
|
||||
depends_on:
|
||||
- fdb-coordinator
|
||||
- fdb-server-1
|
||||
- fdb-server-2
|
||||
build:
|
||||
context: app
|
||||
args:
|
||||
FDB_VERSION: ${FDB_VERSION}
|
||||
ports:
|
||||
- 8080:8080/tcp
|
||||
environment:
|
||||
FDB_COORDINATOR: ${FDB_COORDINATOR}
|
||||
FDB_API_VERSION: ${FDB_API_VERSION}
|
Loading…
Reference in New Issue