/
app.go
88 lines (69 loc) · 1.82 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
//for extracting service credentials from VCAP_SERVICES
//"github.com/cloudfoundry-community/go-cfenv"
"github.com/HouzuoGuo/tiedot/db"
"github.com/amsokol/kendo-ui-echo-samples/api/persistence"
"github.com/amsokol/kendo-ui-echo-samples/api/rest"
"github.com/amsokol/kendo-ui-echo-samples/fakedata"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/tylerb/graceful"
"log"
"os"
"time"
)
const (
DEFAULT_PORT = "8080"
DEFAULT_HOST = "localhost"
)
func main() {
var port string
if port = os.Getenv("VCAP_APP_PORT"); len(port) == 0 {
port = DEFAULT_PORT
}
var host string
if host = os.Getenv("VCAP_APP_HOST"); len(host) == 0 {
host = DEFAULT_HOST
}
// prepare folder for DB with sample data
dir := "./.fakedata-db"
os.RemoveAll(dir)
defer os.RemoveAll(dir)
var tiedot *db.DB
var err error
// init sample data
if tiedot, err = db.OpenDB(dir); err != nil {
log.Panic(err)
}
defer tiedot.Close()
// init sample data
if err = fakedata.AddData(tiedot); err != nil {
log.Panic(err)
}
// init persistence stores
rest.SetProductStore(data.GetProductStore(tiedot))
// Echo instance
e := echo.New()
// Static test web pages
e.Static("/", "website")
e.Index("website/index.html")
// Customization
e.SetDebug(true)
// Middleware
e.Use(mw.Logger())
e.Use(mw.Recover())
// Routes
routes(e)
s := e.Server(host + ":" + port)
// HTTP2 is currently enabled by default in echo.New(). To override TLS handshake errors
// you will need to override the TLSConfig for the server so it does not attempt to validate
// the connection using TLS as required by HTTP2
s.TLSConfig = nil
// Start server
log.Printf("Starting app on %+v:%+v\n", host, port)
graceful.ListenAndServe(s, 5*time.Second)
}
func routes(e *echo.Echo) {
e.Get("/products", rest.GetProducts)
}