-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
61 lines (48 loc) · 1.18 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
package main
import (
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/semanticallynull/dublinbikeparking/apiv0"
)
const StaticDirectoryV1 = "./static"
func main() {
var dialect = "sqlite3"
var connectionString = "./demo.db"
if dbDialect := os.Getenv("DBP_DB_DIALECT"); dbDialect != "" {
dialect = dbDialect
}
if dbConnectionString := os.Getenv("DBP_DB_CONNECTION_STRING"); dbConnectionString != "" {
connectionString = dbConnectionString
}
db, err := gorm.Open(dialect, connectionString)
if err != nil {
panic(err)
}
r := gin.New()
apiRouter := r.Group("/api/v0")
apiv0.NewAPIv0(apiRouter, db)
r.GET("/healthz", func(c *gin.Context) {
c.String(http.StatusOK, "ok")
})
r.Static("/static", StaticDirectoryV1)
r.NoRoute(func(c *gin.Context) {
c.File(StaticDirectoryV1 + "/index.html")
})
port := getPort()
log.Printf("Listening on port %s...", port)
err = http.ListenAndServe(":"+port, r)
if err != nil {
log.Fatalf("%s\n", err)
}
}
func getPort() string {
if port := os.Getenv("PORT"); port != "" {
return port
}
return "3000"
}