main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "golang.org/x/net/netutil"
  6. "battlecamp-go-server/board"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strconv"
  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("/", 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. }