/
main.go
44 lines (34 loc) · 1.03 KB
/
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
// static example package demonstrates how to easily implement
// a yarf handler that serves static files using the net/http package.
package main
import (
"github.com/yarf-framework/yarf"
"net/http"
)
// Static defines a simple resource
type Static struct {
yarf.Resource // Extend the yarf.Resource by composition
path string // Directory to serve static files from.
}
// Get implements the static files handler
func (s *Static) Get(c *yarf.Context) error {
http.FileServer(http.Dir(s.path)).ServeHTTP(c.Response, c.Request)
return nil
}
// StaticDir constructs a Static handler and sets the path to serve under the route.
func StaticDir(path string) *Static {
s := new(Static)
s.path = path
return s
}
// 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 routes/resources
y.Add("/", StaticDir("/tmp"))
y.Add("/test", StaticDir("/var/www/test"))
// Start server listening on port 8080
y.Start(":8080")
}