-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (52 loc) · 1.34 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"log"
"net"
"os"
"github.com/galeone/fitsleepinsights/app"
_ "github.com/galeone/fitsleepinsights/database"
_ "github.com/joho/godotenv/autoload"
"github.com/labstack/echo/v4"
)
func main() {
domains := map[string]*echo.Echo{}
app, err := app.NewRouter()
if err != nil {
panic(err.Error())
}
port := os.Getenv("PORT")
if port == "80" {
port = ""
} else {
port = fmt.Sprintf(":%s", port)
}
log.Printf("Locally, you can visit: %s%s\n", os.Getenv("DOMAIN"), port)
// Hosts without port, because reverse proxies do not forward the port
domains[os.Getenv("DOMAIN")] = app
// Catch-all server & dispatch
e := echo.New()
e.Any("/*", func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
// remove eventual port from req.Host
// so this mapping works also locally
var host string
if host, _, err = net.SplitHostPort(req.Host); err != nil {
// no port found, use the whole host
host = req.Host
}
if server, ok := domains[host]; ok {
server.ServeHTTP(res, req)
return
}
return echo.ErrNotFound
})
if port == "" {
port = ":80"
}
e.Logger.Fatal(e.Start(port))
}