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.

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