Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package main
  2. import (
  3. "github.com/geplauder/lithium/controllers"
  4. "net/http"
  5. "os"
  6. "io"
  7. "github.com/geplauder/lithium/middlewares"
  8. "github.com/geplauder/lithium/pipelines"
  9. "github.com/geplauder/lithium/settings"
  10. "github.com/geplauder/lithium/storage"
  11. "github.com/gorilla/mux"
  12. "github.com/sirupsen/logrus"
  13. "github.com/spf13/afero"
  14. )
  15. var GitCommit string
  16. func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
  17. index := r.Methods(http.MethodGet).Subrouter()
  18. index.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  19. controllers.IndexHandler(pipelines, w, r, GitCommit)
  20. })
  21. upload := r.Methods(http.MethodPost).Subrouter()
  22. upload.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
  23. controllers.UploadHandler(w, r, pipelines, storageProvider)
  24. })
  25. files := r.Methods(http.MethodGet).Subrouter()
  26. files.HandleFunc("/files", func(w http.ResponseWriter, r *http.Request) {
  27. controllers.FileHandler(w, r, pipelines, storageProvider, appSettings)
  28. })
  29. pipeline := r.Methods(http.MethodGet).Subrouter()
  30. pipeline.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
  31. for _, pipeline := range pipelines {
  32. if pipeline.GetSlug() == mux.Vars(r)["pipeline"] {
  33. controllers.PipelineHandler(pipeline, storageProvider, w, r)
  34. return
  35. }
  36. }
  37. w.WriteHeader(404)
  38. })
  39. if appSettings.Authentication.Enabled {
  40. authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token)
  41. upload.Use(authMiddleware.Middleware)
  42. files.Use(authMiddleware.Middleware)
  43. pipeline.Use(authMiddleware.Middleware)
  44. }
  45. }
  46. func main() {
  47. logFile, err := os.OpenFile("lithium.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  48. if err == nil {
  49. multiWriter := io.MultiWriter(os.Stdout, logFile)
  50. logrus.SetOutput(multiWriter)
  51. } else {
  52. logrus.SetOutput(os.Stdout)
  53. }
  54. logrus.SetFormatter(&logrus.JSONFormatter{})
  55. appSettings, err := settings.LoadSettings(afero.NewOsFs())
  56. if err != nil {
  57. logrus.Fatal("Unexpected error while loading settings: ", err)
  58. }
  59. var storageProvider storage.IStorageProvider
  60. if appSettings.StorageProvider.Type == 0 {
  61. storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "")
  62. } else {
  63. logrus.WithFields(logrus.Fields{
  64. "StorageProviderType": appSettings.StorageProvider.Type,
  65. }).Fatal("Invalid file system provided")
  66. }
  67. pipes := pipelines.LoadPipelines()
  68. r := mux.NewRouter()
  69. if appSettings.RateLimiter.Enabled {
  70. rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst)
  71. if err != nil {
  72. logrus.Fatal("Unexpected error while creating rate limiter middleware: ", err)
  73. }
  74. r.Use(rateLimiterMiddleware.Middleware)
  75. }
  76. RegisterRoutes(r, appSettings, pipes, storageProvider)
  77. logrus.Info("Lithium started, listening for requests...")
  78. err = http.ListenAndServe(appSettings.Endpoint, r)
  79. if err != nil {
  80. logrus.Fatal("Unexpected error while serving http server: ", err)
  81. }
  82. }