Skip to content

Commit

Permalink
Move passhash
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Apr 24, 2017
1 parent c9e6eef commit ce519bd
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 55 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/josephspurrier/gocleanarchitecture)](https://goreportcard.com/report/github.com/josephspurrier/gocleanarchitecture)
[![GoDoc](https://godoc.org/github.com/josephspurrier/gocleanarchitecture?status.svg)](https://godoc.org/github.com/josephspurrier/gocleanarchitecture)
[![Coverage Status](https://coveralls.io/repos/github/josephspurrier/gocleanarchitecture/badge.svg?branch=master&randid=5)](https://coveralls.io/github/josephspurrier/gocleanarchitecture?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/josephspurrier/gocleanarchitecture/badge.svg?branch=master&randid=6)](https://coveralls.io/github/josephspurrier/gocleanarchitecture?branch=master)

A good example of clean architecture for a web application in Go.

Expand All @@ -11,13 +11,10 @@ dependencies. These can be structs, interfaces, and functions.

The **usecase** folder is for **application** business logic without any
dependencies with the exception of the domain logic. These can be structs,
interfaces, and functions.
interfaces, and functions. There is no usecase folder in this example.

The **adapter** folder should contain abstractions from the **lib** and
**vendor** folders.

The **repository** folder is for storing and retrieving entities from a generic
repository. This should be easily interchangable.
The **adapter** folder should contain abstractions for the packages in the
**lib** and **vendor** folders.

The **lib** folder contains internal packages, similar to the **vendor** folder
which contains 3rd party packages.
18 changes: 18 additions & 0 deletions adapter/passhash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package adapter

import (
"github.com/josephspurrier/gocleanarchitecture/lib/passhash"
)

// Passhash implements the password hashing system.
type Passhash struct{}

// Hash returns a hashed string or an error.
func (s *Passhash) Hash(password string) (string, error) {
return passhash.HashString(password)
}

// Match returns true if the hash matches the password.
func (s *Passhash) Match(hash, password string) bool {
return passhash.MatchString(hash, password)
}
18 changes: 0 additions & 18 deletions adapter/passhash/passhash.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/webapp/adapter/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/justinas/alice"
)

// IRouterService is the interface for routing.
// IRouterService is the interface for page routing.
type IRouterService interface {
Chain(c ...alice.Constructor) []alice.Constructor
ChainHandler(h http.Handler, c ...alice.Constructor) http.Handler
Expand Down
7 changes: 2 additions & 5 deletions domain/view.go → cmd/webapp/adapter/view.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package domain
package adapter

import "net/http"

// ViewVars maps a string key to a variable.
type ViewVars map[string]interface{}

// IViewService is the interface for HTML templates.
type IViewService interface {
Render(w http.ResponseWriter, r *http.Request) error
Expand All @@ -16,5 +13,5 @@ type IViewService interface {
AddVar(key string, value interface{})
DelVar(key string)
GetVar(key string) interface{}
SetVars(vars ViewVars)
SetVars(vars map[string]interface{})
}
7 changes: 4 additions & 3 deletions cmd/webapp/boot/service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package boot

import (
"github.com/josephspurrier/gocleanarchitecture/adapter"
"github.com/josephspurrier/gocleanarchitecture/adapter/jsonrepo"
"github.com/josephspurrier/gocleanarchitecture/adapter/passhash"
appadapter "github.com/josephspurrier/gocleanarchitecture/cmd/webapp/adapter"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/jsondb"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
Expand All @@ -11,7 +12,7 @@ import (
// Service represents all the services that the application uses.
type Service struct {
User domain.IUserService
View domain.IViewService
View appadapter.IViewService
}

// RegisterServices sets up each service and returns the container for all
Expand All @@ -25,7 +26,7 @@ func RegisterServices(templateFolder string) *Service {
// Store all the services for the application.
s.User = domain.NewUserService(
jsonrepo.NewUserRepo(db),
new(passhash.Item))
new(adapter.Passhash))
s.View = view.New(templateFolder, "tmpl")

return s
Expand Down
3 changes: 2 additions & 1 deletion cmd/webapp/handler/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"
"net/http"

"github.com/josephspurrier/gocleanarchitecture/cmd/webapp/adapter"
"github.com/josephspurrier/gocleanarchitecture/domain"
)

// Login represents the services required for this controller.
type Login struct {
User domain.IUserService
View domain.IViewService
View adapter.IViewService
}

// Index displays the logon screen.
Expand Down
8 changes: 4 additions & 4 deletions cmd/webapp/handler/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/url"
"testing"

"github.com/josephspurrier/gocleanarchitecture/adapter"
"github.com/josephspurrier/gocleanarchitecture/adapter/jsonrepo"
"github.com/josephspurrier/gocleanarchitecture/adapter/passhash"
"github.com/josephspurrier/gocleanarchitecture/cmd/webapp/handler"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/jsondb"
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestLoginStoreMissingRequiredFields(t *testing.T) {
h := new(handler.Login)
h.User = domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
h.View = view.New("../html", "tmpl")
h.Store(w, r)

Expand All @@ -74,7 +74,7 @@ func TestLoginStoreAuthenticateOK(t *testing.T) {
h := new(handler.Login)
h.User = domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
h.View = view.New("../html", "tmpl")

// Create a new user.
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestLoginStoreAuthenticateFail(t *testing.T) {
h := new(handler.Login)
h.User = domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
h.View = view.New("../html", "tmpl")

// Create a new user.
Expand Down
3 changes: 2 additions & 1 deletion cmd/webapp/handler/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"
"net/http"

"github.com/josephspurrier/gocleanarchitecture/cmd/webapp/adapter"
"github.com/josephspurrier/gocleanarchitecture/domain"
)

// Register represents the services required for this controller.
type Register struct {
User domain.IUserService
View domain.IViewService
View adapter.IViewService
}

// Index displays the register screen.
Expand Down
8 changes: 4 additions & 4 deletions cmd/webapp/handler/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/url"
"testing"

"github.com/josephspurrier/gocleanarchitecture/adapter"
"github.com/josephspurrier/gocleanarchitecture/adapter/jsonrepo"
"github.com/josephspurrier/gocleanarchitecture/adapter/passhash"
"github.com/josephspurrier/gocleanarchitecture/cmd/webapp/handler"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/jsondb"
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestRegisterStoreCreateOK(t *testing.T) {
h := new(handler.Register)
h.User = domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
h.View = view.New("../view", "tmpl")
h.Store(w, r)

Expand All @@ -81,7 +81,7 @@ func TestRegisterStoreCreateNoFieldFail(t *testing.T) {
h := new(handler.Register)
h.User = domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
h.View = view.New("../view", "tmpl")
h.Store(w, r)

Expand Down Expand Up @@ -111,7 +111,7 @@ func TestRegisterStoreCreateOneMissingFieldFail(t *testing.T) {
h := new(handler.Register)
h.User = domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
h.View = view.New("../view", "tmpl")
h.Store(w, r)

Expand Down
4 changes: 1 addition & 3 deletions domain/passhash.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package domain

import (
"errors"
)
import "errors"

var (
// ErrPasswordHash is when a password hash creation operation fails.
Expand Down
6 changes: 3 additions & 3 deletions domain/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"testing"

"github.com/josephspurrier/gocleanarchitecture/adapter"
"github.com/josephspurrier/gocleanarchitecture/adapter/jsonrepo"
"github.com/josephspurrier/gocleanarchitecture/adapter/passhash"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/jsondb"

Expand All @@ -29,7 +29,7 @@ func (s *BadHasher) Match(hash, password string) bool {
func setup() *domain.UserService {
return domain.NewUserService(
jsonrepo.NewUserRepo(new(jsondb.MockService)),
new(passhash.Item))
new(adapter.Passhash))
}

// TestCreateUser ensures user can be created.
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestAuthenticate(t *testing.T) {
func TestUserFailures(t *testing.T) {
// Test user creation.
db := new(jsondb.MockService)
s := domain.NewUserService(jsonrepo.NewUserRepo(db), new(passhash.Item))
s := domain.NewUserService(jsonrepo.NewUserRepo(db), new(adapter.Passhash))

db.WriteFail = true
db.ReadFail = true
Expand Down
11 changes: 6 additions & 5 deletions lib/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"html/template"
"net/http"
"path/filepath"

"github.com/josephspurrier/gocleanarchitecture/domain"
)

// Item represents a view template.
Expand All @@ -15,9 +13,12 @@ type Item struct {

baseTemplate string
template string
vars domain.ViewVars
vars map[string]interface{}
}

// ViewVars maps a string key to a variable.
//type ViewVars map[string]interface{}

// New returns a new template.
func New(folder string, extension string) *Item {
v := new(Item)
Expand All @@ -27,7 +28,7 @@ func New(folder string, extension string) *Item {
v.SetExtension(extension)
v.SetBaseTemplate("base")
v.SetTemplate("default")
v.SetVars(domain.ViewVars{})
v.SetVars(map[string]interface{}{})

return v
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func (v *Item) GetVar(key string) interface{} {
}

// SetVars sets the template variable map.
func (v *Item) SetVars(vars domain.ViewVars) {
func (v *Item) SetVars(vars map[string]interface{}) {
v.vars = vars
}

Expand Down

0 comments on commit ce519bd

Please sign in to comment.