wip on structure

This commit is contained in:
Dennis Coyle 2018-04-06 12:32:34 -04:00
commit 250b36f27b
13 changed files with 352 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
# Mac OS X files
.DS_Store
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

0
LICENSE.md Normal file
View File

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
lint:
@echo "gometalinter"
@gometalinter.v2 \
--deadline=60s \
--disable-all \
--enable=golint \
--enable=goimports \
--enable=vet \
--enable=deadcode \
--enable=gosimple \
--exclude=.*\.pb\.go \
./...

56
README.md Normal file
View File

@ -0,0 +1,56 @@
# Kubernetes
[![Go Report Card](https://goreportcard.com/badge/github.com/golang-standards/project-layout?style=flat-square)](https://goreportcard.com/report/github.com/storj/storj)
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/storj/storj)
[![Release](https://img.shields.io/github/release/golang-standards/project-layout.svg?style=flat-square)](https://github.com/storj/storj/releases/latest)
<img src="https://github.com/storj/storj/raw/master/logo/logo.png" width="100">
----
Storj is a platform, cryptocurrency, and suite of decentralized applications that allows you to store data in a secure and decentralized manner. Your files are encrypted, shredded into little pieces called 'shards', and stored in a decentralized network of computers around the globe. No one but you has a complete copy of your file, not even in an ecrypted form.
----
## To start using Storj
See our documentation at [storj docs](https://docs.storj.io/docs).
## To start developing storj
The [community site](https://storj.io/community.html) hosts all information about
building storj from source, how to contribute code
and documentation, who to contact about what, etc.
If you want to build storj right away there are two options:
##### You have a working [Go environment](https://golang.org/doc/install).
```
$ go get -d storj.io/storj
$ cd $GOPATH/src/storj.io/storj
$ make
```
##### You have a working [Docker environment](https://docs.docker.com/engine).
```
$ git clone https://github.com/storj/storj
$ cd storj
$ make docker
```
For the full story, head over to the [developer's documentation].
## Support
If you need support, start with the [troubleshooting guide],
and work your way through the process that we've outlined.
That said, if you have questions, reach out to us
[twitter](https://twitter.com/storjproject).

11
cmd/storj-node/Dockerfile Normal file
View File

@ -0,0 +1,11 @@
# build
FROM golang:alpine AS build-env
ADD . /go/src/research/lang/storj-node-go/
RUN cd /go/src/research/lang/storj-node-go/ && go build -o main
# final stage
FROM alpine
WORKDIR /app
COPY --from=build-env /go/src/research/lang/storj-node-go/main /app/
ENTRYPOINT ./main

45
cmd/storj-node/main.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"github.com/kataras/iris"
"github.com/storj/storj/routes"
"github.com/storj/storj/storage/boltdb"
)
func main() {
bdb, err := boltdb.New()
if err != nil {
fmt.Println(err)
return
}
defer bdb.DB.Close()
users := routes.Users{DB: bdb}
app := iris.Default()
SetRoutes(app, users)
app.Run(iris.Addr(":8080"))
}
// SetRoutes defines all restful routes on the service
func SetRoutes(app *iris.Application, users routes.Users) {
app.Post("/users/:id", users.CreateUser)
app.Get("/users/:id", users.GetUser)
app.Put("/users/:id/:email", users.UpdateUser)
app.Delete("/users/:id", users.DeleteUser)
// app.Get("/users/confirmations/:token", users.Confirm)
// app.Get("/files?startDate=<timestamp>?tag=<tag>", files.ListFiles)
// app.Get("/file-ids/:name", files.GetFileId)
// app.Get("/files/:file?skip=<number>&limit=<number>&exclude=<node-ids>", files.GetPointers)
// app.Delete("/files/:file", files.DeleteFile)
// app.Post("/files", files.NewFile)
// app.Put("/files/:file/shards/:index", files.AddShardToFile)
// app.Post("/reports", reports.CreateReport)
// app.Get("/contacts?address=<address>&skip=<number>&limit=<number>", contacts.GetContacts)
}

36
docs/dependencies.md Normal file
View File

@ -0,0 +1,36 @@
# Storj Dependencies
We should attempt to limit the dependencies we pull in for our projects in order to ease switching between projects.
These are only suggestions based on previous experience and our goals with this platform. If we have a need arise or a better solution is found we should consider and weigh the cost of using that solution.
[The twelve-factor app methodology](https://12factor.net/)
[Go in Production](https://peter.bourgon.org/go-in-production/)
### HTTP
[HTTP](https://golang.org/pkg/net/http/)
[Routing](https://github.com/julienschmidt/httprouter)
### Logging
[Uber Zap](https://github.com/uber-go/zap)
### Metrics
[Space Monkey Monkit](https://github.com/spacemonkeygo/monkit/)
### CLI
[urfave/cli](https://github.com/urfave/cli)
### Testing
[testing](https://golang.org/pkg/testing/)
[assertions](https://github.com/stretchr/testify)
### Configuration
[viper](https://github.com/spf13/viper)

0
go.mod Normal file
View File

BIN
logo/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

64
routes/users.go Normal file
View File

@ -0,0 +1,64 @@
package routes
import (
"log"
"github.com/google/uuid"
"github.com/kataras/iris"
"github.com/storj/storj/storage/boltdb"
)
// Users contains items needed to process requests to the user namespace
type Users struct {
DB *boltdb.Client
}
func (u *Users) CreateUser(ctx iris.Context) {
user := boltdb.User{
Id: uuid.New(),
Username: ctx.Params().Get("id"),
Email: `dece@trali.zzd`,
}
if err := ctx.ReadJSON(user); err != nil {
ctx.JSON(iris.StatusNotAcceptable)
}
u.DB.CreateUser(user)
}
func (u *Users) GetUser(ctx iris.Context) {
userId := ctx.Params().Get("id")
userInfo, err := u.DB.GetUser([]byte(userId))
if err != nil {
log.Println(err)
}
ctx.Writef("%s's info is: %s", userId, userInfo)
}
// Updates only email for now
// Uses two db queries now, can refactor
func (u *Users) UpdateUser(ctx iris.Context) {
userId := ctx.Params().Get("id")
userInfo, err := u.DB.GetUser([]byte(userId))
if err != nil {
log.Println(err)
}
updated := boltdb.User{
Id: userInfo.Id,
Username: userInfo.Username,
Email: ctx.Params().Get("email"),
}
err1 := u.DB.UpdateUser(updated)
if err1 != nil {
log.Println(err)
}
}
func (u *Users) DeleteUser(ctx iris.Context) {
userId := ctx.Params().Get("id")
u.DB.DeleteUser([]byte(userId))
}

40
storage/boltdb/client.go Normal file
View File

@ -0,0 +1,40 @@
package boltdb
import (
"time"
"github.com/boltdb/bolt"
)
var defaultTimeout = 1 * time.Second
// Client is the storage interface for the Bolt database
type Client struct {
DB *bolt.DB
UsersBucket *bolt.Bucket
}
// New instantiates a new BoltDB client
func New() (*Client, error) {
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: defaultTimeout})
if err != nil {
return nil, err
}
b := &bolt.Bucket{}
err = db.Update(func(tx *bolt.Tx) error {
b, err = tx.CreateBucketIfNotExists([]byte("users"))
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return &Client{
DB: db,
}, nil
}

View File

@ -0,0 +1,5 @@
package boltdb
type Contact struct {
Id int64 `json:"id"`
}

69
storage/boltdb/user.go Normal file
View File

@ -0,0 +1,69 @@
package boltdb
import (
"encoding/json"
"log"
"github.com/boltdb/bolt"
"github.com/google/uuid"
)
type User struct {
Id uuid.UUID `json:"id"`
Email string `json:"email"`
Username string `json:"username"`
}
// CreateUser calls bolt database instance to create user
func (bdb *Client) CreateUser(user User) error {
return bdb.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("users"))
usernameKey := []byte(user.Username)
userBytes, err := json.Marshal(user)
if err != nil {
log.Println(err)
}
return b.Put(usernameKey, userBytes)
})
}
func (bdb *Client) GetUser(key []byte) (User, error) {
var userInfo User
err := bdb.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("users"))
v := b.Get(key)
if v == nil {
log.Println("user not found")
return nil
} else {
err1 := json.Unmarshal(v, &userInfo)
return err1
}
})
return userInfo, err
}
func (bdb *Client) UpdateUser(user User) error {
return bdb.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("users"))
usernameKey := []byte(user.Username)
userBytes, err := json.Marshal(user)
if err != nil {
log.Println(err)
}
return b.Put(usernameKey, userBytes)
})
}
func (bdb *Client) DeleteUser(key []byte) {
if err := bdb.DB.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte("users")).Delete(key)
}); err != nil {
log.Println(err)
}
}