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.

76 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/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 `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 RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
  33. r.HandleFunc("/", IndexHandler)
  34. for _, pipeline := range pipelines {
  35. r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
  36. PipelineHandler(pipeline, storageProvider, w, r)
  37. })
  38. }
  39. }
  40. func main() {
  41. appSettings, err := settings.LoadSettings(afero.NewOsFs())
  42. if err != nil {
  43. panic(err)
  44. }
  45. var storageProvider storage.IStorageProvider
  46. if appSettings.StorageProvider.Type == 0 {
  47. storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "")
  48. } else {
  49. panic("Invalid file system provided!")
  50. }
  51. pipes := pipelines.LoadPipelines()
  52. authMiddleware := auth.CreateAuthenticationMiddleware(appSettings.Token)
  53. r := mux.NewRouter()
  54. r.Use(authMiddleware.Middleware)
  55. RegisterRoutes(r, pipes, storageProvider)
  56. err = http.ListenAndServe(appSettings.Endpoint, r)
  57. if err != nil {
  58. panic(err)
  59. }
  60. }