Skip to content

Commit

Permalink
Project structure reorder
Browse files Browse the repository at this point in the history
  • Loading branch information
rfinochi committed May 6, 2020
1 parent c418177 commit 091a6ed
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 80 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ before_install:

before_deploy:
- curl 'https://goreportcard.com/checks' -d 'repo=github.com%2Frfinochi%2Fgolang-workshop-todo'
- rm ./docs/docs.go
- rm ./docs/swagger.json
- rm ./docs/swagger.yaml
- rm ./ui/docs/docs.go
- rm ./ui/docs/swagger.json
- rm ./ui/docs/swagger.yaml
- rm ./LICENSE
- rm ./README.md
- rm ./.travis.yml
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ EXPOSE 82/tcp
COPY . /app
WORKDIR /app

RUN go test -run TestCompleteApiInMemory
RUN go test ./cmd/web -run TestCompleteApiInMemory

ENV PORT 82
ENV REPOSITORY_TYPE Mongo
ENV MONGO_URI mongodb://db:27017
ENV TODO_REPOSITORY_TYPE Mongo
ENV TODO_MONGO_URI mongodb://db:27017

ENTRYPOINT go run .
ENTRYPOINT go run ./cmd/web
31 changes: 0 additions & 31 deletions README.md

This file was deleted.

4 changes: 2 additions & 2 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ runtime: go113
env_variables:
GOLANGORG_CHECK_COUNTRY: true
GIN_MODE: release
REPOSITORY_TYPE: 'Google'
TODO_REPOSITORY_TYPE: 'Google'

handlers:
- url: /.*
script: auto

