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.

68 lines
1.7 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
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/geplauder/lithium/pipelines"
  6. "github.com/geplauder/lithium/settings"
  7. "github.com/geplauder/lithium/storage"
  8. "github.com/gorilla/mux"
  9. "github.com/spf13/afero"
  10. )
  11. const Name string = "Lithium"
  12. const Version string = "0.1.0"
  13. type Metadata struct {
  14. Name string
  15. Version string
  16. }
  17. func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) {
  18. w.Header().Set("Content-Type", "application/json")
  19. err := json.NewEncoder(w).Encode(pipeline)
  20. if err != nil {
  21. w.WriteHeader(http.StatusInternalServerError)
  22. }
  23. }
  24. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  25. w.Header().Set("Content-Type", "application/json")
  26. err := json.NewEncoder(w).Encode(Metadata{Name, Version})
  27. if err != nil {
  28. w.WriteHeader(http.StatusInternalServerError)
  29. }
  30. }
  31. func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
  32. for _, pipeline := range pipelines {
  33. r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
  34. PipelineHandler(pipeline, storageProvider, w, r)
  35. })
  36. }
  37. }
  38. func main() {
  39. settings := settings.LoadSettings(afero.NewOsFs())
  40. var storageProvider storage.IStorageProvider
  41. if settings.StorageProvider.Type == 0 {
  42. storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath, "")
  43. } else {
  44. panic("Invalid file system provided!")
  45. }
  46. pipes := pipelines.LoadPipelines()
  47. r := mux.NewRouter()
  48. r.HandleFunc("/", IndexHandler)
  49. RegisterPipelineRoutes(r, pipes, storageProvider)
  50. err := http.ListenAndServe(settings.Endpoint, r)
  51. if err != nil {
  52. panic(err)
  53. }
  54. }