Aucune description

main.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "runtime"
  7. "github.com/codegangsta/negroni"
  8. "github.com/gorilla/mux"
  9. )
  10. const DBNAME = "satellizer"
  11. var (
  12. PORT = ":3000"
  13. MONGODB_URL = "localhost"
  14. )
  15. func init() {
  16. if p := os.Getenv("PORT"); p != "" {
  17. PORT = ":" + p
  18. }
  19. if m := os.Getenv("MONGODB_URL"); m != "" {
  20. MONGODB_URL = m
  21. }
  22. }
  23. func main() {
  24. runtime.GOMAXPROCS(runtime.NumCPU())
  25. dbSession := DBConnect(MONGODB_URL)
  26. DBEnsureIndices(dbSession)
  27. router := mux.NewRouter().StrictSlash(true)
  28. api := router.PathPrefix("/api").Subrouter()
  29. api.HandleFunc("/me", Me).Methods("GET")
  30. api.HandleFunc("/me", UpdateAccount).Methods("PUT")
  31. authApi := router.PathPrefix("/auth").Subrouter()
  32. authApi.HandleFunc("/login", Login).Methods("POST")
  33. authApi.HandleFunc("/signup", SignUp).Methods("POST")
  34. authApi.HandleFunc("/facebook", LoginWithFacebook).Methods("POST")
  35. authApi.HandleFunc("/twitter", LoginWithTwitter).Methods("POST")
  36. authApi.HandleFunc("/google", LoginWithGoogle).Methods("POST")
  37. n := negroni.Classic()
  38. n.Use(JWTMiddleware())
  39. n.Use(DBMiddleware(dbSession))
  40. n.Use(ParseFormMiddleware())
  41. n.UseHandler(router)
  42. static := router.PathPrefix("/").Subrouter()
  43. static.Methods("GET").Handler(http.FileServer(http.Dir("../../client")))
  44. fmt.Println("Launching server at http://localhost" + PORT)
  45. err := http.ListenAndServe(PORT, n)
  46. if err != nil {
  47. fmt.Println(err)
  48. }
  49. }