Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some refactoring to get file system routes defined in routing package #12

Merged
merged 1 commit into from Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions routing/router.go
@@ -1,20 +1,33 @@
package routing

import (
"net/http"

"github.com/gorilla/mux"

"github.com/swift-sunshine/swscore/config"
)

func NewRouter() *mux.Router {
func NewRouter(conf *config.Config) *mux.Router {

router := mux.NewRouter().StrictSlash(true)

for _, route := range paths {
// Build our API server routes and install them.
routes := NewRoutes()
for _, route := range routes.Routes {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}

// Build our console routes by first creating the file server handler that will serve
// the webapp js files and other static content. Then tell the router about our fixed
// routes which pass all static file requests to the file handler.
fileServerHandler := http.FileServer(http.Dir(conf.Server.Static_Content_Root_Directory))
router.PathPrefix("/console").Handler(http.StripPrefix("/console", fileServerHandler))
router.PathPrefix("/").Handler(fileServerHandler)

return router
}
4 changes: 3 additions & 1 deletion routing/router_test.go
Expand Up @@ -4,10 +4,12 @@ import (
"testing"

"github.com/gorilla/mux"

"github.com/swift-sunshine/swscore/config"
)

func TestDrawPathProperly(t *testing.T) {
router := NewRouter()
router := NewRouter(new(config.Config))
testRoute(router, "Root", "GET", t)
}

Expand Down
25 changes: 16 additions & 9 deletions routing/routes.go
Expand Up @@ -13,14 +13,21 @@ type Route struct {
HandlerFunc http.HandlerFunc
}

type Routes []Route
type Routes struct {
Routes []Route
}

func NewRoutes() (r *Routes) {
r = new(Routes)

r.Routes = []Route{
Route{
"Root",
"GET",
"/api",
handlers.Root,
},
}

// Constant that defines all the paths and the handlers for those paths
var paths = Routes{
Route{
"Root",
"GET",
"/api",
handlers.Root,
},
return
}
25 changes: 6 additions & 19 deletions server/server.go
Expand Up @@ -12,14 +12,7 @@ import (

func StartServer(conf *config.Config) {
// create a router that will route all incoming API server requests to different handlers
router := routing.NewRouter()

// create the file server handler that will serve the webapp js files and other static content
fileServerHandler := http.FileServer(http.Dir(conf.Server.Static_Content_Root_Directory))

// tell the router to pass all static file requests to the file handler - this includes our console UI
router.PathPrefix("/console").Handler(http.StripPrefix("/console", fileServerHandler))
router.PathPrefix("/").Handler(fileServerHandler)
router := routing.NewRouter(conf)

// put our proxy handler in front to handle auth
proxyHandler := serverAuthProxyHandler{
Expand Down Expand Up @@ -73,18 +66,12 @@ func (h *serverAuthProxyHandler) handler(w http.ResponseWriter, r *http.Request)

switch statusCode {
case http.StatusOK:
{
h.trueHandler.ServeHTTP(w, r)
}
h.trueHandler.ServeHTTP(w, r)
case http.StatusUnauthorized:
{
w.Header().Set("WWW-Authenticate", "Basic realm=\"Swift-Sunshine\"")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
w.Header().Set("WWW-Authenticate", "Basic realm=\"Swift-Sunshine\"")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
default:
{
http.Error(w, http.StatusText(statusCode), statusCode)
log.Errorf("Cannot send response to unauthorized user: %v", statusCode)
}
http.Error(w, http.StatusText(statusCode), statusCode)
log.Errorf("Cannot send response to unauthorized user: %v", statusCode)
}
}