Skip to content

Commit

Permalink
removes dependency on Echo
Browse files Browse the repository at this point in the history
  • Loading branch information
deiu committed Dec 17, 2016
1 parent e142eb6 commit ab359f9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 39 deletions.
31 changes: 6 additions & 25 deletions handler.go
Expand Up @@ -7,9 +7,6 @@ import (
"net/http"
"os"
"sync"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)

var (
Expand All @@ -26,52 +23,36 @@ func InitLogger(config *ServerConfig) *log.Logger {
}

// NewServer creates a new server handler
func NewProxyHandler(config *ServerConfig, proxy *Proxy) *echo.Echo {
func NewProxyHandler(config *ServerConfig, proxy *Proxy) http.Handler {
logger := InitLogger(config)
logger.Println("\n---- starting proxy server ----")
logger.Printf("config: %#v\n", config)

proxy.Log = logger

// Create new handler
handler := echo.New()

// Recover in case of panics
handler.Use(middleware.Recover())
handler := http.NewServeMux()

// ****** Routes Middleware ******

// Proxy handler
// The proxy handler uses the standard ResponseWriter and Request objects
handler.Any("/proxy", echo.WrapHandler(http.HandlerFunc(proxy.Handler)))
// Catch all other routes with 501 - Not Implemented
handler.Any("/*", func(c echo.Context) error {
return c.String(http.StatusNotImplemented, "Not implemented")
})
handler.HandleFunc("/proxy", proxy.Handler)

return handler
}

func NewAgentHandler(config *ServerConfig, agent *Agent) *echo.Echo {
func NewAgentHandler(config *ServerConfig, agent *Agent) http.Handler {
logger := InitLogger(config)
logger.Println("\n---- starting agent server ----")
logger.Printf("config: %#v\n", config)
agent.Log = logger

// Create new handler
handler := echo.New()

// Recover in case of panics
handler.Use(middleware.Recover())
handler := http.NewServeMux()

// Agent's WebID handler
handler.OPTIONS("/webid", echo.WrapHandler(http.HandlerFunc(agent.Handler)))
handler.HEAD("/webid", echo.WrapHandler(http.HandlerFunc(agent.Handler)))
handler.GET("/webid", echo.WrapHandler(http.HandlerFunc(agent.Handler)))
// Catch all other routes with 501 - Not Implemented
handler.Any("/*", func(c echo.Context) error {
return c.String(http.StatusNotImplemented, "Not implemented")
})
handler.HandleFunc("/webid", agent.Handler)

return handler
}
Expand Down
6 changes: 3 additions & 3 deletions handler_test.go
Expand Up @@ -62,18 +62,18 @@ func TestServerVersion(t *testing.T) {
assert.Equal(t, SERVER_VERSION, GetVersion())
}

func TestRouteNotImplemented(t *testing.T) {
func TestRouteDoesNotExist(t *testing.T) {
req, err := http.NewRequest("GET", testAgentServer.URL, nil)
assert.NoError(t, err)
resp, err := testClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, 501, resp.StatusCode)
assert.Equal(t, 404, resp.StatusCode)

req, err = http.NewRequest("GET", testProxyServer.URL, nil)
assert.NoError(t, err)
resp, err = testClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, 501, resp.StatusCode)
assert.Equal(t, 404, resp.StatusCode)
}

func TestRouteWebID(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions proxy-server/proxy-server.go
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"os"

"github.com/labstack/echo"
"github.com/solid/solidproxy"
)

Expand Down Expand Up @@ -96,11 +95,11 @@ func main() {

// Start servers
println("\nStarting SolidProxy", solidproxy.GetVersion())
go agentHandler.StartServer(agentServer)
proxyHandler.StartServer(proxyServer)
go agentServer.ListenAndServe()
proxyServer.ListenAndServe()
}

func NewServer(handler *echo.Echo, config *solidproxy.ServerConfig) (*http.Server, error) {
func NewServer(handler http.Handler, config *solidproxy.ServerConfig) (*http.Server, error) {
// Create proxy server listener and set config values
var err error
s := &http.Server{
Expand Down
2 changes: 1 addition & 1 deletion proxy_test.go
Expand Up @@ -172,7 +172,7 @@ func TestProxyBadRequest(t *testing.T) {
assert.NoError(t, err)
resp, err := testClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, 405, resp.StatusCode)
assert.Equal(t, 400, resp.StatusCode)
}

func TestProxyNoSkipVerify(t *testing.T) {
Expand Down
13 changes: 9 additions & 4 deletions webid.go
Expand Up @@ -42,14 +42,19 @@ func NewAgent(uri string) (*Agent, error) {
return agent, nil
}

func NewAgentLocal(uri string) (*Agent, error) {
func NewAgentLocal(uri string, bits ...int) (*Agent, error) {
agent, err := NewAgent(uri)
if err != nil {
return agent, err
}

keyLen := rsaBits
if len(bits) > 0 {
keyLen = bits[0]
}

// Create a new keypair
privKey, E, N, err := NewRSAKey()
privKey, E, N, err := NewRSAKey(keyLen)
if err != nil {
return agent, err
}
Expand All @@ -61,9 +66,9 @@ func NewAgentLocal(uri string) (*Agent, error) {
return agent, nil
}

func NewRSAKey() (*rsa.PrivateKey, string, string, error) {
func NewRSAKey(bits int) (*rsa.PrivateKey, string, string, error) {
var e, n string
p, err := rsa.GenerateKey(rand.Reader, rsaBits)
p, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return p, e, n, err
}
Expand Down
14 changes: 12 additions & 2 deletions webid_test.go
Expand Up @@ -12,13 +12,18 @@ import (
)

func TestNewRSAKey(t *testing.T) {
p, e, n, err := NewRSAKey()
p, e, n, err := NewRSAKey(rsaBits)
assert.NoError(t, err)
assert.IsType(t, new(rsa.PrivateKey), p)
assert.Equal(t, fmt.Sprintf("%d", p.PublicKey.E), e)
assert.NotEmpty(t, fmt.Sprintf("%x", p.PublicKey.N), n)
}

func TestNewRSAKeyError(t *testing.T) {
_, _, _, err := NewRSAKey(0)
assert.Error(t, err)
}

func TestNewAgent(t *testing.T) {
agent, err := NewAgent(testAgentWebID)
assert.NoError(t, err)
Expand All @@ -37,8 +42,13 @@ func TestNewAgentLocalEmptyURI(t *testing.T) {
assert.Empty(t, agent.WebID)
}

func TestNewAgentLocalBadKey(t *testing.T) {
_, err := NewAgentLocal(testAgentWebID, 0)
assert.Error(t, err)
}

func TestNewRSAcert(t *testing.T) {
p, _, _, err := NewRSAKey()
p, _, _, err := NewRSAKey(rsaBits)
assert.NoError(t, err)
cert, err := NewRSAcert(testAgentWebID, "Solid Proxy Agent", p)
assert.NoError(t, err)
Expand Down

0 comments on commit ab359f9

Please sign in to comment.