-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra.go
59 lines (49 loc) · 1.26 KB
/
infra.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
package infra
import (
"context"
"html/template"
"net/http"
"github.com/imantung/golang_webform_for_gsheet/internal/app/infra/di"
"github.com/labstack/echo/v4"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func init() {
di.Provide(LoadTemplate)
di.Provide(NewServer)
di.Provide(NewGSheetService)
}
func LoadTemplate(cfg *Config) *template.Template {
return template.Must(template.ParseGlob(cfg.Pages))
}
func NewServer(cfg *Config, tmpl *template.Template) *echo.Echo {
e := echo.New()
e.HTTPErrorHandler = func(err error, ec echo.Context) {
code := http.StatusInternalServerError
message := "Internal Server Error"
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
message = he.Message.(string)
}
ec.Logger().Error(err)
w := ec.Response().Writer
tmpl.ExecuteTemplate(w, "error.gohtml", struct {
Code int
Message string
Detail string
}{
Code: code,
Message: message,
Detail: err.Error(),
})
}
e.HideBanner = true
e.Debug = cfg.Debug
return e
}
func NewGSheetService(cfg *Config) (*sheets.Service, error) {
ctx := context.Background()
return sheets.NewService(ctx,
option.WithCredentialsFile(cfg.GSheet.CredPath), // or option.WithCredentialsFile() to read credential from file
)
}