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.

84 lines
2.2 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. authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Token)
  54. rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst)
  55. if err != nil {
  56. panic(err)
  57. }
  58. r := mux.NewRouter()
  59. r.Use(authMiddleware.Middleware)
  60. r.Use(rateLimiterMiddleware.Middleware)
  61. r.HandleFunc("/", IndexHandler)
  62. RegisterPipelineRoutes(r, pipes, storageProvider)
  63. err = http.ListenAndServe(appSettings.Endpoint, r)
  64. if err != nil {
  65. panic(err)
  66. }
  67. }