Skip to content

Commit

Permalink
Issue #434, Added version number to server startup log
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Hunt <[email protected]>
  • Loading branch information
independentid committed Jun 26, 2024
1 parent 0b96578 commit 2c4720d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
7 changes: 5 additions & 2 deletions demo/cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"fmt"
"log"
"net"
"net/http"
"os"

"github.com/hexa-org/policy-orchestrator/demo/pkg/hexaConstants"
log "golang.org/x/exp/slog"

"github.com/hexa-org/policy-orchestrator/demo/internal/admin"

"github.com/hexa-org/policy-mapper/pkg/websupport"
Expand All @@ -24,7 +26,7 @@ func newApp(addr string) (*http.Server, net.Listener) {
host, _, _ := net.SplitHostPort(addr)
addr = fmt.Sprintf("%v:%v", host, found)
}
log.Printf("Found server address %v", addr)
log.Debug("Found server address %v", addr)

orchestratorUrl := os.Getenv("ORCHESTRATOR_URL")

Expand All @@ -33,5 +35,6 @@ func newApp(addr string) (*http.Server, net.Listener) {
}

func main() {
log.Info("Hexa Orchestrator Admin UI Server starting...", "version", hexaConstants.HexaOrchestratorVersion)
websupport.Start(newApp("0.0.0.0:8884"))
}
7 changes: 5 additions & 2 deletions demo/cmd/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hexa-org/policy-mapper/pkg/keysupport"
"github.com/hexa-org/policy-orchestrator/demo/internal/orchestrator"
"github.com/hexa-org/policy-orchestrator/demo/pkg/dataConfigGateway"
"github.com/hexa-org/policy-orchestrator/demo/pkg/hexaConstants"

log "golang.org/x/exp/slog"

Expand Down Expand Up @@ -47,14 +48,14 @@ func newApp(addr string) (*http.Server, net.Listener) {
host, _, _ := net.SplitHostPort(addr)
addr = fmt.Sprintf("%v:%v", host, found)
}
log.Info("Orchestrator Start", "Found server address", addr)
log.Debug("Orchestrator Start", "Found server address", addr)

if found := os.Getenv("HOST"); found != "" {
_, port, _ := net.SplitHostPort(addr)
addr = fmt.Sprintf("%v:%v", found, port)
}

log.Info("Orchestrator Start", "Found server host", addr)
log.Debug("Orchestrator Start", "Found server host", addr)

key := os.Getenv("ORCHESTRATOR_KEY")
hostPort := os.Getenv("ORCHESTRATOR_HOSTPORT")
Expand All @@ -76,6 +77,8 @@ func newApp(addr string) (*http.Server, net.Listener) {
}

func main() {
log.Info("Hexa Orchestrator API Server starting...", "version", hexaConstants.HexaOrchestratorVersion)

app, listener := newApp("0.0.0.0:8885")

websupport.Start(app, listener)
Expand Down
19 changes: 10 additions & 9 deletions demo/internal/admin/orchestrator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"

log "golang.org/x/exp/slog"

"github.com/hexa-org/policy-mapper/pkg/hexapolicy"
"github.com/hexa-org/policy-mapper/pkg/oauth2support"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func NewOrchestratorClient(client HTTPClient, url string) Client {
func (c orchestratorClient) Health() (string, error) {
resp, err := c.client.Get(fmt.Sprintf("%v/health", c.url))
if err != nil {
log.Println(err)
log.Error(err.Error())
return "[{\"name\":\"Unreachable\",\"pass\":\"fail\"}]", err
}
body, err := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c orchestratorClient) Applications(refresh bool) (applications []Applicati

var jsonResponse applicationList
if err = json.NewDecoder(resp.Body).Decode(&jsonResponse); err != nil {
log.Printf("unable to parse found json: %s\n", err.Error())
log.Error(fmt.Sprintf("unable to parse found json: %s\n", err.Error()))
return applications, err
}

Expand Down Expand Up @@ -107,7 +108,7 @@ func (c orchestratorClient) Application(id string) (Application, error) {

var jsonResponse application
if err := json.NewDecoder(resp.Body).Decode(&jsonResponse); err != nil {
log.Printf("unable to parse found json: %s\n", err.Error())
log.Error(fmt.Sprintf("unable to parse found json: %s\n", err.Error()))
return Application{}, err
}
app := Application{
Expand Down Expand Up @@ -141,7 +142,7 @@ func (c orchestratorClient) Integrations() (integrations []Integration, err erro

var jsonResponse integrationList
if err = json.NewDecoder(resp.Body).Decode(&jsonResponse); err != nil {
log.Printf("unable to parse found json: %s\n", err.Error())
log.Error(fmt.Sprintf("unable to parse found json: %s\n", err.Error()))
return integrations, err
}

Expand Down Expand Up @@ -179,7 +180,7 @@ func (c orchestratorClient) GetPolicies(id string) ([]hexapolicy.PolicyInfo, str

var jsonResponse hexapolicy.Policies
if err := json.NewDecoder(bytes.NewReader(jsonBody)).Decode(&jsonResponse); err != nil {
log.Println(err)
log.Error(err.Error())
return []hexapolicy.PolicyInfo{}, string(jsonBody), err
}

Expand Down Expand Up @@ -208,18 +209,18 @@ func (c orchestratorClient) Orchestration(from string, to string) error {

func errorOrBadResponse(response *http.Response, status int, err error) error {
if err != nil {
log.Println(err)
log.Error(err.Error())
return err
}
if response.StatusCode != status {
err := checkTokenError(response)
if err != nil {
log.Println(err.Error())
log.Error(err.Error())
return err
}
all, _ := io.ReadAll(response.Body)
message := string(all)
log.Println(message)
log.Error(message)
return errors.New(message)
}
return err
Expand Down
8 changes: 5 additions & 3 deletions demo/internal/orchestrator/applications_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package orchestrator

import (
"encoding/json"
"log"
"fmt"
"net/http"
"sort"
"strings"

log "golang.org/x/exp/slog"

"github.com/hexa-org/policy-mapper/pkg/hexapolicy"
"github.com/hexa-org/policy-orchestrator/demo/pkg/dataConfigGateway"

Expand Down Expand Up @@ -49,7 +51,7 @@ func (handler ApplicationsHandler) List(w http.ResponseWriter, r *http.Request)

records, applicationErr := handler.applicationsGateway.Find(doRefresh)
if applicationErr != nil {
log.Println("Error accessing database: " + applicationErr.Error())
log.Error("Error accessing database: " + applicationErr.Error())
http.Error(w, applicationErr.Error(), http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -143,7 +145,7 @@ func (handler ApplicationsHandler) SetPolicies(w http.ResponseWriter, r *http.Re
}
status, setErr := provider.SetPolicyInfo(integration, application, policies.Policies)
if setErr != nil {
log.Printf("unable to update policy: %s", setErr.Error())
log.Error(fmt.Sprintf("unable to update policy: %s", setErr.Error()))
// todo - should we return the error msg here.
http.Error(w, "unable to update policy.", http.StatusInternalServerError)
return
Expand Down
5 changes: 5 additions & 0 deletions demo/pkg/hexaConstants/hexaConstants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hexaConstants

const (
HexaOrchestratorVersion string = "v0.6.0"
)

0 comments on commit 2c4720d

Please sign in to comment.