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

use go modules #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,16 @@ An example Go application demonstrating The Clean Architecture.
Install
-------

Create a new directory, where to host this project

mkdir -p $GOPATH:src/github.com/manuelkiessling/

Check out the source

cd $GOPATH:src/github.com/manuelkiessling/
git clone https://github.com/manuelkiessling/go-cleanarchitecture
git clone https://github.com/manuelkiessling/go-cleanarchitecture && cd go-cleanarchitecture

Setup the GOPATH to include this path
Download modules

cd go-cleanarchitecture
export GOPATH=$GOPATH:`pwd`
go mod download

Then build the project

go get
go build

Create the SQLite structure
Expand All @@ -34,14 +27,13 @@ Create the SQLite structure

Run the server

go-cleanarchitecture
./go-cleanarchitecture

Access the web endpoint at [http://localhost:8080/orders?userId=40&orderId=60](http://localhost:8080/orders?userId=40&orderId=60)

To run the tests, for each module, run

cd src/infrastructure && go test
cd src/interfaces && go test
go test ./...

Enjoy.

Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/manuelkiessling/go-cleanarchitecture

go 1.16

require github.com/mattn/go-sqlite3 v1.14.8
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.8 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU=
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"usecases"
"interfaces"
"infrastructure"
"net/http"

"github.com/manuelkiessling/go-cleanarchitecture/src/infrastructure"
"github.com/manuelkiessling/go-cleanarchitecture/src/interfaces"
"github.com/manuelkiessling/go-cleanarchitecture/src/usecases"
)

func main() {
dbHandler := infrastructure.NewSqliteHandler("/var/tmp/production.sqlite")

handlers := make(map[string] interfaces.DbHandler)
handlers := make(map[string]interfaces.DbHandler)
handlers["DbUserRepo"] = dbHandler
handlers["DbCustomerRepo"] = dbHandler
handlers["DbItemRepo"] = dbHandler
Expand Down
10 changes: 7 additions & 3 deletions src/infrastructure/sqlitehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package infrastructure
import (
"database/sql"
"fmt"

"github.com/manuelkiessling/go-cleanarchitecture/src/interfaces"
_ "github.com/mattn/go-sqlite3"
"interfaces"
)

type SqliteHandler struct {
Expand All @@ -16,7 +17,7 @@ func (handler *SqliteHandler) Execute(statement string) {
}

func (handler *SqliteHandler) Query(statement string) interfaces.Row {
//fmt.Println(statement)
// fmt.Println(statement)
rows, err := handler.Conn.Query(statement)
if err != nil {
fmt.Println(err)
Expand All @@ -40,7 +41,10 @@ func (r SqliteRow) Next() bool {
}

func NewSqliteHandler(dbfileName string) *SqliteHandler {
conn, _ := sql.Open("sqlite3", dbfileName)
conn, err := sql.Open("sqlite3", dbfileName)
if err != nil {
panic(err)
}
sqliteHandler := new(SqliteHandler)
sqliteHandler.Conn = conn
return sqliteHandler
Expand Down
5 changes: 3 additions & 2 deletions src/interfaces/repositories.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package interfaces

import (
"domain"
"fmt"
"usecases"

"github.com/manuelkiessling/go-cleanarchitecture/src/domain"
"github.com/manuelkiessling/go-cleanarchitecture/src/usecases"
)

type DbHandler interface {
Expand Down
12 changes: 6 additions & 6 deletions src/interfaces/repositories_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package interfaces_test

import (
"domain"
_ "fmt"
"infrastructure"
"interfaces"
"testing"
"usecases"

"github.com/manuelkiessling/go-cleanarchitecture/src/domain"
"github.com/manuelkiessling/go-cleanarchitecture/src/infrastructure"
"github.com/manuelkiessling/go-cleanarchitecture/src/interfaces"
"github.com/manuelkiessling/go-cleanarchitecture/src/usecases"
)

func Test_UserRepository(t *testing.T) {
Expand Down Expand Up @@ -103,7 +103,7 @@ func Test_OrderRepository(t *testing.T) {
o.Id = 39
o.Customer = c
o.Add(i1)
o.Add(i2) //fails because it's not available
o.Add(i2) // fails because it's not available
o.Add(i3)
or.Store(o)

Expand Down
18 changes: 14 additions & 4 deletions src/interfaces/webservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"io"
"net/http"
"strconv"
"usecases"

"github.com/manuelkiessling/go-cleanarchitecture/src/usecases"
)

type OrderInteractor interface {
Expand All @@ -18,9 +19,18 @@ type WebserviceHandler struct {
}

func (handler WebserviceHandler) ShowOrder(res http.ResponseWriter, req *http.Request) {
userId, _ := strconv.Atoi(req.FormValue("userId"))
orderId, _ := strconv.Atoi(req.FormValue("orderId"))
items, _ := handler.OrderInteractor.Items(userId, orderId)
userId, err := strconv.Atoi(req.FormValue("userId"))
if err != nil {
panic(err)
}
orderId, err := strconv.Atoi(req.FormValue("orderId"))
if err != nil {
panic(err)
}
items, err := handler.OrderInteractor.Items(userId, orderId)
if err != nil {
panic(err)
}
for _, item := range items {
io.WriteString(res, fmt.Sprintf("item id: %d\n", item.Id))
io.WriteString(res, fmt.Sprintf("item name: %v\n", item.Name))
Expand Down
3 changes: 2 additions & 1 deletion src/usecases/usecases.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package usecases

import (
"domain"
"fmt"

"github.com/manuelkiessling/go-cleanarchitecture/src/domain"
)

type UserRepository interface {
Expand Down