Fieldnotes app backend
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.

main.go 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "net/http"
  6. "os"
  7. "github.com/dannyvankooten/extemplate"
  8. "github.com/go-chi/chi/middleware"
  9. "github.com/go-chi/chi"
  10. )
  11. //nolint: gochecknoglobals
  12. var (
  13. address = ":3000"
  14. staticDir = "./static"
  15. tiffDir = "./tiff"
  16. templateDir = "template/"
  17. nomenclaturesPath = "data/nomenclatures.json"
  18. xt = extemplate.New()
  19. )
  20. func init() { //nolint: gochecknoinits
  21. flag.StringVar(&address, "address", address, "Server HTTP address")
  22. flag.StringVar(&staticDir, "static", staticDir, "Static files directory")
  23. flag.StringVar(&tiffDir, "tiff", tiffDir, "Tiff files directory")
  24. flag.StringVar(&templateDir, "template", templateDir, "Template files directory")
  25. flag.StringVar(&nomenclaturesPath, "nomenclature", nomenclaturesPath, "Nomenclatures file path")
  26. }
  27. func main() {
  28. flag.Parse()
  29. if err := os.MkdirAll(tiffDir, os.FileMode(0740)); err != nil {
  30. panic(err)
  31. }
  32. if err := xt.ParseDir(templateDir, []string{".tmpl"}); err != nil {
  33. panic(err)
  34. }
  35. r := chi.NewRouter()
  36. r.Use(middleware.Logger)
  37. r.Use(middleware.Recoverer)
  38. r.Group(func(r chi.Router) {
  39. username := os.Getenv("FIELDNOTES_USERNAME")
  40. password := os.Getenv("FIELDNOTES_PASSWORD")
  41. r.Use(BasicAuth(username, password))
  42. r.Get("/", showHomePage)
  43. r.Get("/geotiff-explorer", showGeoTiffExplorerPage)
  44. r.Route("/field-surveys", func(r chi.Router) {
  45. r.Get("/editor", showNomenclatureEditorPage)
  46. r.Post("/nomenclatures.json", saveNomenclatures)
  47. })
  48. r.Route("/tiff-optimizer", func(r chi.Router) {
  49. r.Get("/", showTiffOptimizerPage)
  50. r.With(middleware.Throttle(1)).Post("/", uploadImage)
  51. r.Get("/results/{ID}", downloadResult)
  52. })
  53. r.Get("/*", http.FileServer(http.Dir(staticDir)).ServeHTTP)
  54. })
  55. r.Group(func(r chi.Router) {
  56. r.Get("/field-surveys/nomenclatures.json", downloadNomenclatures)
  57. })
  58. log.Printf("listening on '%s'", address)
  59. if err := http.ListenAndServe(address, r); err != nil {
  60. panic(err)
  61. }
  62. }