Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/42loco42/echotool
A setup helper for https://echo.labstack.com
https://github.com/42loco42/echotool
Last synced: 1 day ago
JSON representation
A setup helper for https://echo.labstack.com
- Host: GitHub
- URL: https://github.com/42loco42/echotool
- Owner: 42LoCo42
- License: gpl-3.0
- Created: 2024-01-29T13:08:43.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-01-29T16:23:21.000Z (10 months ago)
- Last Synced: 2024-06-21T16:55:52.851Z (5 months ago)
- Language: Go
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
README
* echotool
Helper functions for working with [[https://echo.labstack.com/][echo]] - a web framework for Go.* Features
- HTTP error helper (=Die=)
- request logging
- static file loading from the =static= directory
- user authentication: password login → JWT (=Auth= middleware)
#+begin_src shell
curl localhost:8080/api/login -d username=user -d password=pass -c cookie
# the Login endpoint returns the auth JWT as string
# and also sets the auth cookie to that JWT
#+end_src
- when an empty JWT key is passed, =SetupDefaultEcho= will print one and return =nil=* Usage
#+begin_src go
import (
"embed"
"net/http""github.com/42LoCo42/echotool"
)//go:embed static
var embedFS embedFStype User struct {
Name string
Hash string
}func main() {
e, api := echotool.SetupDefaultEcho(
http.FS(embedFS), // directory for static files (can be nil to read from disk)os.Getenv("JWT_KEY_VAR"), // auth token key
"appname", // auth token issuer
time.Hour*24, // auth token lifetimefunc(name string) (*User, error) {
// find a user by name
panic("TODO implement this!")
},// get a user's hash
func(user *User) (string, error) {
return user.Hash, nil
},
)
if e == nil {
os.Exit(1)
}e.GET("/example", func(c echo.Context) error {
return c.JSON(http.StatusOK, c.Get("user"))
}, echotool.Auth) // this endpoint requires authenticatione.GET("/error", func(c echo.Context) error {
return echotool.Die(http.StatusInternalServerError, nil, "error message")
})e.Start(":8080")
}
#+end_src