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.

75 lines
1.9 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. type Metadata struct {
  15. Name string `json:"name"`
  16. Version string `json:"version"`
  17. }
  18. func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) {
  19. w.Header().Set("Content-Type", "application/json")
  20. err := json.NewEncoder(w).Encode(pipeline)
  21. if err != nil {
  22. w.WriteHeader(http.StatusInternalServerError)
  23. }
  24. }
  25. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  26. w.Header().Set("Content-Type", "application/json")
  27. err := json.NewEncoder(w).Encode(Metadata{Name, Version})
  28. if err != nil {
  29. w.WriteHeader(http.StatusInternalServerError)
  30. }
  31. }
  32. func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
  33. for _, pipeline := range pipelines {
  34. r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
  35. PipelineHandler(pipeline, storageProvider, w, r)
  36. })
  37. }
  38. }
  39. func main() {
  40. appSettings, err := settings.LoadSettings(afero.NewOsFs())
  41. if err != nil {
  42. panic(err)
  43. }
  44. var storageProvider storage.IStorageProvider
  45. if appSettings.StorageProvider.Type == 0 {
  46. storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "")
  47. } else {
  48. panic("Invalid file system provided!")
  49. }
  50. pipes := pipelines.LoadPipelines()
  51. authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Token)
  52. r := mux.NewRouter()
  53. r.Use(authMiddleware.Middleware)
  54. r.HandleFunc("/", IndexHandler)
  55. RegisterPipelineRoutes(r, pipes, storageProvider)
  56. err = http.ListenAndServe(appSettings.Endpoint, r)
  57. if err != nil {
  58. panic(err)
  59. }
  60. }