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.

55 lines
1.2 KiB

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