| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package main
- import (
- "battlecamp-go/board"
- "flag"
- "fmt"
- "log"
- "net"
- "net/http"
- "os"
- "runtime/pprof"
- "strconv"
- "golang.org/x/net/netutil"
- )
- var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
- func main() {
- fmt.Println("Board generator version 0.1")
- logFile, err := os.Create("server.log")
- if err == nil {
- log.SetOutput(logFile)
- } else {
- log.Println("ERROR: Cannot open log file, using console.")
- log.Printf("%v=n", err)
- }
- initCpuProfiler()
- http.HandleFunc("/", gzipHandler(generateBoard))
- http.HandleFunc("/status", status)
- http.HandleFunc("/debug/profiledump", profileDump)
- l, _ := net.Listen("tcp", ":8081")
- limitListerer := netutil.LimitListener(l, 100)
- log.Fatal(http.Serve(limitListerer, nil))
- }
- func generateBoard(w http.ResponseWriter, r *http.Request) {
- x, _ := strconv.Atoi(r.FormValue("x"))
- y, _ := strconv.Atoi(r.FormValue("y"))
- width, _ := strconv.Atoi(r.FormValue("width"))
- height, _ := strconv.Atoi(r.FormValue("height"))
- totalWidth, _ := strconv.Atoi(r.FormValue("totalWidth"))
- totalHeight, _ := strconv.Atoi(r.FormValue("totalHeight"))
- //log.Printf("Creating partial x=%v, y=%v, width=%v, height=%v, totalWidth=%v, totalHeight=%v", x, y, width, height, totalWidth, totalHeight)
- b := board.NewRegion(x, y, width, height, totalWidth, totalHeight)
- b.WriteData(w)
- }
- func status(w http.ResponseWriter, r *http.Request) {
- w.Write([]byte("ok"))
- }
- func profileDump(w http.ResponseWriter, req *http.Request) {
- log.Println("Dumping cpu profile")
- pprof.StopCPUProfile()
- }
- func initCpuProfiler() {
- flag.Parse()
- if *cpuprofile != "" {
- f, err := os.Create(*cpuprofile)
- if err != nil {
- log.Printf("ERROR: %v\n", err)
- } else {
- pprof.StartCPUProfile(f)
- }
- }
- }
|