gitstack

grindlemire/gothem-stack code browser

1.3 KB Go 55 lines 2025-01-11 · 7257ca7 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package handler

import (
	"time"

	"github.com/google/uuid"
	"github.com/grindlemire/gothem-stack/pkg/log"
	"github.com/grindlemire/gothem-stack/web/pages/home"
	"github.com/labstack/echo/v4"
	"github.com/pkg/errors"
	"go.uber.org/zap"
)

type HomeHandler struct {
	// you could put a database handle here or any dependencies you want
}

func NewHomeHandler() (h *HomeHandler, err error) {
	return &HomeHandler{}, nil
}

// RegisterRoutes registers all the subroutes for the home handler to manage
func (h *HomeHandler) RegisterRoutes(g *echo.Group) {
	g.GET("", h.RenderHomepage)
	g.GET("/random-string", h.GetRandomString)
}

func (h *HomeHandler) RenderHomepage(c echo.Context) error {
	return render(c, home.Page())
}

func (h *HomeHandler) GetRandomString(c echo.Context) error {
	time.Sleep(750 * time.Millisecond)

	err := DoThing()
	if err != nil {
		zap.L().Info("example error", log.Callers(err)...)
		// zap.L().Info("example error with stacktrace", log.Callers(err, log.WithStack())...)
	}

	return render(c, home.RandomString(uuid.NewString()))
}

func DoThing() error {
	return DoSubThing()
}

func DoSubThing() error {
	// wrap third party errors at the callsite to get nice stack traces
	return errors.Wrap(ThirdPartyError(), "wrapped third party error")
}

func ThirdPartyError() error {
	return errors.New("third party error")
}