PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/cmd/orgo/main.go

https://gitlab.com/rvaz/orgo
Go | 79 lines | 62 code | 15 blank | 2 comment | 3 complexity | 19385424f50fafd5e59a1f2b8a88fa16 MD5 | raw file
  1. package main
  2. import (
  3. "context"
  4. "net/http"
  5. log "github.com/Sirupsen/logrus"
  6. oauth2google "golang.org/x/oauth2/google"
  7. calendar "google.golang.org/api/calendar/v3"
  8. oauth2api "google.golang.org/api/oauth2/v2"
  9. "github.com/gorilla/sessions"
  10. "github.com/joeshaw/envdecode"
  11. "gitlab.com/rvaz/orgo/conf"
  12. "gitlab.com/rvaz/orgo/dropbox"
  13. "gitlab.com/rvaz/orgo/google"
  14. "gitlab.com/rvaz/orgo/web"
  15. "gitlab.com/rvaz/orgo/work"
  16. "golang.org/x/oauth2"
  17. )
  18. func main() {
  19. var (
  20. cfg conf.Config
  21. urls map[string]string
  22. )
  23. err := envdecode.Decode(&cfg)
  24. if err != nil {
  25. log.Fatal(err.Error())
  26. }
  27. ctx := context.Background()
  28. store := sessions.NewCookieStore([]byte(cfg.HTTPCookieSecret))
  29. googleOauth := &oauth2.Config{
  30. ClientID: cfg.Google.APIKey,
  31. ClientSecret: cfg.Google.APISecret,
  32. RedirectURL: cfg.Google.RedirectURL,
  33. Endpoint: oauth2google.Endpoint,
  34. Scopes: []string{calendar.CalendarScope, oauth2api.UserinfoEmailScope, oauth2api.UserinfoProfileScope},
  35. }
  36. dropboxOauth := &oauth2.Config{
  37. ClientID: cfg.Dropbox.APIKey,
  38. ClientSecret: cfg.Dropbox.APISecret,
  39. RedirectURL: cfg.Dropbox.RedirectURL,
  40. Endpoint: oauth2.Endpoint{
  41. AuthURL: "https://www.dropbox.com/1/oauth2/authorize",
  42. TokenURL: "https://api.dropbox.com/1/oauth2/token",
  43. },
  44. }
  45. // Buffered work chan for async producers
  46. worker := work.NewWorker(googleOauth, dropboxOauth)
  47. go worker.WaitWork()
  48. dropboxHandler := dropbox.NewDropboxHandler(dropboxOauth, worker.WorkChan, store)
  49. googleHandler := google.NewGoogleHandler(googleOauth, store)
  50. urls = map[string]string{
  51. "Dropbox": dropboxHandler.AuthCodeURL(),
  52. "Google": googleHandler.AuthCodeURL(),
  53. }
  54. handler := web.NewHandler(ctx, store, urls)
  55. // Default handler
  56. http.HandleFunc("/dropbox/webhook", dropboxHandler.WebhookHandler)
  57. http.HandleFunc("/dropbox/oauth", dropboxHandler.OauthHandler)
  58. http.HandleFunc("/google/oauth", googleHandler.OauthHandler)
  59. fs := http.FileServer(http.Dir("static"))
  60. http.Handle("/static/", http.StripPrefix("/static/", fs))
  61. templateHandler := http.HandlerFunc(handler.TemplateHandler)
  62. http.Handle("/", handler.IndexMiddleware(templateHandler))
  63. log.Fatal(http.ListenAndServe(":8080", nil).Error())
  64. }