echotool

package module
v0.0.0-...-1fe41ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

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 embedFS

  type 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 lifetime

          func(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 authentication

      e.GET("/error", func(c echo.Context) error {
          return echotool.Die(http.StatusInternalServerError, nil, "error message")
      })

      e.Start(":8080")
  }
#+end_src

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auth

func Auth(next echo.HandlerFunc) echo.HandlerFunc

func Die

func Die(code int, err error, format string, args ...any) error

func MkHash

func MkHash(user string, pass string) string

func SetupDefaultEcho

func SetupDefaultEcho[User any](
	staticFS http.FileSystem,
	jwtKeyB64 string,
	jwtIssuer string,
	jwtLifetime time.Duration,
	name2user func(string) (User, error),
	user2hash func(User) (string, error),
) (*echo.Echo, *echo.Group)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL