-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
83 lines (70 loc) · 1.87 KB
/
server.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
// Command qrest is a quick RESTful JSON server
//
// How to use
//
// Create a JSON file containing the data you'd like to be part of your server. An example file might look like:
//
// {
// "posts": [ { "id": 1, "title": "Foo" } ]
// }
//
// Start qrest with this file as an argument:
//
// qrest db.json
//
// Or in a docker container:
//
// $ docker build -t qrest .
// $ docker run --rm -p 3000:3000 qrest "db.json" # assuming db.json is in this source directory
//
// This will create the following routes for you to use:
//
// POST /posts (creates a new post record)
// GET /posts (returns all post records)
// GET /posts/:id (returns a specific record)
// PUT /posts/:id (creates or updates a record with the specified ID)
// PATCH /posts/:id (updates a record with the specified ID)
// DELETE /posts/:id (deletes the specified record)
//
//
package main
import (
"os"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/negroni"
"github.com/julienschmidt/httprouter"
nlogrus "github.com/meatballhat/negroni-logrus"
)
var (
loggerMiddleware *nlogrus.Middleware
logger *logrus.Logger
)
func init() {
loggerMiddleware = nlogrus.NewMiddleware()
logger = loggerMiddleware.Logger
}
func main() {
// TODO: Should probably use `flag` package
if len(os.Args) != 2 {
logger.Println(os.Args)
logger.Fatalf("Invalid number of arguments. Usage: %s /path/to/db.json", os.Args[0])
}
parseJsonFile(os.Args[2])
port := ":" + os.Getenv("PORT")
if port == ":" {
port = ":3000"
}
StartServer(port)
}
func StartServer(addr string) {
router := httprouter.New()
addStaticRoutes(router)
addDynamicRoutes(router)
// This goroutine will flush the JSON to the db.json file every 30 seconds,
// OR before the application exits
go flushJson(JsonFilePath)
n := negroni.Classic()
n.Use(loggerMiddleware)
n.UseHandler(router)
n.Run(addr)
}