This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eden.go
78 lines (65 loc) · 2.23 KB
/
eden.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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/fortytw2/eden/api"
"github.com/fortytw2/eden/datastore"
"github.com/fortytw2/eden/datastore/pgsql"
"github.com/fortytw2/eden/datastore/pgsql/queries"
"github.com/fortytw2/eden/datastore/redis"
"github.com/fortytw2/eden/web"
"github.com/julienschmidt/httprouter"
// autoload ENV from .env
_ "github.com/joho/godotenv/autoload"
)
func main() {
log.Println("eden: initializing")
log.Printf("eden: loaded %d sql queries\n", len(queries.All()))
data := constructDatastore()
router := httprouter.New()
router.GET("/", web.Homepage)
router.GET("/b/", api.GetBoards(data))
router.POST("/b/", api.CreateBoard(data))
router.GET("/b/:board/", api.GetBoardPosts(data))
router.GET("/b/:board/p/:post_id", api.GetPost(data))
router.POST("/b/:board/p/", api.CreatePost(data))
router.POST("/b/:board/p/:post_id/v/:vote", api.UpvotePost(data))
// router.POST("/b/:board/:id/down", api.DownvotePost(data))
router.POST("/b/:board/p/:post_id/c/", api.CreateComment(data))
router.POST("/b/:board/p/:post_id/c/:comment_id/v/:vote", api.VoteComment(data))
// router.POST("/b/:board/:post_id/:comment_id/down", api.DownvoteComment(data))
router.POST("/u/", api.NewUser(data))
// router.GET("/u/:username")
log.Println("eden: now listening on port", os.Getenv("PORT"))
err := http.ListenAndServe(os.Getenv("PORT"), httpLogger(router))
if err != nil {
panic(err)
}
}
// cleanly log all HTTP requests
func httpLogger(router http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
startTime := time.Now()
router.ServeHTTP(w, req)
finishTime := time.Now()
elapsedTime := finishTime.Sub(startTime)
log.Println("eden:", req.Method, req.URL, elapsedTime)
})
}
// build our production datastore
func constructDatastore() *datastore.Datastore {
db, err := pgsql.NewDBHandle()
if err != nil {
log.Fatalf("eden: fatal error getting DB handle, %s\n", err)
}
r := redis.GetRedisPool()
return &datastore.Datastore{
UserService: pgsql.NewUserService(db),
BoardService: pgsql.NewBoardService(db),
PostService: pgsql.NewPostService(db),
CommentService: pgsql.NewCommentService(db),
VoteService: redis.NewVoteService(r),
}
}