nobuild_files: views/
nobuild_files: ui/html/
27 changes: 17 additions & 10 deletions main.go → cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import (
"os"
"strconv"

models "github.com/rfinochi/golang-workshop-todo/pkg/models"
google "github.com/rfinochi/golang-workshop-todo/pkg/models/google"
memory "github.com/rfinochi/golang-workshop-todo/pkg/models/memory"
mongo "github.com/rfinochi/golang-workshop-todo/pkg/models/mongo"

"github.com/rfinochi/golang-workshop-todo/docs"

"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/rfinochi/golang-workshop-todo/docs"

swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
Expand Down Expand Up @@ -48,7 +55,7 @@ func main() {
func SetupRouter() *gin.Engine {
router := gin.Default()

router.Use(static.Serve("/", static.LocalFile("./views", true)))
router.Use(static.Serve("/", static.LocalFile("./ui/html", true)))

api := router.Group("/api")
api.GET("/", getItemsEndpoint)
Expand Down Expand Up @@ -80,7 +87,7 @@ func SetupSwagger(router *gin.Engine) {
// @Success 201 {string} string "{\"message\": \"Ok\"}"
// @Router / [post]
func postItemEndpoint(c *gin.Context) {
var newItem Item
var newItem models.Item
c.BindJSON(&newItem)

repo := createRepository()
Expand All @@ -99,7 +106,7 @@ func postItemEndpoint(c *gin.Context) {
// @Success 201 {string} string "{\"message\": \"Ok\"}"
// @Router / [put]
func putItemEndpoint(c *gin.Context) {
var newItem Item
var newItem models.Item
c.BindJSON(&newItem)

repo := createRepository()
Expand Down Expand Up @@ -147,7 +154,7 @@ func getItemsEndpoint(c *gin.Context) {
func updateItemEndpoint(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))

var updatedItem Item
var updatedItem models.Item
c.BindJSON(&updatedItem)

repo := createRepository()
Expand Down Expand Up @@ -175,14 +182,14 @@ func deleteItemEndpoint(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "OK"})
}

func createRepository() TodoRepository {
repositoryType := os.Getenv("REPOSITORY_TYPE")
func createRepository() models.TodoRepository {
repositoryType := os.Getenv("TODO_REPOSITORY_TYPE")

if repositoryType == "Mongo" {
return &MongoRepository{}
return &mongo.MongoRepository{}
} else if repositoryType == "Google" {
return &GoogleDatastoreRepository{}
return &google.GoogleDatastoreRepository{}
} else {
return &MemoryRepository{}
return &memory.MemoryRepository{}
}
}
10 changes: 6 additions & 4 deletions main_test.go → cmd/web/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"testing"

models "github.com/rfinochi/golang-workshop-todo/pkg/models"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
Expand All @@ -20,13 +22,13 @@ func init() {
}

func TestCompleteApiInMemory(t *testing.T) {
os.Setenv("REPOSITORY_TYPE", "Memory")
os.Setenv("TODO_REPOSITORY_TYPE", "Memory")

doAllAPIRequests(t)
}

func TestCompleteApiInMongo(t *testing.T) {
os.Setenv("REPOSITORY_TYPE", "Mongo")
os.Setenv("TODO_REPOSITORY_TYPE", "Mongo")

doAllAPIRequests(t)
}
Expand Down Expand Up @@ -80,7 +82,7 @@ func doGetItem(r http.Handler, t *testing.T, id int, title string, isdone bool)

assert.Equal(t, http.StatusOK, request.Code)

var response Item
var response models.Item

err := json.Unmarshal([]byte(request.Body.String()), &response)

Expand All @@ -95,7 +97,7 @@ func doGetItems(r http.Handler, t *testing.T, title string, isdone bool, length

assert.Equal(t, http.StatusOK, request.Code)

var response []Item
var response []models.Item

err := json.Unmarshal([]byte(request.Body.String()), &response)

Expand Down
4 changes: 2 additions & 2 deletions docs/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rfinochi/golang-workshop-todo/docs
module github.com/rfinochi/golang-workshop-todo/ui/docs

go 1.12
go 1.13

require (
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@ module github.com/rfinochi/golang-workshop-todo
go 1.13

require (
github.com/rfinochi/golang-workshop-todo/docs v0.0.0
cloud.google.com/go v0.56.0 // indirect
cloud.google.com/go/datastore v1.1.0
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/gin-gonic/contrib v0.0.0-20191209060500-d6e26eeaa607
github.com/gin-gonic/gin v1.6.2
github.com/go-openapi/spec v0.19.7 // indirect
github.com/go-openapi/swag v0.19.8 // indirect
github.com/go-openapi/swag v0.19.9 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/klauspost/compress v1.10.4 // indirect
github.com/kr/pty v1.1.8 // indirect
github.com/mailru/easyjson v0.7.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rfinochi/golang-workshop-todo/docs v0.0.0
github.com/rogpeppe/go-internal v1.3.1 // indirect
github.com/stretchr/testify v1.4.0
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.5
github.com/ugorji/go v1.1.7 // indirect
github.com/urfave/cli v1.22.4 // indirect
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.3.2
golang.org/x/crypto v0.0.0-20200406173513-056763e48d71 // indirect
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5 // indirect
golang.org/x/image v0.0.0-20190829233526-b3c06291d021 // indirect
golang.org/x/mobile v0.0.0-20190830201351-c6da95954960 // indirect
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c // indirect
golang.org/x/sys v0.0.0-20200409092240-59c9f1ba88fa // indirect
golang.org/x/tools v0.0.0-20200413161937-250b2131eb8b // indirect
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 // indirect
google.golang.org/api v0.21.0
google.golang.org/genproto v0.0.0-20200413115906-b5235f65be36 // indirect
google.golang.org/grpc v1.28.1 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -99,6 +102,8 @@ github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tF
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.8 h1:vfK6jLhs7OI4tAXkvkooviaE1JEPcw3mutyegLHHjmk=
github.com/go-openapi/swag v0.19.8/go.mod h1:ao+8BpOPyKdpQz3AOJfbeEVpLmWAvlT1IfTe5McPyhY=
github.com/go-openapi/swag v0.19.9 h1:1IxuqvBUU3S2Bi4YC7tlP9SJF1gVpCvqN0T2Qof4azE=
github.com/go-openapi/swag v0.19.9/go.mod h1:ao+8BpOPyKdpQz3AOJfbeEVpLmWAvlT1IfTe5McPyhY=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
Expand Down Expand Up @@ -243,8 +248,10 @@ github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
Expand Down Expand Up @@ -281,7 +288,10 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
Expand Down Expand Up @@ -374,6 +384,8 @@ golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c h1:zJ0mtu4jCalhKg6Oaukv6iIkb+cOvDrajDH9DH46Q4M=
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
Expand Down Expand Up @@ -476,6 +488,8 @@ golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200413161937-250b2131eb8b h1:FvD0+J5ZtXZrrc2bVxQaUSnJYUhSNlB1P3XHuZohH9I=
golang.org/x/tools v0.0.0-20200413161937-250b2131eb8b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 h1:BMFHd4OFnFtWX46Xj4DN6vvT1btiBxyq+s0orYBqcQY=
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
14 changes: 8 additions & 6 deletions googledatastorerepository.go → pkg/models/google/google.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main
package google

import (
"context"
"log"

models "github.com/rfinochi/golang-workshop-todo/pkg/models"

"cloud.google.com/go/datastore"
"google.golang.org/api/iterator"
)
Expand All @@ -15,7 +17,7 @@ type GoogleDatastoreRepository struct {
}

// CreateItem godoc
func (GoogleDatastoreRepository) CreateItem(newItem Item) {
func (GoogleDatastoreRepository) CreateItem(newItem models.Item) {
ctx, client := connnectToDatastore()

key := datastore.IDKey(entityName, int64(newItem.ID), nil)
Expand All @@ -26,7 +28,7 @@ func (GoogleDatastoreRepository) CreateItem(newItem Item) {
}

// UpdateItem godoc
func (GoogleDatastoreRepository) UpdateItem(item Item) {
func (GoogleDatastoreRepository) UpdateItem(item models.Item) {
ctx, client := connnectToDatastore()

key := datastore.IDKey(entityName, int64(item.ID), nil)
Expand All @@ -37,13 +39,13 @@ func (GoogleDatastoreRepository) UpdateItem(item Item) {
}

// GetItems godoc
func (GoogleDatastoreRepository) GetItems() (items []Item) {
func (GoogleDatastoreRepository) GetItems() (items []models.Item) {
ctx, client := connnectToDatastore()

query := datastore.NewQuery("todoitem").Order("ID")
it := client.Run(ctx, query)
for {
var item Item
var item models.Item
if _, err := it.Next(&item); err == iterator.Done {
break
} else if err != nil {
Expand All @@ -56,7 +58,7 @@ func (GoogleDatastoreRepository) GetItems() (items []Item) {
}

// GetItem godoc
func (GoogleDatastoreRepository) GetItem(id int) (item Item) {
func (GoogleDatastoreRepository) GetItem(id int) (item models.Item) {
ctx, client := connnectToDatastore()

key := datastore.IDKey(entityName, int64(id), nil)
Expand Down

0 comments on commit 091a6ed

Please sign in to comment.