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.

74 lines
1.8 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/auth"
  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
  16. Version string
  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. settings := settings.LoadSettings(afero.NewOsFs())
  41. var storageProvider storage.IStorageProvider
  42. if settings.StorageProvider.Type == 0 {
  43. storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath, "")
  44. } else {
  45. panic("Invalid file system provided!")
  46. }
  47. pipes := pipelines.LoadPipelines()
  48. authMiddleware := auth.AuthenticationMiddleware{
  49. Secret: settings.Token,
  50. }
  51. r := mux.NewRouter()
  52. r.Use(authMiddleware.Middleware)
  53. r.HandleFunc("/", IndexHandler)
  54. RegisterPipelineRoutes(r, pipes, storageProvider)
  55. err := http.ListenAndServe(settings.Endpoint, r)
  56. if err != nil {
  57. panic(err)
  58. }
  59. }