- Added helper function `ExpandUser` - Added basic cache impmentations for in-memory and file based caches. Reviewed-on: #1 Co-authored-by: Felipe M. <me@fmartingr.com> Co-committed-by: Felipe M. <me@fmartingr.com>
97 lines
1.9 KiB
Markdown
97 lines
1.9 KiB
Markdown
# gotoolkit
|
|
|
|
[](https://pkg.go.dev/git.nakama.town/fmartingr/gotoolkit)
|
|
|
|
A set of basic tools to develop Go applications.
|
|
|
|
# Index
|
|
|
|
- [Cache](#cache)
|
|
- [Database](#database)
|
|
- [Paths](#paths)
|
|
- [Service & Servers](#service--servers)
|
|
- [Template](#template)
|
|
|
|
## Cache
|
|
|
|
A basic cache engine to manage the cache of the application.
|
|
|
|
```go
|
|
cache := cache.NewMemoryCache()
|
|
```
|
|
|
|
## Database
|
|
|
|
A basic database engine to manage the connection to a database.
|
|
|
|
```go
|
|
db, err := database.New("postgres://user:password@localhost:5432/dbname")
|
|
if err != nil {
|
|
// ...
|
|
}
|
|
```
|
|
|
|
## Paths
|
|
|
|
A basic utility to manage the paths of the application.
|
|
|
|
```go
|
|
path := paths.ExpandUser("~/myapp")
|
|
```
|
|
|
|
## Service & Servers
|
|
|
|
A basic way to expose servers within one service.
|
|
|
|
A service is the main point of the application, and it can expose multiple servers.
|
|
Using the provided interfaces you can create a new _service_ with multiple _servers_.
|
|
|
|
A simple example with one single HTTP server:
|
|
|
|
```go
|
|
|
|
import "git.nakama.town/fmartingr/gotoolkit/model"
|
|
|
|
type httpServer struct {}
|
|
|
|
func (s *httpServer) IsEnabled() bool {
|
|
return true
|
|
}
|
|
|
|
func (s *httpServer) Start(_ context.Context) error {
|
|
return s.http.ListenAndServe()
|
|
}
|
|
|
|
func (s *httpServer) Stop(ctx context.Context) error {
|
|
return s.http.Shutdown(ctx)
|
|
}
|
|
|
|
func newHttpServer() (servers.Server, error) {
|
|
httpServer := &httpServer{}
|
|
// http server logic
|
|
return httpServer, nil
|
|
}
|
|
|
|
func main() {
|
|
httpServer, _ := newHttpServer()
|
|
svc := service.New([]model.Server{server})
|
|
svc.Start(context.Background())
|
|
svc.WaitStop()
|
|
}
|
|
```
|
|
|
|
## Template
|
|
|
|
A basic template engine to render html templates.
|
|
|
|
```go
|
|
//go:embed templates/*.html
|
|
var Templates embed.FS
|
|
// ...
|
|
engine, _ := template.NewEngine(Templates)
|
|
result, _ := engine.Render("template.html", struct {
|
|
Message string
|
|
}{
|
|
Message: "nometokens"
|
|
})
|
|
```
|