DRT bus information.
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.
 
 
 

48 lines
1.1 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "os"
  8. "git.geek1011.net/geek1011/drtapi/v1"
  9. "git.geek1011.net/geek1011/drtapi/v2"
  10. "github.com/julienschmidt/httprouter"
  11. "github.com/spf13/pflag"
  12. )
  13. var gitShortSHA = "unknown"
  14. func main() {
  15. addr := pflag.StringP("addr", "a", ":8001", "address to listen on")
  16. help := pflag.BoolP("help", "h", false, "show this message")
  17. pflag.Parse()
  18. if *help || pflag.NArg() != 0 {
  19. fmt.Fprintf(os.Stderr, "Usage: drtapi [OPTIONS]\n")
  20. fmt.Fprintf(os.Stderr, "\nOptions:\n")
  21. pflag.PrintDefaults()
  22. fmt.Fprintf(os.Stderr, "\n")
  23. os.Exit(1)
  24. }
  25. r := httprouter.New()
  26. r.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  27. w.Header().Set("Content-Type", "application/json")
  28. w.WriteHeader(404)
  29. io.WriteString(w, `{"status":"error","result":"not found"}`)
  30. })
  31. v1.Mount(r)
  32. v2.Mount(r)
  33. r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  34. http.Redirect(w, r, "/v2/", 301)
  35. })
  36. log.Printf("Serving on %s", *addr)
  37. log.Fatal("Error starting server: ", http.ListenAndServe(*addr, r))
  38. }