Skip to content

Commit

Permalink
Closes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrodriguez committed Dec 19, 2018
2 parents fac4d74 + 34f5c4f commit 39f3ddf
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 336 deletions.
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"go.toolsEnvVars": {
"GO111MODULE": "on"
}
"go.docsTool": "gogetdoc"
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ Movies View

- [vuejs](https://vuejs.org/) - [tachyons-sass](https://github.com/tachyons-css/tachyons-sass)

- 4th Iteration [(code)](https://github.com/jbrodriguez/mediagui/) <br />
- 4th Iteration [(code)](https://github.com/jbrodriguez/mediagui/tree/4.2.0/) <br />
To learn:

- [vue-cli](https://cli.vuejs.org/)
- [vuex modules](https://vuex.vuejs.org/guide/modules.html)

- 5th Iteration [(code)](https://github.com/jbrodriguez/mediagui/) <br />
To learn:
- [gRPC](https://gprc.io/)

## Summary

### 5th Iteration

gRPC is easier to work with than go-micro (+consul).

### 4th Iteration

@vue-cli 3.x is an awesome tool.
Expand Down
7 changes: 5 additions & 2 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ clean:
go clean

protobuf:
protoc -I=./src/proto --go_out=./src/proto ./src/proto/agent.proto
protoc -I mediaagent/ mediaagent/agent.proto --go_out=plugins=grpc:mediaagent

# run formatting tool and build
build: dependencies clean
Expand All @@ -40,9 +40,12 @@ buildx: dependencies clean
go build fmt
env GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=$(mb_version)-$(mb_count).$(mb_hash)" -v -o ${PROG}

agent: dependencies clean
agentx: dependencies clean
env GOOS=linux GOARCH=amd64 go build -tags agent -ldflags "-X main.Version=$(mb_version)-$(mb_count).$(mb_hash)" -v -o agentx agent.go

agent: dependencies clean
go build -tags agent -ldflags "-X main.Version=$(mb_version)-$(mb_count).$(mb_hash)" -v -o agentx agent.go

release: dependencies clean
go build fmt
go build -ldflags "-X main.Version=$(mb_version)-$(mb_count).$(mb_hash)" -v -o ${PROG}
Expand Down
141 changes: 117 additions & 24 deletions server/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,138 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"

"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/server"
"google.golang.org/grpc"

"mediagui/agent"
pb "mediagui/mediaagent"
)

// var Version string
// Version - plugin version
var Version string
var (
Version string
port = flag.Int("port", 7624, "The server port")
)

func main() {
log.Printf("Started agent v%s", Version)
type mediaAgentServer struct {
host string
}

// Scan -
func (m *mediaAgentServer) Scan(_ context.Context, req *pb.ScanReq) (*pb.ScanRsp, error) {
log.Printf("Received Agent.Scan request: %v\n", req)

rep := &pb.ScanRsp{
Filenames: make([]string, 0),
}

for _, folder := range req.Folders {
list := m.walk(folder, req.Mask)
// if err != nil {
// mlog.Warning("Unable to scan folder (%s): %s", folder, err)
// }

// files = append(files, list...)
rep.Filenames = append(rep.Filenames, list...)
}

// for _, f := range files {
// rsp.Filenames = append(rsp.Filenames, f)
// }

log.Printf("Sent back %d files\n", len(rep.Filenames))

return rep, nil
}

func (m *mediaAgentServer) walk(folder, mask string) []string {
if folder[len(folder)-1] != '/' {
folder = folder + "/"
}

var files []string

filepath.Walk(folder, func(path string, f os.FileInfo, err error) error {
if err != nil {
log.Printf("Agent.Scan.walk: %s (%s) - [%+v]\n", err, path, f)
}

if f.IsDir() {
return nil
}

if !strings.Contains(mask, strings.ToLower(filepath.Ext(path))) {
// mlog.Info("[%s] excluding %s", filepath.Ext(path), path)
return nil
}

// log.Infof("file=%s", path)

files = append(files, m.host+":"+path)

// optionally setup command line usage
cmd.Init()
return nil
})

return files
}

// Exists -
func (m *mediaAgentServer) Exists(_ context.Context, req *pb.ExistsReq) (*pb.ExistsRsp, error) {
log.Printf("Received Agent.Exists request: %d items", len(req.Items))

rep := &pb.ExistsRsp{
Items: make([]*pb.Item, 0),
}

for _, item := range req.Items {
exists := true

if _, err := os.Stat(item.Location); err != nil {
exists = !os.IsNotExist(err)
}

if !exists {
log.Printf("Location %s doesn't exist\n", item.Location)
rep.Items = append(rep.Items, item)
}
}

return rep, nil
}

func newServer() *mediaAgentServer {
host, err := os.Hostname()
if err != nil {
log.Fatalf("Unable to obtain hostname: %s", err)
}

// Initialise Server
server.Init(
server.Name("io.jbrodriguez.mediagui.agent."+host),
server.Address("0.0.0.0:0"),
)

// Register Handlers
server.Handle(
server.NewHandler(
&agent.Agent{Host: host},
),
)

// Run server
if err := server.Run(); err != nil {
log.Printf("%s", err)
return &mediaAgentServer{host: host}
}

func main() {
flag.Parse()

address := fmt.Sprintf("0.0.0.0:%d", *port)

lis, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

log.Printf("Started MediaAgent v%s listening on %s", Version, address)

var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)

pb.RegisterMediaAgentServer(grpcServer, newServer())

grpcServer.Serve(lis)
}
95 changes: 0 additions & 95 deletions server/agent/handler.go

This file was deleted.

24 changes: 7 additions & 17 deletions server/go.mod
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
module mediagui

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-log/log v0.1.0 // indirect
github.com/golang/protobuf v1.2.0
github.com/hashicorp/consul v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.0 // indirect
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90 // indirect
github.com/hashicorp/serf v0.8.1 // indirect
github.com/jbrodriguez/actor v0.0.0-20170412170624-fa646545c8f3
github.com/jbrodriguez/go-tmdb v0.0.0-20151103115148-c1c9b4a6a2a8
github.com/jbrodriguez/mlog v0.0.0-20180805173533-cbd5ae8e9c53
github.com/jbrodriguez/pubsub v0.0.0-20170412143127-17eb44fbbab7
github.com/jmcvetta/napping v3.2.0+incompatible // indirect
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/labstack/echo v3.2.1+incompatible
github.com/labstack/gommon v0.2.7 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mattn/go-sqlite3 v1.9.0
github.com/micro/cli v0.0.0-20180830071301-8b9d33ec2f19 // indirect
github.com/micro/go-log v0.0.0-20170512141327-cbfa9447f9b6 // indirect
github.com/micro/go-micro v0.12.0
github.com/micro/go-rcache v0.0.0-20180418165751-a581a57b5133 // indirect
github.com/micro/mdns v0.0.0-20160929165650-cdf30746f9f7 // indirect
github.com/micro/util v0.0.0-20180417104657-4b7ed83e8520 // indirect
github.com/miekg/dns v1.0.13 // indirect
github.com/mitchellh/go-homedir v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/namsral/flag v1.7.4-pre
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/pborman/uuid v1.2.0 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
golang.org/x/crypto v0.0.0-20181009213950-7c1a557ab941 // indirect
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba // indirect
google.golang.org/grpc v1.17.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
Loading

0 comments on commit 39f3ddf

Please sign in to comment.