Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "log"
- "os"
- "net"
- "net/http"
- "net/http/fcgi"
- )
- var logPath string = "stat.log"
- type FastCGIServer struct{}
- func (s FastCGIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
- w.Write([]byte("Game statistics.\n"))
- if req.Method == "POST" {
- w.Write([]byte("ID = " + req.FormValue("i") + "\n"))
- w.Write([]byte("Version = " + req.FormValue("v") + "\n"))
- }
- w.Write([]byte("Game = " + req.FormValue("game") + "\n"))
- // How to access logger inside this function?
- //logger.Println("Game = " + req.FormValue("game"))
- }
- func main() {
- logFile, err := os.OpenFile(logPath, os.O_APPEND | os.O_RDWR, 0666)
- if os.IsNotExist(err) {
- // Create file if not exist and assign it to logFile variable - this doesn't work
- logFile, _ := os.Create(logPath)
- }
- logger := log.New(logFile, "", log.Ldate | log.Ltime)
- logger.Println("Start")
- fmt.Printf("Starting server")
- l, _ := net.Listen("tcp", "127.0.0.1:9001")
- b := new(FastCGIServer)
- fcgi.Serve(l, b)
- fmt.Printf("Server stopped")
- logFile.Close()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement