Skip to content

Commit

Permalink
chore(blog): update db.json usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaraa committed Mar 15, 2024
1 parent 0edfea1 commit 6eaeb77
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions files/blogs/setting-up-go-templ-with-tailwind-htmx-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,17 @@ const dbFilePath = "./db.json"
// will be used with the json database implementations
var jsonMgr = &storeManager{}

// create the db file if not exists
func init() {
_, err := os.Stat(dbFilePath)
if os.IsNotExist(err) {
_, err = os.Create(dbFilePath)
if err != nil {
panic(err)
}
}
}

type storeManager struct {
// mu is to make the manager concurrently safe
mu sync.RWMutex
Expand Down Expand Up @@ -1006,6 +1017,17 @@ func main() {
}
```

Now that `main.go` is utilizing the database, the file `db.json` is generated, since it was in the `init` of the `db` package.

Now you need modify it now to set your balance, and don't worry about the spendings, it'll be handled by the json data stores we wrote earlier.

```json
{
"spendings": [],
"balance": 1234
}
```

#### Implementing add, update and delete endpoints

For the handler, to complete the cycle, we'll create a struct hodling the endpoints then handle them using `http.HandleFunc`, and since Go has recently added specifieing the endpoint's method in version [1.22](https://tip.golang.org/doc/go1.22), and this is Go's official docs for the new mux thingy [Routing Enhancements for Go 1.22](https://go.dev/blog/routing-enhancements), this will be an easy task.
Expand Down

0 comments on commit 6eaeb77

Please sign in to comment.