Skip to content

Commit

Permalink
allow type conversion through helper and json interface
Browse files Browse the repository at this point in the history
  • Loading branch information
narendasan committed Apr 26, 2017
1 parent 491ee71 commit ba69d68
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 2 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/acm-uiuc/arbor/security"
"github.com/acm-uiuc/arbor/server"
"github.com/acm-uiuc/arbor/services"
)

// Boot is a standard server CLI
Expand Down Expand Up @@ -73,7 +72,7 @@ func CheckRegistration(token string) {
func StartServer(routes RouteCollection, port uint16) {

security.Init()
router := server.NewRouter(services.RouteCollection(routes))
router := server.NewRouter(routes.toServiceRoutes())

log.Println("ROOTS BEING PLANTED [Server is listening on :" + fmt.Sprintf("%d", port) + "]")
log.Fatal(http.ListenAndServe(":"+fmt.Sprintf("%d", port), router))
Expand All @@ -84,7 +83,7 @@ func StartServer(routes RouteCollection, port uint16) {
// StartUnsecuredServer starts an unsecured arbor server (Token required for access)
// Provide a set of routes to server and a port to serve on
func StartUnsecuredServer(routes RouteCollection, port uint16) {
router := server.NewRouter(services.RouteCollection(routes))
router := server.NewRouter(routes.toServiceRoutes())

log.Println("ROOTS BEING PLANTED [Server is listening on :" + fmt.Sprintf("%d", port) + "]")
log.Fatal(http.ListenAndServe(":"+fmt.Sprintf("%d", port), router))
Expand Down
21 changes: 19 additions & 2 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package arbor

import (
"net/http"

"github.com/acm-uiuc/arbor/services"
)

Expand All @@ -19,8 +21,23 @@ import (
// Method: The type of request (GET, POST, DELETE, etc.)
// Pattern: The exposed url pattern for clients to hit, allows for url encoded variables to be specified with {VARIABLE}
// HandlerFunc: The function to handle the request, this basicically should just be the proxy call, but it allows you to specify more specific things
type Route services.Route
type Route struct {
Name string `json:"Name"`
Method string `json:"Method"`
Pattern string `json:"Pattern"`
HandlerFunc http.HandlerFunc `json:"Handler"`
}

// RouteCollection is a slice of routes that is used to represent a service (may change name here )
// Usage: The recomendation is to create a RouteCollection variable for all of you services and for each service create a specific one then in a registration function append all the service collections into the single master collection.
type RouteCollection []services.Route
type RouteCollection []Route

func (routes RouteCollection) toServiceRoutes() services.RouteCollection {
var serviceRoutes services.RouteCollection
for r := range routes {
sr := services.Route(routes[r])
serviceRoutes = append(serviceRoutes, sr)
}

return serviceRoutes
}
8 changes: 4 additions & 4 deletions services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ package services
import "net/http"

type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
Name string `json:"Name"`
Method string `json:"Method"`
Pattern string `json:"Pattern"`
HandlerFunc http.HandlerFunc `json:"Handler"`
}

type RouteCollection []Route

0 comments on commit ba69d68

Please sign in to comment.