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.

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