Skip to content

Commit 089624c

Browse files
committed
project structure refactor prep for api testing
1 parent 61d772b commit 089624c

File tree

8 files changed

+66
-27
lines changed

8 files changed

+66
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ To Run Server (defaults to port 3000, will also download dependencies and build)
2323

2424
To Run all tests
2525

26-
`go test ./test`
26+
`go test ./tests`
2727

2828
To Build
2929

db/db.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package db
2+
3+
/*
4+
Normally would have db connection handing function here, but for now, we just have a DeckStore inmemory storage
5+
*/
6+
7+
var deckStore DeckStore
8+
9+
func InitDb() {
10+
deckStore = NewDeckStore()
11+
}
12+
13+
func GetDeckStore() DeckStore {
14+
return deckStore
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
package decks
1+
package db
2+
3+
import "decksframework/decks"
24

35
// DeckStore stores decks, placeholder struct for future functionality such as persistent storage
46
type DeckStore struct {
5-
decksMap map[string]*Deck
7+
decksMap map[string]*decks.Deck
68
}
79

8-
func (ds DeckStore) AddDeck(d *Deck) {
10+
func (ds DeckStore) AddDeck(d *decks.Deck) {
911
ds.decksMap[d.DeckId] = d
1012
}
1113

12-
func (ds DeckStore) RemoveDeck(d *Deck) {
14+
func (ds DeckStore) RemoveDeck(d *decks.Deck) {
1315
delete(ds.decksMap, d.DeckId)
1416
}
1517

16-
func (ds DeckStore) GetDeck(uuid string) *Deck {
18+
func (ds DeckStore) GetDeck(uuid string) *decks.Deck {
1719
return ds.decksMap[uuid]
1820
}
1921

2022
// NewDeckStore initialize deckStore
2123
func NewDeckStore() DeckStore {
2224
return DeckStore{
23-
decksMap: map[string]*Deck{},
25+
decksMap: map[string]*decks.Deck{},
2426
}
2527
}

main.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package main
22

33
import (
4-
"decksframework/decks"
4+
"decksframework/db"
5+
"decksframework/server"
56
"github.com/joho/godotenv"
67
"os"
78
"strings"
@@ -20,15 +21,13 @@ func isDebug() bool {
2021
return debugStr == "true"
2122
}
2223

23-
var deckStore decks.DeckStore
24-
2524
func main() {
2625
// load .env files if set in directory
2726
_ = godotenv.Load()
2827

2928
// init in memory deck storage, replace with database call in future
30-
deckStore = decks.NewDeckStore()
29+
db.InitDb()
3130

3231
// start server and listen
33-
startApiServer(getServerPort(), isDebug())
32+
server.StartApiServer(getServerPort(), isDebug())
3433
}

server.go renamed to server/server.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package main
1+
package server
22

33
import (
4+
"decksframework/db"
45
"decksframework/decks"
56
"fmt"
67
"github.com/gin-gonic/gin"
@@ -16,16 +17,14 @@ Notes: Please branch out server routes into separate handler files when adding n
1617
Single file for now
1718
*/
1819

19-
var router *gin.Engine
20-
21-
func startApiServer(port string, debug bool) {
20+
func StartApiServer(port string, debug bool) {
2221
if debug == false {
2322
gin.SetMode("release")
2423
}
2524

26-
router = gin.Default()
25+
router := gin.Default()
2726

28-
initializeRoutes()
27+
InitializeRoutes(router)
2928

3029
// listen and serve
3130
err := router.Run(":" + port)
@@ -34,8 +33,10 @@ func startApiServer(port string, debug bool) {
3433
}
3534
}
3635

37-
func initializeRoutes() {
38-
router.POST("/deck/create", func(c *gin.Context) {
36+
func InitializeRoutes(r *gin.Engine) {
37+
deckStore := db.GetDeckStore()
38+
39+
r.POST("/deck/create", func(c *gin.Context) {
3940
shouldShuffleDeck := false
4041

4142
// ?shouldShuffleDeck=true for deck shuffling
@@ -72,7 +73,7 @@ func initializeRoutes() {
7273
c.JSON(http.StatusCreated, deck.GetClosedDeck())
7374
})
7475

75-
router.POST("/deck/draw", func(c *gin.Context) {
76+
r.POST("/deck/draw", func(c *gin.Context) {
7677
uuid, ok := c.GetQuery("uuid")
7778
if !ok || uuid == "" {
7879
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorMsg("uuid required"))
@@ -107,7 +108,7 @@ func initializeRoutes() {
107108
})
108109
})
109110

110-
router.GET("/deck/open", func(c *gin.Context) {
111+
r.GET("/deck/open", func(c *gin.Context) {
111112
uuid, ok := c.GetQuery("uuid")
112113
if !ok || uuid == "" {
113114
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorMsg("uuid required"))

tests/api_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tests
2+
3+
import (
4+
"decksframework/server"
5+
"github.com/gin-gonic/gin"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
func GetRouter() *gin.Engine {
11+
r := gin.Default()
12+
server.InitializeRoutes(r)
13+
return r
14+
}
15+
16+
func TestApiDecks(t *testing.T) {
17+
t.Run("create deck", func(t *testing.T) {
18+
httptest.NewRequest()
19+
})
20+
}

test/deckHelpers_test.go renamed to tests/deckHelpers_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package test
1+
package tests
22

3-
import "decksframework/decks"
3+
import (
4+
decks2 "decksframework/decks"
5+
)
46

5-
func CardsEqual(c1 []decks.Card, c2 []decks.Card) bool {
7+
func CardsEqual(c1 []decks2.Card, c2 []decks2.Card) bool {
68
if len(c1) != len(c2) {
79
return false
810
}

test/deck_test.go renamed to tests/deck_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test
1+
package tests
22

33
import (
44
"decksframework/decks"
@@ -228,7 +228,7 @@ func TestCustomDeck(t *testing.T) {
228228

229229
func TestGenerateCards(t *testing.T) {
230230

231-
t.Run("extra card test", func(t *testing.T) {
231+
t.Run("extra card tests", func(t *testing.T) {
232232
extraCards := []decks.Card{
233233
{
234234
Value: "GodFather",

0 commit comments

Comments
 (0)