/
main.go
44 lines (34 loc) · 913 Bytes
/
main.go
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
package main
import (
"fmt"
"github.com/yarf-framework/yarf"
)
// Panic defines a simple resource
type Panic struct {
yarf.Resource // Extend the yarf.Resource by composition
}
// Get implements a GET handler that panics
func (p *Panic) Get(c *yarf.Context) error {
c.Render("I'm panicking!")
panic("Totally panicking!")
// The next line is unreachable (govet)
//return nil
}
// PanicHandler is used to catch panics and display the error message
func PanicHandler() {
if err := recover(); err != nil {
fmt.Printf("Handling panic: %v \n", err)
}
}
// Entry point of the executable application
// It runs a default server listening on http://localhost:8080
func main() {
// Create a new empty YARF server
y := yarf.New()
// Add route/resource
y.Add("/", new(Panic))
// Set our custom panic handler
y.PanicHandler = PanicHandler
// Start server listening on port 8080
y.Start(":8080")
}