-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (54 loc) · 1.95 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/kelseyhightower/envconfig"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"gopkg.in/go-playground/validator.v10"
)
type ServerOptions struct {
Port int `default:"8080" validate:"port"`
CORSAllowOrigins []string `split_words:"true" default:"*"`
CORSAllowMethods []string `split_words:"true" default:"GET" validate:"cors_allow_methods"`
MatrixHServer string `required:"true" split_words:"true" validate:"fqdn|hostname_port"`
MatrixMHomeserver string `required:"true" split_words:"true" validate:"url"`
MatrixMIdentityServer string `split_words:"true" default:"https://vector.im" validate:"url"`
}
var opts *ServerOptions
func init() {
opts = new(ServerOptions)
if err := envconfig.Process("discovery", opts); err != nil {
log.Fatal(err)
}
validate := validator.New()
validate.RegisterValidation("cors_allow_methods", validateCorsAllowMethods)
validate.RegisterValidation("port", validatePort)
if err := validate.Struct(opts); err != nil {
log.Fatal(err)
}
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: opts.CORSAllowOrigins,
AllowMethods: opts.CORSAllowMethods,
}))
e.GET("/.well-known/matrix/server", getWellKnownMatrixServer)
e.GET("/.well-known/matrix/client", getWellKnownMatrixClient)
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", opts.Port)))
}
func getWellKnownMatrixServer(c echo.Context) error {
wellKnown := map[string]string{"m.server": opts.MatrixHServer}
return c.JSON(http.StatusOK, wellKnown)
}
func getWellKnownMatrixClient(c echo.Context) error {
wellKnown := map[string]map[string]string{
"m.homeserver": map[string]string{"base_url": opts.MatrixMHomeserver},
"m.identity_server": map[string]string{"base_url": opts.MatrixMIdentityServer},
}
return c.JSON(http.StatusOK, wellKnown)
}