Skip to content

Commit

Permalink
Update parser and server to watch file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
caalberts committed Sep 28, 2018
1 parent 1fbc9d9 commit 3fbfc34
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 216 deletions.
33 changes: 3 additions & 30 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
name = "github.com/julienschmidt/httprouter"
version = "1.1.0"

[[constraint]]
name = "github.com/spf13/afero"
version = "1.1.1"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.1.0"
9 changes: 7 additions & 2 deletions cmd/localroast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"flag"
"log"
log "github.com/sirupsen/logrus"

"github.com/caalberts/localroast/json"
)
Expand All @@ -19,7 +19,12 @@ func main() {
flag.Parse()
args := flag.Args()

err := json.NewCommand().Execute(*port, args)
cmd, err := json.NewCommand()
if err != nil {
log.Fatal(err)
}

err = cmd.Execute(*port, args)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions filesystem/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func (f *FileHandler) Watch() error {
file, err := os.Open(event.Name)
if err != nil {
log.Errorf("error opening file: %s", err)
} else {
f.send(file)
}

f.send(file)
}
case err := <-f.watcher.Errors:
log.Errorf("error watching file: %s", err)
Expand Down
8 changes: 3 additions & 5 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Server interface.
type Server interface {
ListenAndServe() error
Watch() chan<- []localroast.Schema
Watch(chan []localroast.Schema)
}

type server struct {
Expand All @@ -35,15 +35,13 @@ func NewServer(port string) Server {
}
}

func (s *server) Watch() chan<- []localroast.Schema {
updateChan := make(chan []localroast.Schema)
func (s *server) Watch(incomingSchemas chan []localroast.Schema) {
go func() {
for {
schemas := <-updateChan
schemas := <-incomingSchemas
s.router.updateSchema(schemas)
}
}()
return updateChan
}

type router struct {
Expand Down
60 changes: 34 additions & 26 deletions json/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,75 @@ import (
"io"

"github.com/caalberts/localroast"
"github.com/caalberts/localroast/filesystem"
"github.com/caalberts/localroast/http"
"github.com/spf13/afero"
"log"
)

type validator interface {
Validate([]string) error
}

type fileHandler interface {
Output() chan io.Reader
Open(fileName string) error
Watch() error
}

type parser interface {
Parse(<-chan io.Reader, chan<- []localroast.Schema) error
Output() chan []localroast.Schema
Watch(chan io.Reader)
}

// Command struct contains a file reader to read input file,
// a parser to parse input into schema,
// and a server constructor.
type Command struct {
v validator
p parser
s http.ServerFunc
fs fileSystem
}

type fileSystem interface {
Open(string) (afero.File, error)
validator validator
fileHandler fileHandler
parser parser
serverFunc http.ServerFunc
}

// NewCommand creates a command with a JSON file reader and parser.
func NewCommand() Command {
return Command{
v: Validator{},
p: Parser{},
s: http.NewServer,
fs: afero.NewOsFs(),
func NewCommand() (*Command, error) {
fileHandler, err := filesystem.NewFileHandler()
if err != nil {
return nil, err
}

cmd := Command{
validator: Validator{},
fileHandler: fileHandler,
parser: NewParser(),
serverFunc: http.NewServer,
}
return &cmd, nil
}

// Execute runs the command and start a server.
func (c Command) Execute(port string, args []string) error {
if err := c.v.Validate(args); err != nil {
err := c.validator.Validate(args)
if err != nil {
return err
}

input := make(chan io.Reader)
filepath := args[0]
file, err := c.fs.Open(filepath)
err = c.fileHandler.Open(filepath)
if err != nil {
return err
}

go func() {
input <- file
}()

server := c.s(port)

err = c.p.Parse(input, server.Watch())
err = c.fileHandler.Watch()
if err != nil {
return err
}

server := c.serverFunc(port)

c.parser.Watch(c.fileHandler.Output())
server.Watch(c.parser.Output())

log.Println("brewing on port " + port)
return server.ListenAndServe()
}
Loading

0 comments on commit 3fbfc34

Please sign in to comment.