main.go 1.2 KB

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