/
main.go
73 lines (63 loc) · 1.33 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
// Package main RustPlanner API.
//
// Schemes: http
// Host: localhost
// BasePath: /api/v1
// Version: 0.0.1
// Contact: Philip Giuliani<info@philipg.net> http://philipg.net
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// swagger:meta
package main
import (
"log"
"os"
"strconv"
"github.com/codegangsta/negroni"
"github.com/julienschmidt/httprouter"
"github.com/philipgiuliani/rustplanner/controllers"
"github.com/unrolled/render"
"upper.io/db.v2"
"upper.io/db.v2/sqlite"
)
func main() {
// render
r := render.New(render.Options{})
// middleware
n := negroni.New(
negroni.NewLogger(),
negroni.NewRecovery(),
)
// database
sess, err := db.Open(sqlite.Adapter, sqlite.ConnectionURL{
Database: "test.db",
})
if err != nil {
log.Fatal(err)
}
defer sess.Close()
// router
router := httprouter.New()
groupsController := controllers.NewGroupsController(r, sess)
router.GET("/api/v1/groups.json", groupsController.Index)
router.POST("/api/v1/groups.json", groupsController.Create)
n.UseHandler(router)
n.Run(":" + strconv.Itoa(getPort()))
}
func getPort() int {
defaultPort := 3000
envPort := os.Getenv("PORT")
if envPort == "" {
return defaultPort
}
port, err := strconv.Atoi(envPort)
if err != nil {
return defaultPort
}
return port
}