-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
111 lines (92 loc) · 3.21 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/Chingu-cohorts/ChinguDevelopersNetwork/controllers"
"github.com/Chingu-cohorts/ChinguDevelopersNetwork/utils"
"github.com/julienschmidt/httprouter"
"github.com/phyber/negroni-gzip/gzip"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"
negroniprometheus "github.com/zbindenren/negroni-prometheus"
)
func main() {
// Load the config file
config, err := utils.LoadSettings("config.json")
if err != nil {
logrus.WithError(err)
}
// Setup CORS
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"Authorization", "Content-Type"},
ExposedHeaders: []string{"Authorization", "Content-Type"},
Debug: config.Debug,
})
// For some nice-to-have metrics
m := negroniprometheus.NewMiddleware("ChinguDevelopersNetwork")
// Instantiate router
r := httprouter.New()
// Prometheus route
r.Handler("GET", "/metrics", prometheus.Handler())
// Cohorts routes
r.GET("/api/cohorts", controllers.ListCohorts)
r.GET("/api/cohorts/:name", controllers.ShowCohort)
r.POST("/api/cohorts", utils.AuthRequest(controllers.CreateCohort))
// User routes
r.GET("/api/users", controllers.ListUsers)
r.GET("/api/users/:username", controllers.ShowUser)
r.POST("/api/users", controllers.CreateUser)
r.PUT("/api/users", utils.AuthRequest(controllers.UpdateUser))
r.POST("/api/users/login", controllers.Login)
r.DELETE("/api/users/:username", utils.AuthRequest(controllers.DeleteUser))
r.GET("/api/currentuser", utils.AuthRequest(controllers.CurrentUser))
// Projects routes
r.GET("/api/projects", controllers.ListProjects)
r.GET("/api/projects/:id", controllers.ShowProject)
r.POST("/api/projects", utils.AuthRequest(controllers.CreateProject))
// Posts routes
r.GET("/api/posts", controllers.ListPosts)
r.GET("/api/posts/:postID", controllers.ShowPost)
r.POST("/api/posts", utils.AuthRequest(controllers.CreatePost))
r.PUT("/api/posts/:postID", utils.AuthRequest(controllers.UpdatePost))
r.DELETE("/api/posts/:postID", utils.AuthRequest(controllers.DeletePost))
// Comments routes
r.POST("/api/posts/:postID/comments", utils.AuthRequest(controllers.CreateComment))
r.PUT("/api/posts/:postID/comments/:commentID", utils.AuthRequest(controllers.UpdateComment))
r.DELETE("/api/posts/:postID/comments/:commentID", utils.AuthRequest(controllers.DeleteComment))
// Panic recover, request/response logger, static file serving
n := negroni.Classic()
// Gzip responses
n.Use(gzip.Gzip(gzip.DefaultCompression))
// Use Cors
n.Use(c)
// Use Prometheus
n.Use(m)
// Use router instance
n.UseHandler(r)
// Temporary port to test on heroku
// If there is no env variable called port
// then use whatever is inside the config file
var port string
if os.Getenv("PORT") != "" {
port = ":" + os.Getenv("PORT")
} else {
port = config.Port
}
// Configure server
s := &http.Server{
Addr: port,
Handler: n,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
}