Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrick committed Jul 2, 2021
0 parents commit 8d3f221
Show file tree
Hide file tree
Showing 16 changed files with 1,776 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.o
shotis-node
shotis-*.json
.DS_Store
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Build the protocol buffers
protoc --go_out=network/ --go-grpc_out=network/ proto/*.proto --plugin=grpc:
# Tidy up our go mod
go mod tidy
# Run go build
go build -x
46 changes: 46 additions & 0 deletions cmd/healthreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/rbrick/shotis-node/network"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

// healthreportCmd represents the healthreport command
var healthreportCmd = &cobra.Command{
Use: "healthreport",
Short: "Get a health report of a specific on the network",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("healthreport called")

conn, err := grpc.Dial(":1337", grpc.WithInsecure())

if err != nil {
log.Fatalln(err)
}

shotisClient := network.NewShotisServiceClient(conn)

report, err := shotisClient.Health(context.Background(), &network.HealthReportRequest{})

if err != nil {
log.Fatalln(err)
}

jsreport, _ := json.MarshalIndent(report, "", " ")

fmt.Println(string(jsreport))

conn.Close()
},
}

func init() {
rootCmd.AddCommand(healthreportCmd)
}
56 changes: 56 additions & 0 deletions cmd/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"

"github.com/rbrick/shotis-node/network"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

// httpCmd represents the http command
var httpCmd = &cobra.Command{
Use: "http",
Short: "Runs an HTTP API server for Shotis",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("http called")

f, _ := os.Open(args[0])

conn, err := grpc.Dial(":1337", grpc.WithInsecure())

if err != nil {
log.Fatalln(err)
}

shotisClient := network.NewShotisServiceClient(conn)

b, _ := ioutil.ReadAll(f)

report, err := shotisClient.UploadImage(context.Background(), &network.UploadImageMessage{
FileName: args[0],
MimeType: "whatever",
Data: b,
})

if err != nil {
log.Fatalln(err)
}

jsreport, _ := json.MarshalIndent(report, "", " ")

fmt.Println(string(jsreport))

conn.Close()
},
}

func init() {
rootCmd.AddCommand(httpCmd)
}
71 changes: 71 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "shotis-node",
Short: "Command a node on the shotis network",
Long: `shotis nodes are broken down into two possible configurations.
One is a gRPC server node, which starts running a gRPC server. These server nodes handle direct communications
to Google Cloud Storage and are used as essentially workers for handling uploading and fetching of images.
The second is an API server node, which starts an HTTP server and handles all the more forward facing API endpoints.
This is in direct communication with the gRPC server node(s)
The commands are
shotis-node http - Runs the HTTP API server node.
shotis-node rpc - Runs the gRPC server node.
`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)

// Search config in home directory with name ".cmd" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".cmd")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
45 changes: 45 additions & 0 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"context"
"fmt"
"log"
"net"

"github.com/rbrick/shotis-node/network"
"github.com/rbrick/shotis-node/storage"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

// rpcCmd represents the rpc command
var rpcCmd = &cobra.Command{
Use: "rpc",
Short: "Runs an gRPC server on the Shotis network",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("rpc called")

//TODO: Implement a more robust server with go routines & worker pools and actually read from a config file
// etc. etc.
server := grpc.NewServer()

service, _ := storage.NewGoogleCloudStorage(context.Background(), "key", storage.Bucket("bucket"))

network.RegisterShotisServiceServer(server, &network.ServerImpl{
GCPService: service,
})

l, err := net.Listen("tcp", ":1337")

if err != nil {
log.Fatalln(err)
}

server.Serve(l)
},
}

func init() {
rootCmd.AddCommand(rpcCmd)
}
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"peers": [
"grpc://192.168.0.1:1337"
],
"grpc": {
"tlsEnabled": false,
"tls": {
"key": "some_key.key",
"cert": "some_cert.cert"
}
},
"storage": {
"bucket": "",
"key": ""
}
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/rbrick/shotis-node

go 1.16

require (
cloud.google.com/go v0.86.0 // indirect
cloud.google.com/go/storage v1.16.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.8.1
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
google.golang.org/api v0.50.0
google.golang.org/genproto v0.0.0-20210701191553-46259e63a0a9 // indirect
google.golang.org/grpc v1.39.0
google.golang.org/protobuf v1.27.1
)
Loading

0 comments on commit 8d3f221

Please sign in to comment.