Skip to content

Commit

Permalink
Added debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jensborch committed Oct 20, 2022
1 parent 097f7b0 commit 5942b7a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
87 changes: 52 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,49 @@

Go-foosball is an application to manage your foosball tournaments. It supports multiple tournaments, and you can easily add players and tables. Each players receives a rating based on a chess like rating system (Elo).


![Screenshot](screenshot.png)

The application is using a REST service back-end written in Go and a front-end created written TypeScript and React.
The application is using a REST service back-end written in Go and a front-end created using TypeScript and React.

## Run

Start the application on MacOS using:

```sh
./go-foosball-darwin
```

or on Linux:

```sh
./go-foosball-linux
```

or on Windows:

```bat
go-foosball.exe
```

The GUI can then be accessed using [http://localhost:8080/]

Use `--help` to list command line options - e.g.:

```sh
./go-foosball-darwin --help
```

This will output:

```txt
Usage of ./go-foosball-darwin:
-db string
the database file (default "foosball.db")
-debug
enable debug
-port uint
the port number (default 8080)
```

## Build

Expand All @@ -28,19 +67,26 @@ go build -o go-foosball

[Go](https://go.dev/) must be installed.

Build the front-end using:
The front-end will be embed in the back-end executable when build using:

```sh
make client
make client build
```

or
but back-end and front-end can also run separately. To only build the front-end use:

```sh
cd client
yarn build
```

To only start the front-end use:

```sh
cd client
yarn start
```

[Node.js](https://nodejs.org/) version 14 or above and [Yarn](https://yarnpkg.com/) must be installed.

## Test
Expand All @@ -64,35 +110,6 @@ cd client
yarn test
```

## Run

Start the application using:

```sh
./go-foosball
```

or on Windows:

```bat
go-foosball.exe
```

The GUI can then be accessed using [http://localhost:8080/]

The front-end will be embed in the back-end executable when build using:

```sh
make client build
```

but back-end and front-end can also run separately. To only start the front-end use:

```sh
cd client
yarn start
```

## Swagger

```sh
Expand All @@ -105,4 +122,4 @@ or
$(go env GOPATH)/bin/swag init
```

Swagger can be viewed using [http://localhost:8080/swagger/index.html]
Swagger can be viewed using [http://localhost:8080/swagger/index.html]
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ func main() {
var (
port uint
dbfile string
debug bool
)
flag.UintVar(&port, "port", 8080, "the port number")
flag.StringVar(&dbfile, "db", "foosball.db", "the database file")
flag.BoolVar(&debug, "debug", false, "enable debug")
flag.Parse()

engine, _ := setupServer(dbfile)
engine, _ := setupServer(dbfile, debug)
engine.Run(":" + strconv.FormatUint(uint64(port), 10))
}

Expand All @@ -55,7 +57,15 @@ func corsHandler() gin.HandlerFunc {
return cors.New(config)
}

func setupServer(dbfile string) (*gin.Engine, *gorm.DB) {
func setupServer(dbfile string, debug bool) (*gin.Engine, *gorm.DB) {
var log logger.Interface
if !debug {
gin.SetMode(gin.ReleaseMode)
log = logger.Default.LogMode(logger.Warn)
} else {
log = logger.Default.LogMode(logger.Info)
}

router := gin.Default()
router.Use(corsHandler())

Expand All @@ -66,7 +76,7 @@ func setupServer(dbfile string) (*gin.Engine, *gorm.DB) {
}

db, err := gorm.Open(sqlite.Open(dbfile), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
Logger: log,
})
if err != nil {
panic("failed to connect database")
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func startServer() (*httptest.Server, *gorm.DB) {
if err := os.Remove("test.db"); err != nil {
fmt.Println("Could not delete test DB", err)
}
engine, db := setupServer("test.db")
engine, db := setupServer("test.db", true)
return httptest.NewServer(engine), db
}

Expand Down

0 comments on commit 5942b7a

Please sign in to comment.