Last active
April 29, 2017 21:19
-
-
Save josh-gree/51dbe7b9218ecc8b0845b24f7d44e9e1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package comm | |
import ( | |
"fmt" | |
"time" | |
"bytes" | |
"encoding/json" | |
"net/http" | |
"github.com/labstack/echo" | |
) | |
type Message interface{ | |
Recieve() | |
Send() | |
} | |
type JobMessage struct{ | |
Id int `json:"id"` | |
Data []float64 `json:"data"` | |
Service string `json:"service"` | |
} | |
var locs = map[string]string{"sum":"localhost:8001/job","prod":"localhost:8002/job"} | |
func (j *JobMessage) Recieve(public bool, service ...func(data []float64, id int)) func(c echo.Context) error{ | |
return func (c echo.Context) error{ | |
err := c.Bind(&j) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
fmt.Printf("%#v\n",j) | |
if public { | |
// Send Job to service | |
j.Send(locs[j.Service]) | |
} else { | |
// Do Job | |
// Send result to public | |
go service[0](j.Data,j.Id) | |
} | |
return nil | |
} | |
} | |
func (j *JobMessage) Send(dest string) error{ | |
data, err := json.Marshal(j) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
_, err = http.Post(fmt.Sprintf("http://%s",dest),"application/json",bytes.NewBuffer(data)) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
return nil | |
} | |
type ResMessage struct{ | |
Id int `json:"id"` | |
Result float64 `json:"result"` | |
} | |
func (r *ResMessage) Recieve(public bool) func(c echo.Context) error{ | |
return func (c echo.Context) error{ | |
err := c.Bind(&r) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
if public { | |
// Log result to stdout | |
fmt.Printf("%#v\n",r) | |
} else { | |
// This will never happen (services do not recieve results) | |
} | |
return nil | |
} | |
} | |
func (r *ResMessage) Send(dest string) error{ | |
data, err := json.Marshal(r) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
_, err = http.Post(fmt.Sprintf("http://%s",dest),"application/json",bytes.NewBuffer(data)) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
return nil | |
} |
This file contains hidden or 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
package main | |
import ( | |
"github.com/labstack/echo" | |
"github.com/josh-gree/comm" | |
"math/rand" | |
"time" | |
) | |
var j = comm.JobMessage{} | |
var r = comm.ResMessage{} | |
var public = false // read from cml | |
func Prod(data []float64, id int){ | |
sum := 1.0 | |
for _,d := range data{ | |
sum *= d | |
} | |
time.Sleep(time.Duration(rand.Int31n(10000)) * time.Millisecond) | |
resmsg := comm.ResMessage{Id:id,Result:sum} | |
resmsg.Send("localhost:8000/res") | |
} | |
func main(){ | |
e := echo.New() | |
e.POST("/job", j.Recieve(public,Prod)) | |
e.Start(":8002") | |
} |
This file contains hidden or 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
package main | |
import ( | |
"github.com/labstack/echo" | |
"github.com/josh-gree/comm" | |
) | |
var j = comm.JobMessage{} | |
var r = comm.ResMessage{} | |
var public = true // read from cml | |
func main(){ | |
e := echo.New() | |
e.POST("/job", j.Recieve(public)) | |
e.POST("/res", r.Recieve(public)) | |
e.Start(":8000") | |
} |
This file contains hidden or 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
package main | |
import ( | |
"github.com/labstack/echo" | |
"github.com/josh-gree/comm" | |
"math/rand" | |
"time" | |
) | |
var j = comm.JobMessage{} | |
var r = comm.ResMessage{} | |
var public = false // read from cml | |
func Sum(data []float64,id int){ | |
sum := 0.0 | |
for _,d := range data{ | |
sum += d | |
} | |
time.Sleep(time.Duration(rand.Int31n(10000)) * time.Millisecond) | |
resmsg := comm.ResMessage{Id:id,Result:sum} | |
resmsg.Send("localhost:8000/res") | |
} | |
func main(){ | |
e := echo.New() | |
e.POST("/job", j.Recieve(public,Sum)) | |
e.Start(":8001") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment