Go backend for tracking results from conway game of life soup search.
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.

api.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gorilla/mux"
  5. "fmt"
  6. "encoding/json"
  7. )
  8. type SpeciesResp struct {
  9. Name string
  10. FirstFound int
  11. LastFound int
  12. Population int
  13. Examples []string
  14. Hashes []string
  15. }
  16. // Returns first and last found of species, a list of "example soups" for the user to see in browser and the complete list of soups recorded in the db
  17. func GetSpecies(w http.ResponseWriter, r *http.Request) {
  18. vars := mux.Vars(r)
  19. species := vars["species"]
  20. genus := vars["genus"]
  21. family := vars["family"]
  22. // Start collecting data from sql db
  23. apgcode := family + genus + "_" + species
  24. life, err := SQLGetLife(apgcode)
  25. if err != nil {
  26. panic(err)
  27. }
  28. samples, err := SQLGetSamples(apgcode)
  29. if err != nil {
  30. panic(err)
  31. }
  32. // Examples should be the first found and the lastfound,
  33. // if the first found is the last found than skip it
  34. var examples []string
  35. if len(samples) == 1 {
  36. examples = append(examples, samples[0].Hash)
  37. } else {
  38. examples = append(examples, samples[0].Hash)
  39. examples = append(examples, samples[len(samples) - 1].Hash)
  40. }
  41. // Get list of all found hashes
  42. hashes := make([]string, len(samples))
  43. for i, s := range samples {
  44. hashes[i] = s.Hash
  45. }
  46. ret := SpeciesResp{
  47. Name: life.Apgcode,
  48. FirstFound: life.FirstFound,
  49. LastFound: life.LastFound,
  50. Population: life.Population,
  51. Examples: examples,
  52. Hashes: hashes,
  53. }
  54. b, err := json.Marshal(ret)
  55. if err != nil {
  56. panic(err)
  57. }
  58. fmt.Fprintf(w, "%s", string(b))
  59. }
  60. // Returns list of species in a genus ordered by population
  61. func GetGenus(w http.ResponseWriter, r *http.Request) {
  62. vars := mux.Vars(r)
  63. genus := vars["genus"]
  64. family := vars["family"]
  65. apgcode := family + genus
  66. ret, err := SQLGetGenus(apgcode)
  67. if err != nil {
  68. panic(err)
  69. }
  70. // No processing is needed here so we just send the json
  71. b, err := json.Marshal(ret)
  72. if err != nil {
  73. panic(err)
  74. }
  75. fmt.Fprintf(w, "%s", string(b))
  76. }
  77. func GetFamily(w http.ResponseWriter, r *http.Request) {
  78. vars := mux.Vars(r)
  79. family := vars["family"]
  80. ret, err := SQLGetFamily(family)
  81. if err != nil {
  82. panic(err)
  83. }
  84. // No processing is needed here so we just send the json
  85. b, err := json.Marshal(ret)
  86. if err != nil {
  87. panic(err)
  88. }
  89. fmt.Fprintf(w, "%s", string(b))
  90. }