This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
296 changed files
with
172,168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# macOS | ||
.DS_Store | ||
|
||
# vim | ||
.swp | ||
*~ | ||
*.swp | ||
*.swo | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
bin/ | ||
build/ | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# misc | ||
notes.txt | ||
|
||
.data/ | ||
|
||
.testfiles/ | ||
|
||
*.pem | ||
|
||
*.tar | ||
|
||
node/test_data/go_example_image.tar | ||
|
||
coverage.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: go | ||
go: | ||
- 1.10.x | ||
- master | ||
script: | ||
- go test -v -covermode=count -coverprofile=coverage.out | ||
- $(go env GOPATH | awk 'BEGIN{FS=":"} {print $1}')/bin/goveralls -coverprofile=coverage.out | ||
- service=travis-ci -repotoken=$COVERALLS_TOKEN | ||
env: | ||
global: | ||
secure: iVR2L/wRzt9TsiUCeCklTvndN4pedMFrcUR0/ZGO6nNjwTxid6KD+DyPT4iiZh6NnNrGRfhZa1wy8Y2oEJtns9kZJ5ARp+SPXAmvj2OZhrpRiSm+JRugz/sGFXxViTfujwfH/igVHQJLC8T/jfZ7mirxzZCptaxT6OFl2j1Yt7WgOumvpgnixdnr0yxEThLJZwug2SC51jlfPv4HzxwW1sRJtG6J4US/8PMLd5ZBXiKncWODa4YUCHm+cGXZhD+8aAWZzTWU1v8VfyWQQHrjVR36dIIyInMzUAg1VQiBOQxBMofu6q4SOWV4Hpq29J72Ai6SxljNU32ACHTHIk3mjdt9mO/23p0TyLCgmZ49dreQCshNPp6TDnMZNlc9jiln0pxNicnk+WbocSMJr1IXocwB23ewzBKBJkrugINW9Z2gs/tbyEy0+t5G0UU84F8+ctycUJsdUQAXMC8HpDEu/G1j70fGVAROL6HRilO2KXNjlyo6ZwvjiO2HOTBl1pcSTSXW4Ixco+b4SsBsQFuFOZD+Hr7wkb09Mxvh8GKxE4/OVIbVC8l0NqXpxSWRGBjyqzy/SXlKCGnZu3gRUEst/wpVi4hv7HtQ5OyUXQZfp41XLr1qe9ndqg7dwIlhusqw4Lt8MwOwtatQ3IBnGLFj4kgzVPVVAJQtEvsVKtE7x4g= | ||
install: | ||
- go get golang.org/x/tools/cmd/cover | ||
- go get github.com/mattn/goveralls |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[[constraint]] | ||
branch = "develop" | ||
name = "github.com/c3systems/c3" | ||
|
||
[[constraint]] | ||
name = "github.com/sirupsen/logrus" | ||
version = "1.0.5" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[![Build Status](https://travis-ci.org/c3systems/sdk-go.svg?branch=master)](https://travis-ci.org/c3systems/sdk-go) [![Coverage Status](https://coveralls.io/repos/github/c3systems/sdk-go/badge.svg?branch=master)](https://coveralls.io/github/c3systems/sdk-go?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/c3systems/sdk-go)](https://goreportcard.com/report/github.com/c3systems/sdk-go) [![GoDoc](https://godoc.org/github.com/c3systems/sdk-go?status.svg)](https://godoc.org/github.com/c3systems/sdk-go) | ||
|
||
# C3 SDK-GO | ||
|
||
> The golang sdk for c3 | ||
## Usage | ||
**TODO** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/c3systems/c3/common/stringutil" | ||
c3config "github.com/c3systems/c3/config" | ||
"github.com/c3systems/c3/core/server" | ||
loghooks "github.com/c3systems/c3/logger/hooks" | ||
) | ||
|
||
var ( | ||
// ErrMethodAlreadyRegistred ... | ||
ErrMethodAlreadyRegistred = errors.New("method already registered") | ||
) | ||
|
||
// State ... | ||
type State struct { | ||
state map[string]string | ||
} | ||
|
||
// C3 ... | ||
type C3 struct { | ||
registeredMethods map[string]func(args ...interface{}) error | ||
receiver chan []byte | ||
state *State | ||
statefile string | ||
} | ||
|
||
// store | ||
type store struct { | ||
*C3 | ||
} | ||
|
||
// NewC3 ... | ||
func NewC3() *C3 { | ||
receiver := make(chan []byte) | ||
c3 := &C3{ | ||
registeredMethods: map[string]func(args ...interface{}) error{}, | ||
receiver: receiver, | ||
state: &State{ | ||
state: map[string]string{}, | ||
}, | ||
statefile: c3config.TempContainerStateFilePath, | ||
} | ||
|
||
err := c3.setInitialState() | ||
if err != nil { | ||
log.Fatalf("[c3] %s", err) | ||
} | ||
|
||
go func() { | ||
err = c3.listen() | ||
if err != nil { | ||
log.Fatalf("[c3] %s", err) | ||
} | ||
}() | ||
|
||
return c3 | ||
} | ||
|
||
// RegisterMethod ... | ||
func (c3 *C3) RegisterMethod(methodName string, types []string, ifn interface{}) error { | ||
if _, ok := c3.registeredMethods[methodName]; ok { | ||
return ErrMethodAlreadyRegistred | ||
} | ||
|
||
c3.registeredMethods[methodName] = func(args ...interface{}) error { | ||
switch v := ifn.(type) { | ||
// TODO: accept arbitrary args | ||
case func(string, string) error: | ||
key, ok := args[0].(string) | ||
if !ok { | ||
return errors.New("not ok") | ||
} | ||
value, ok := args[1].(string) | ||
if !ok { | ||
return errors.New("not ok") | ||
} | ||
|
||
log.Printf("[c3] executed method %s with args: %s %s", methodName, key, value) | ||
err := v(key, value) | ||
if err != nil { | ||
log.Error("[c3] method failed %s", err) | ||
log.Fatalf("[c3] %s", err) | ||
} | ||
} | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
// Serve ... | ||
func (c3 *C3) Serve() { | ||
server.NewServer(&server.Config{ | ||
Host: c3config.ServerHost, | ||
Port: c3config.ServerPort, | ||
Receiver: c3.receiver, | ||
}).Run() | ||
} | ||
|
||
// State ... | ||
func (c3 *C3) State() *State { | ||
return c3.state | ||
} | ||
|
||
// Set ... | ||
// TODO: accept interfaces | ||
func (s *State) Set(key, value string) error { | ||
s.state[key] = value | ||
fmt.Println("setting state k/v", key, value) | ||
fmt.Println("latest state:", s.state) | ||
|
||
b, err := json.Marshal(s.state) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[c3] marshed state %s", string(b)) | ||
|
||
f, err := os.OpenFile(c3config.TempContainerStateFilePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0600) | ||
if err != nil { | ||
log.Error("[c3] failed to store file") | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
f.Write(b) | ||
|
||
return nil | ||
} | ||
|
||
// Get ... | ||
// TODO: accept interfaces | ||
func (s *State) Get(key string) string { | ||
v := s.state[key] | ||
return v | ||
} | ||
|
||
func (c3 *C3) setInitialState() error { | ||
if _, err := os.Stat(c3.statefile); err == nil { | ||
src, err := ioutil.ReadFile(c3.statefile) | ||
if err != nil { | ||
log.Errorf("[c3] fail to read %s", err) | ||
return err | ||
} | ||
|
||
log.Printf("[c3] json data %s", string(src)) | ||
|
||
if len(src) == 0 { | ||
return nil | ||
} | ||
|
||
b, err := stringutil.CompactJSON(src) | ||
if err != nil { | ||
log.Errorf("[c3] failed to compact %s", err) | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(b, &c3.state.state) | ||
if err != nil { | ||
log.Errorf("[c3] fail to unmarshal %s", err) | ||
return err | ||
} | ||
} else { | ||
log.Error("[c3] state file not found") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Process ... | ||
func (c3 *C3) Process(payload []byte) error { | ||
var ifcs []interface{} | ||
if err := json.Unmarshal(payload, &ifcs); err != nil { | ||
log.Errorf("[c3] %s", err) | ||
return err | ||
} | ||
|
||
// if format is [a, b, c] | ||
_, ok := ifcs[0].(string) | ||
if ok { | ||
v := make([]string, len(ifcs)) | ||
for i, k := range ifcs { | ||
v[i] = k.(string) | ||
} | ||
|
||
err := c3.invoke(v[0], v[1:]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// if format is [[a, b, c], [a, b, c]] | ||
for i := range ifcs { | ||
ifc := ifcs[i].([]interface{}) | ||
v := make([]string, len(ifc)) | ||
for j, k := range ifc { | ||
v[j] = k.(string) | ||
} | ||
|
||
err := c3.invoke(v[0], v[1:]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// invoke ... | ||
func (c3 *C3) invoke(method string, params []string) error { | ||
var args []interface{} | ||
for _, v := range params { | ||
args = append(args, v) | ||
} | ||
|
||
return c3.registeredMethods[method](args...) | ||
} | ||
|
||
// listen ... | ||
func (c3 *C3) listen() error { | ||
for payload := range c3.receiver { | ||
err := c3.Process(payload) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
log.AddHook(loghooks.ContextHook{}) | ||
} |
Oops, something went wrong.