Skip to content

Commit

Permalink
welcome message at home api route (#145)
Browse files Browse the repository at this point in the history
Signed-off-by: Aura6 <[email protected]>
  • Loading branch information
zilosz authored Mar 24, 2023
1 parent d4f26fc commit 04fc979
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions tornjak-backend/api/agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/cenkalti/backoff/v4"
"github.com/gorilla/mux"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/pkg/errors"

"github.com/spiffe/spire/pkg/common/catalog"
agentdb "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/db"
auth "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/auth"
agentdb "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/db"
)

type Server struct {
Expand All @@ -39,8 +39,8 @@ type Server struct {

// AgentDB for storing Workload Attestor Plugin Info of agents
TornjakConfig *TornjakConfig
Db agentdb.AgentDB
Auth auth.Auth
Db agentdb.AgentDB
Auth auth.Auth
}

func (s *Server) agentList(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -355,7 +355,7 @@ func retError(w http.ResponseWriter, emsg string, status int) {
}

// Handle preflight checks
func (s *Server) verificationMiddleware(next http.Handler) (http.Handler) {
func (s *Server) verificationMiddleware(next http.Handler) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
cors(w, r)
Expand Down Expand Up @@ -456,6 +456,19 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}

func (s *Server) Home(w http.ResponseWriter, r *http.Request) {
var ret = "Welcome to the Tornjak Backend!"

cors(w, r)
je := json.NewEncoder(w)

var err = je.Encode(ret)
if err != nil {
emsg := fmt.Sprintf("Error: %v", err.Error())
retError(w, emsg, http.StatusBadRequest)
}
}

// HandleRequests connects api links with respective functions
// Functions currently handle the api calls all as post-requests
func (s *Server) HandleRequests() {
Expand All @@ -465,6 +478,9 @@ func (s *Server) HandleRequests() {
}
rtr := mux.NewRouter()

// Home
rtr.HandleFunc("/", s.Home)

// Agents
rtr.HandleFunc("/api/agent/list", s.agentList)
rtr.HandleFunc("/api/agent/ban", s.agentBan)
Expand Down Expand Up @@ -552,7 +568,7 @@ func (s *Server) HandleRequests() {
}
}

//TODO map[string]catalog. type
// TODO map[string]catalog. type
func getPluginConfig(plugin map[string]catalog.HCLPluginConfig) (string, ast.Node, error) {
for k, d := range plugin {
return k, d.PluginData, nil
Expand Down Expand Up @@ -620,7 +636,7 @@ func NewAuth(authPlugin map[string]catalog.HCLPluginConfig) (auth.Auth, error) {
}
}

func (s *Server) ConfigureDefaults() (error) {
func (s *Server) ConfigureDefaults() error {
var err error
expBackoff := backoff.NewExponentialBackOff()
expBackoff.MaxElapsedTime = time.Second
Expand All @@ -633,7 +649,7 @@ func (s *Server) ConfigureDefaults() (error) {
return nil
}

func (s *Server) Configure() (error) {
func (s *Server) Configure() error {
var err error
//configs := map[string]map[string]catalog.HCLPluginConfig(*s.TornjakConfig.Plugins)
if s.TornjakConfig == nil {
Expand Down

0 comments on commit 04fc979

Please sign in to comment.