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.

91 lines
2.3 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
3 years ago
3 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/geplauder/lithium/middlewares"
  6. "github.com/geplauder/lithium/pipelines"
  7. "github.com/geplauder/lithium/settings"
  8. "github.com/geplauder/lithium/storage"
  9. "github.com/gorilla/mux"
  10. "github.com/spf13/afero"
  11. )
  12. const Name string = "Lithium"
  13. const Version string = "0.1.0"
  14. var GitCommit string
  15. type Metadata struct {
  16. Name string `json:"name"`
  17. Version string `json:"version"`
  18. CommitHash string `json:"commit_hash"`
  19. }
  20. func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) {
  21. w.Header().Set("Content-Type", "application/json")
  22. err := json.NewEncoder(w).Encode(pipeline)
  23. if err != nil {
  24. w.WriteHeader(http.StatusInternalServerError)
  25. }
  26. }
  27. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  28. w.Header().Set("Content-Type", "application/json")
  29. err := json.NewEncoder(w).Encode(Metadata{Name, Version, GitCommit})
  30. if err != nil {
  31. w.WriteHeader(http.StatusInternalServerError)
  32. }
  33. }
  34. func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
  35. for _, pipeline := range pipelines {
  36. r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
  37. PipelineHandler(pipeline, storageProvider, w, r)
  38. })
  39. }
  40. }
  41. func main() {
  42. appSettings, err := settings.LoadSettings(afero.NewOsFs())
  43. if err != nil {
  44. panic(err)
  45. }
  46. var storageProvider storage.IStorageProvider
  47. if appSettings.StorageProvider.Type == 0 {
  48. storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "")
  49. } else {
  50. panic("Invalid file system provided!")
  51. }
  52. pipes := pipelines.LoadPipelines()
  53. r := mux.NewRouter()
  54. if appSettings.Authentication.Enabled {
  55. authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token)
  56. r.Use(authMiddleware.Middleware)
  57. }
  58. if appSettings.RateLimiter.Enabled {
  59. rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst)
  60. if err != nil {
  61. panic(err)
  62. }
  63. r.Use(rateLimiterMiddleware.Middleware)
  64. }
  65. r.HandleFunc("/", IndexHandler)
  66. RegisterPipelineRoutes(r, pipes, storageProvider)
  67. err = http.ListenAndServe(appSettings.Endpoint, r)
  68. if err != nil {
  69. panic(err)
  70. }
  71. }