forked from savely-krasovsky/go-autoconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (61 loc) · 1.46 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main // import "go-autoconfig"
import (
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
flag "github.com/spf13/pflag"
"go-autoconfig/config"
"go-autoconfig/handlers"
"go-autoconfig/templates"
)
var path = flag.String(
"config",
os.Getenv("CONFIG"),
"enter path to config file",
)
func main() {
// Parse at first startup
flag.Parse()
// Read config
conf, err := config.New(*path)
if err != nil {
fmt.Printf("Failed to load configuration: %v\n", err)
os.Exit(2)
}
tmpl := &Template{
templates: templates.Templates,
}
// Init Echo
e := echo.New()
e.Renderer = tmpl
e.HideBanner = true
// Middleware
e.Pre(LowerPath)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
h := handlers.Handler{Config: conf}
e.POST("/autodiscover/autodiscover.xml", h.Outlook)
e.GET("/mail/config-v1.1.xml", h.Thunderbird)
e.GET("/.well-known/autoconfig/mail/config-v1.1.xml", h.Thunderbird)
e.GET("/email.mobileconfig", h.AppleMail)
// Start server
e.Logger.Fatal(e.Start(conf.ServiceAddr))
}
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func LowerPath(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Request().URL.Path = strings.ToLower(c.Request().URL.Path)
next(c)
return nil
}
}