-
Notifications
You must be signed in to change notification settings - Fork 48
/
file_controller.go
67 lines (57 loc) · 1.8 KB
/
file_controller.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package interfaces
import (
"bytes"
"context"
"net/http"
"os"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
)
type (
// FileController implementing FileController
FileController struct {
responder *web.Responder
logger flamingo.Logger
robotsTxtFilepath string
securityTxtFilepath string
humansTxtFilepath string
}
)
// Inject configuration
func (d *FileController) Inject(
responder *web.Responder,
logger flamingo.Logger,
config *struct {
RobotsTxtFilepath string `inject:"config:core.robotstxt.filepath"`
SecurityTxtFilepath string `inject:"config:core.securitytxt.filepath"`
HumansTxtFilepath string `inject:"config:core.humanstxt.filepath"`
},
) {
d.responder = responder
d.logger = logger.WithField("category", "robotstxt")
if config != nil {
d.robotsTxtFilepath = config.RobotsTxtFilepath
d.securityTxtFilepath = config.SecurityTxtFilepath
d.humansTxtFilepath = config.HumansTxtFilepath
}
}
// GetRobotsTxt returns /robots.txt
func (d *FileController) GetRobotsTxt(ctx context.Context, _ *web.Request) web.Result {
return d.serveFile(ctx, d.robotsTxtFilepath)
}
// GetSecurityTxt returns /.well-known/security.txt
func (d *FileController) GetSecurityTxt(ctx context.Context, _ *web.Request) web.Result {
return d.serveFile(ctx, d.securityTxtFilepath)
}
// GetHumansTxt returns /humans.txt
func (d *FileController) GetHumansTxt(ctx context.Context, _ *web.Request) web.Result {
return d.serveFile(ctx, d.humansTxtFilepath)
}
func (d *FileController) serveFile(ctx context.Context, filePath string) web.Result {
fileContent, err := os.ReadFile(filePath)
if err != nil {
d.logger.WithContext(ctx).Error(err)
d.responder.ServerError(err)
}
return d.responder.HTTP(http.StatusOK, bytes.NewReader(fileContent))
}