Advertisement
lvs

Simple server

lvs
May 25th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.15 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "os"
  7.     "net"
  8.     "net/http"
  9.     "net/http/fcgi"
  10. )
  11.  
  12. var logPath string = "stat.log"
  13.  
  14. type FastCGIServer struct{}
  15. func (s FastCGIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  16.     w.Write([]byte("Game statistics.\n"))
  17.     if req.Method == "POST" {
  18.         w.Write([]byte("ID = " + req.FormValue("i") + "\n"))
  19.         w.Write([]byte("Version = " + req.FormValue("v") + "\n"))
  20.     }
  21.     w.Write([]byte("Game = " + req.FormValue("game") + "\n"))
  22.     // How to access logger inside this function?
  23.     //logger.Println("Game = " + req.FormValue("game"))
  24. }
  25.  
  26. func main() {
  27.     logFile, err := os.OpenFile(logPath, os.O_APPEND | os.O_RDWR, 0666)
  28.     if os.IsNotExist(err) {
  29.         // Create file if not exist and assign it to logFile variable - this doesn't work
  30.         logFile, _ := os.Create(logPath)
  31.     }
  32.     logger := log.New(logFile, "", log.Ldate | log.Ltime)
  33.     logger.Println("Start")
  34.  
  35.     fmt.Printf("Starting server")
  36.     l, _ := net.Listen("tcp", "127.0.0.1:9001")
  37.     b := new(FastCGIServer)
  38.     fcgi.Serve(l, b)
  39.  
  40.     fmt.Printf("Server stopped")
  41.     logFile.Close()
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement