Skip to content

Commit

Permalink
Merge pull request #2 from ara-framework/feat/env-variables
Browse files Browse the repository at this point in the history
feat: support env variables for config file and port
  • Loading branch information
marconi1992 authored Oct 16, 2019
2 parents 01843d5 + eea26f3 commit d027445
Showing 1 changed file with 65 additions and 53 deletions.
118 changes: 65 additions & 53 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"

"github.com/gookit/color"
"github.com/gorilla/mux"
"github.com/imdario/mergo"
)
Expand Down Expand Up @@ -101,10 +103,10 @@ func BatchRequest(server string, batch map[string]ViewJob) BatchResponse {
}

func GetViewDefintions() map[string]ViewDefinition {
dat, err := ioutil.ReadFile("./views.json")
dat, err := ioutil.ReadFile(os.Getenv("CONFIG_FILE"))

if err != nil {
log.Fatal(err)
log.Fatal("Config file not found")
}

var viewDefinitions map[string]ViewDefinition
Expand All @@ -114,79 +116,89 @@ func GetViewDefintions() map[string]ViewDefinition {
return viewDefinitions
}

func BatchHandler(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
func BatchHandler(viewDefinitions map[string]ViewDefinition) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)

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

var viewRequests map[string]ViewJob

json.Unmarshal(b, &viewRequests)
if err != nil {
log.Fatal(err)
}

viewDefinitions := GetViewDefintions()
var viewRequests map[string]ViewJob

aggregatedResponse := BatchResponse{
Results: make(map[string]ViewJobResult),
}
json.Unmarshal(b, &viewRequests)

batches := make(map[string]map[string]ViewJob)
aggregatedResponse := BatchResponse{
Results: make(map[string]ViewJobResult),
}

for uuid, job := range viewRequests {
server := viewDefinitions[job.Name].Server
batches := make(map[string]map[string]ViewJob)

for uuid, job := range viewRequests {
server := viewDefinitions[job.Name].Server

if server == "" {
aggregatedResponse.Results[uuid] = ViewJobResult{
Name: job.Name,
Success: false,
Error: ViewJobError{
Name: "ReferenceError",
Message: "Component\"" + job.Name + "\" not registered in cluster",
},
}
continue
}

if server == "" {
aggregatedResponse.Results[uuid] = ViewJobResult{
Name: job.Name,
Success: false,
Error: ViewJobError{
Name: "ReferenceError",
Message: "Component\"" + job.Name + "\" not registered in cluster",
},
if batches[server] == nil {
batch := make(map[string]ViewJob)
batches[server] = batch
}
continue
}

if batches[server] == nil {
batch := make(map[string]ViewJob)
batches[server] = batch
batches[server][uuid] = job
}

batches[server][uuid] = job
}
var wg sync.WaitGroup
var batchResponsesMap sync.Map

var wg sync.WaitGroup
var batchResponsesMap sync.Map
for server, batch := range batches {
wg.Add(1)

for server, batch := range batches {
wg.Add(1)
go func(server string, batch map[string]ViewJob) {
response := BatchRequest(server, batch)
batchResponsesMap.Store(server, response)
defer wg.Done()
}(server, batch)
}

go func(server string, batch map[string]ViewJob) {
response := BatchRequest(server, batch)
batchResponsesMap.Store(server, response)
defer wg.Done()
}(server, batch)
}
wg.Wait()

wg.Wait()
for server := range batches {
response, _ := batchResponsesMap.Load(server)

for server := range batches {
response, _ := batchResponsesMap.Load(server)
batchResponse := response.(BatchResponse)
mergo.Merge(&aggregatedResponse.Results, batchResponse.Results)
}

batchResponse := response.(BatchResponse)
mergo.Merge(&aggregatedResponse.Results, batchResponse.Results)
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(aggregatedResponse)
}

w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(aggregatedResponse)
}

func main() {

router := mux.NewRouter()
router.Use(corsMiddleware)

router.HandleFunc("/batch", BatchHandler).Methods("OPTIONS", "POST")
http.ListenAndServe(":8000", router)
viewDefinitions := GetViewDefintions()

router.HandleFunc("/batch", BatchHandler(viewDefinitions)).Methods("OPTIONS", "POST")

port := os.Getenv("PORT")

if len(port) == 0 {
port = "8000"
}

color.Info.Printf("Nova cluster running on http://0.0.0.0:%s\n", port)
http.ListenAndServe(":"+port, router)
}

0 comments on commit d027445

Please sign in to comment.