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.

63 lines
1.6 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/auth"
  6. "github.com/geplauder/lithium/pipelines"
  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. storageProvider := storage.GetFileSystemStorageProvider("test", "")
  39. storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10})
  40. pipes := pipelines.LoadPipelines()
  41. authMiddleware := auth.CreateAuthenticationMiddleware(settings.Token)
  42. r := mux.NewRouter()
  43. r.Use(authMiddleware.Middleware)
  44. r.HandleFunc("/", IndexHandler)
  45. RegisterPipelineRoutes(r, pipes, storageProvider)
  46. err := http.ListenAndServe(":8000", r)
  47. if err != nil {
  48. panic(err)
  49. }
  50. }