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.

60 lines
1.5 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/storage"
  7. "github.com/gorilla/mux"
  8. )
  9. const Name string = "Lithium"
  10. const Version string = "0.1.0"
  11. type Metadata struct {
  12. Name string
  13. Version string
  14. }
  15. func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) {
  16. w.Header().Set("Content-Type", "application/json")
  17. err := json.NewEncoder(w).Encode(pipeline)
  18. if err != nil {
  19. w.WriteHeader(http.StatusInternalServerError)
  20. }
  21. }
  22. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  23. w.Header().Set("Content-Type", "application/json")
  24. err := json.NewEncoder(w).Encode(Metadata{Name, Version})
  25. if err != nil {
  26. w.WriteHeader(http.StatusInternalServerError)
  27. }
  28. }
  29. func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
  30. for _, pipeline := range pipelines {
  31. r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
  32. PipelineHandler(pipeline, storageProvider, w, r)
  33. })
  34. }
  35. }
  36. func main() {
  37. storageProvider := storage.GetFileSystemStorageProvider("test")
  38. storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10})
  39. pipes := pipelines.LoadPipelines()
  40. r := mux.NewRouter()
  41. r.HandleFunc("/", IndexHandler)
  42. RegisterPipelineRoutes(r, pipes, storageProvider)
  43. err := http.ListenAndServe(":8000", r)
  44. if err != nil {
  45. panic(err)
  46. }
  47. }