config.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package geoip
  2. import (
  3. "github.com/mholt/caddy"
  4. )
  5. // Config specifies configuration parsed for Caddyfile
  6. type Config struct {
  7. DatabasePath string
  8. // Yout can set returned header names in config
  9. // Country
  10. HeaderNameCountryCode string
  11. HeaderNameCountryIsEU string
  12. HeaderNameCountryName string
  13. // City
  14. HeaderNameCityName string
  15. // Location
  16. HeaderNameLocationLat string
  17. HeaderNameLocationLon string
  18. HeaderNameLocationTimeZone string
  19. }
  20. // NewConfig initialize new Config with default values
  21. func NewConfig() Config {
  22. c := Config{}
  23. c.HeaderNameCountryCode = "X-Geoip-Country-Code"
  24. c.HeaderNameCountryIsEU = "X-Geoip-Country-Eu"
  25. c.HeaderNameCountryName = "X-Geoip-Country-Name"
  26. c.HeaderNameCityName = "X-Geoip-City-Name"
  27. c.HeaderNameLocationLat = "X-Geoip-Location-Lat"
  28. c.HeaderNameLocationLon = "X-Geoip-Location-Lon"
  29. c.HeaderNameLocationTimeZone = "X-Geoip-Location-Tz"
  30. return c
  31. }
  32. func parseConfig(c *caddy.Controller) (Config, error) {
  33. var config = NewConfig()
  34. for c.Next() {
  35. for c.NextBlock() {
  36. value := c.Val()
  37. switch value {
  38. case "database":
  39. if !c.NextArg() {
  40. continue
  41. }
  42. config.DatabasePath = c.Val()
  43. case "set_header_country_code":
  44. if !c.NextArg() {
  45. continue
  46. }
  47. config.HeaderNameCountryCode = c.Val()
  48. case "set_header_country_name":
  49. if !c.NextArg() {
  50. continue
  51. }
  52. config.HeaderNameCountryName = c.Val()
  53. case "set_header_country_eu":
  54. if !c.NextArg() {
  55. continue
  56. }
  57. config.HeaderNameCountryIsEU = c.Val()
  58. case "set_header_city_name":
  59. if !c.NextArg() {
  60. continue
  61. }
  62. config.HeaderNameCityName = c.Val()
  63. case "set_header_location_lat":
  64. if !c.NextArg() {
  65. continue
  66. }
  67. config.HeaderNameLocationLat = c.Val()
  68. case "set_header_location_lon":
  69. if !c.NextArg() {
  70. continue
  71. }
  72. config.HeaderNameLocationLon = c.Val()
  73. case "set_header_location_tz":
  74. if !c.NextArg() {
  75. continue
  76. }
  77. config.HeaderNameLocationTimeZone = c.Val()
  78. }
  79. }
  80. }
  81. return config, nil
  82. }