main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "battlecamp-go-server/board"
  4. "fmt"
  5. "log"
  6. "net"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "golang.org/x/net/netutil"
  11. )
  12. func main() {
  13. fmt.Println("Board generator version 0.1")
  14. logFile, err := os.Create("server.log")
  15. if err == nil {
  16. log.SetOutput(logFile)
  17. } else {
  18. log.Println("ERROR: Cannot open log file, using console.")
  19. log.Printf("%v=n", err)
  20. }
  21. http.HandleFunc("/", gzipHandler(generateBoard))
  22. http.HandleFunc("/status", status)
  23. l, _ := net.Listen("tcp", ":8081")
  24. limitListerer := netutil.LimitListener(l, 100)
  25. log.Fatal(http.Serve(limitListerer, nil))
  26. }
  27. func generateBoard(w http.ResponseWriter, r *http.Request) {
  28. x, _ := strconv.Atoi(r.FormValue("x"))
  29. y, _ := strconv.Atoi(r.FormValue("y"))
  30. width, _ := strconv.Atoi(r.FormValue("width"))
  31. height, _ := strconv.Atoi(r.FormValue("height"))
  32. totalWidth, _ := strconv.Atoi(r.FormValue("totalWidth"))
  33. totalHeight, _ := strconv.Atoi(r.FormValue("totalHeight"))
  34. //log.Printf("Creating partial x=%v, y=%v, width=%v, height=%v, totalWidth=%v, totalHeight=%v", x, y, width, height, totalWidth, totalHeight)
  35. b := board.NewRegion(x, y, width, height, totalWidth, totalHeight)
  36. b.WriteData(w)
  37. }
  38. func status(w http.ResponseWriter, r *http.Request) {
  39. w.Write([]byte("ok"))
  40. }