bootstrap_data_cloudfunctions

package
v0.0.0-...-231df5d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2019 License: MIT Imports: 9 Imported by: 0

README

The Cloud Functions Is Already Deployed

Please go to Test Cloud Functions API to the end of this README.


Here is how to do it from scratch:

Cloud Firestore Quickstart

gcloud config set account ACCOUNT

Create new GCP project

Edit Project Name and CREATE

New Project

Start Cloud Firestore

SELECT NATIVE MODE

Firestore 1

Select a location and CREATE DATABASE

Firestore 2

Create new service account key

IAM & admin -> Service accounts

Edit Service account name, choose Role Owner andCREATE

Firestore 1

Save the JSON file with your private key

Prepare your environment

export GCP_PROJECT="bootstrap-data-cloudfunctions"
export GOOGLE_APPLICATION_CREDENTIALS="/Users/stefan/.secret/bootstrap-data-cloudfunctions-c628b7847572.json"
export GCP_SA_BOOTSTRAP_PEERS="sa-owner-bootstrap-data@bootstrap-data-cloudfunctions.iam.gserviceaccount.com"

Prepare your Cloud SDK

Cloud SDK Quickstarts

Find appropriate region for Cloud Functions

gcloud functions regions list
export GCP_REGION="europe-west1"

Set account, project and region for Cloud Function

gcloud auth activate-service-account $GCP_SA_BOOTSTRAP_PEERS --key-file=$GOOGLE_APPLICATION_CREDENTIALS
gcloud config set project $GCP_PROJECT
gcloud config set functions/region $GCP_REGION

Check config

gcloud config list

Prepare Cloud Functions for Go

export GO111MODULE=on
go mod init
go mod edit -go=1.11
go mod vendor

Deploy Cloud Functions

gcloud functions deploy reset --entry-point Reset --runtime go111 --trigger-http
gcloud functions deploy join --entry-point Join --runtime go111 --trigger-http
gcloud functions deploy leave --entry-point Leave --runtime go111 --trigger-http
gcloud functions deploy refill --entry-point Refill --runtime go111 --trigger-http
gcloud functions deploy list --entry-point List --runtime go111 --trigger-http
gcloud functions deploy ping --entry-point Ping --runtime go111 --trigger-http
gcloud functions deploy config --entry-point ConfigUpdate --runtime go111 --trigger-http

Test Cloud Functions

export GCP_SERVICE_URL="https://$GCP_REGION-$GCP_PROJECT.cloudfunctions.net"

curl -s $GCP_SERVICE_URL/ping
curl -sd "3 1"  $GCP_SERVICE_URL/config

curl -sd "alice-id alice 127.0.0.1 12340 tcp test 1"  $GCP_SERVICE_URL/join
curl -sd "bob-id bob 127.0.0.1 12341 tcp test 2"  $GCP_SERVICE_URL/join

curl -sd "alice-id" $GCP_SERVICE_URL/leave
curl -s $GCP_SERVICE_URL/list

curl -sd "2 1"  $GCP_SERVICE_URL/config
curl -sd "alice-id alice 127.0.0.1 12341 tcp test 1552655444"  $GCP_SERVICE_URL/refill

curl -s $GCP_SERVICE_URL/reset
curl -s $GCP_SERVICE_URL/list

Test Cloud Functions API

cd ~/go/src/github.com/stefanhans/cli-chat/bootstrap-data-api

export BOOTSTRAP_DATA_SERVER="https://europe-west1-bootstrap-data-cloudfunctions.cloudfunctions.net"
go test -run TestCf

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigUpdate

func ConfigUpdate(w http.ResponseWriter, r *http.Request)

func Join

func Join(w http.ResponseWriter, r *http.Request)

Join get information of the new member via http and stores it in Firestore

func Leave

func Leave(w http.ResponseWriter, r *http.Request)

Leave get information of the leaving peer via http and deletes it in Firestore

func List

func List(w http.ResponseWriter, r *http.Request)

List returns all bootstrap data from Firestore

func Ping

func Ping(w http.ResponseWriter, r *http.Request)

func Refill

func Refill(w http.ResponseWriter, r *http.Request)

func Reset

func Reset(w http.ResponseWriter, r *http.Request)

Reset deletes all documents of the collection from Firestore

Types

type BootstrapData

type BootstrapData struct {
	Config Config
	Peers  map[string]*Peer
}

BootstrapData is a complete data structure

type Config

type Config struct {
	MaxPeers            int `json:"maxpeers,omitempty"`            // Max number of bootstrap peers to be saved
	MinRefillCandidates int `json:"minrefillcandidates,omitempty"` // Number used to decide peer send refill request
	NumPeers            int `json:"numpeers,omitempty"`            // Number of bootstrap peers
}

type Peer

type Peer struct {
	ID       string `json:"id,omitempty"`   // UUID
	Name     string `json:"name,omitempty"` // chat name
	Ip       string `json:"ip,omitempty"`
	Port     string `json:"port,omitempty"`
	Protocol string `json:"protocol,omitempty"` // "tcp" or "udp"
	// todo get rid of unused field status
	Status    string `json:"status,omitempty"`
	Timestamp string `json:"timestamp,omitempty"` // Unix time in seconds
}

Peer is the struct for the collection

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL