Skip to content

Commit

Permalink
adding documenations, firming up api
Browse files Browse the repository at this point in the history
  • Loading branch information
narendasan committed Apr 25, 2017
1 parent a21e124 commit efe8f88
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
30 changes: 30 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,52 @@ import (
"github.com/acm-uiuc/arbor/proxy"
)

// DELETE provides a proxy DELETE request allowing authorized clients to make DELETE requests of the microservices
// Pass the http Request from the client and the ResponseWriter it expects
// Pass the target url of the backend service (not the url the client called)
// Pass the format of the service
// Pass a authorization token (optional)
// Will call the service and return the result to the client.
func DELETE(w http.ResponseWriter, url string, format string, token string, r *http.Request) {
proxy.DELETE(w, url, format, token, r)
}

// GET provides a proxy GET request allowing authorized clients to make GET requests of the microservices
// Pass the http Request from the client and the ResponseWriter it expects
// Pass the target url of the backend service (not the url the client called)
// Pass the format of the service
// Pass a authorization token (optional)
// Will call the service and return the result to the client.
func GET(w http.ResponseWriter, url string, format string, token string, r *http.Request) {
proxy.GET(w, url, format, token, r)
}

// PATCH provides a proxy PATCH request allowing authorized clients to make PATHC requests of the microservices
// Pass the http Request from the client and the ResponseWriter it expects
// Pass the target url of the backend service (not the url the client called)
// Pass the format of the service
// Pass a authorization token (optional)
// Will call the service and return the result to the client.
func PATCH(w http.ResponseWriter, url string, format string, token string, r *http.Request) {
proxy.PATCH(w, url, format, token, r)
}

// POST provides a proxy POST request allowing authorized clients to make POST requests of the microservices
// Pass the http Request from the client and the ResponseWriter it expects
// Pass the target url of the backend service (not the url the client called)
// Pass the format of the service
// Pass a authorization token (optional)
// Will call the service and return the result to the client.
func POST(w http.ResponseWriter, url string, format string, token string, r *http.Request) {
proxy.POST(w, url, format, token, r)
}

// PUT provides a proxy PUT request allowing authorized clients to make PUT requests of the microservices
// Pass the http Request from the client and the ResponseWriter it expects
// Pass the target url of the backend service (not the url the client called)
// Pass the format of the service
// Pass a authorization token (optional)
// Will call the service and return the result to the client.
func PUT(w http.ResponseWriter, url string, format string, token string, r *http.Request) {
proxy.PUT(w, url, format, token, r)
}
36 changes: 28 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ import (
"log"
"net/http"
"os"

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

// Boot is a standard server CLI
// executable [-r | --register-client client_name] [-c | --check-registration token] [-u | --unsecured]
// -r | --register-client client_name
// registers a client, generates a token
// -c | --check-registration token
// checks if a token is valid and returns name of client
// -u | --unsecured
// runs groot without the security layer
// without args
// runs groot with the security layer
// It will start the arbor instance, parsing the command arguments and execute the behavior
func Boot(routes RouteCollection) {
if len(os.Args) == 3 && (os.Args[1] == "--register-client" || os.Args[1] == "-r") {
RegisterClient(os.Args[2])
Expand All @@ -32,6 +45,8 @@ func Boot(routes RouteCollection) {
}
}

// RegisterClient will generate a access token for a client
// Currently uses a db of client names
func RegisterClient(name string) {
security.Init()
token, err := security.AddClient(name)
Expand All @@ -44,26 +59,31 @@ func RegisterClient(name string) {
defer security.Shutdown()
}

// CheckRegistration allows you to check what client was assigned to a particular token
func CheckRegistration(token string) {
security.Init()
fmt.Println(security.IsAuthorizedClient(token))
defer security.Shutdown()
}

func StartServer(routes RouteCollection) {
// StartServer starts a secured arbor server (Token required for access)
// Provide a set of routes to service and a port to serve on
func StartServer(routes RouteCollection, port uint16) {

security.Init()
router := NewRouter(routes)
router := server.NewRouter(routes)

log.Println("ROOTS BEING PLANTED [Server is listening on :8000]")
log.Fatal(http.ListenAndServe(":8000", router))
log.Println("ROOTS BEING PLANTED [Server is listening on :" + fmt.Sprintf("%d", port) + "]")
log.Fatal(http.ListenAndServe(":"+fmt.Sprintf("%d", port), router))

defer security.Shutdown()
}

func StartUnsecuredServer(routes RouteCollection) {
router := NewRouter(routes)
// StartUnsecuredServer starts an unsecured arbor server (Token required for access)
// Provide a set of routes to service and a port to serve on
func StartUnsecuredServer(routes RouteCollection, port uint16) {
router := server.NewRouter(routes)

log.Println("ROOTS BEING PLANTED [Server is listening on :8000]")
log.Fatal(http.ListenAndServe(":8000", router))
log.Println("ROOTS BEING PLANTED [Server is listening on :" + fmt.Sprintf("%d", port) + "]")
log.Fatal(http.ListenAndServe(":"+fmt.Sprintf("%d", port), router))
}
6 changes: 3 additions & 3 deletions logger.go → server/logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Copyright © 2017, ACM@UIUC
*
* This file is part of the Groot Project.
*
* This file is part of the Groot Project.
*
* The Groot Project is open source software, released under the University of
* Illinois/NCSA Open Source License. You should have received a copy of
* this license in a file with the distribution.
Expand All @@ -16,7 +16,7 @@ import (
"time"
)

func Logger(inner http.Handler, name string) http.Handler {
func logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

Expand Down
4 changes: 2 additions & 2 deletions router.go → server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* this license in a file with the distribution.
**/

package arbor
package server

import (
"net/http"
Expand All @@ -24,7 +24,7 @@ func NewRouter(routes RouteCollection) *mux.Router {

handler = route.HandlerFunc
//Log request
handler = Logger(handler, route.Name)
handler = logger(handler, route.Name)

router.
Methods(route.Method).
Expand Down
7 changes: 7 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ package arbor

import "net/http"

// Route is a struct that defines a route for a microservice
// Name: Name of the route
// 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 struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

// 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 []Route

0 comments on commit efe8f88

Please sign in to comment.