func main() { if buf, err := ioutil.ReadFile("freegeoip.conf"); err != nil { panic(err) } else { conf = &Settings{} if err := xml.Unmarshal(buf, conf); err != nil { panic(err) } } http.Handle("/", http.FileServer(http.Dir(conf.DocumentRoot))) h := GeoipHandler() http.HandleFunc("/csv/", h) http.HandleFunc("/xml/", h) http.HandleFunc("/json/", h) server := http.Server{ Addr: conf.Addr, Handler: httpxtra.Handler{ Logger: logger, XHeaders: conf.XHeaders, }, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, } log.Println("FreeGeoIP server starting") if e := httpxtra.ListenAndServe(server); e != nil { log.Println(e.Error()) } }
func main() { cf := flag.String("config", "freegeoip.conf", "set config file") flag.Parse() if buf, err := ioutil.ReadFile(*cf); err != nil { log.Fatal(err) } else { conf = &Settings{} if err := xml.Unmarshal(buf, conf); err != nil { log.Fatal(err) } } templates = template.Must(template.ParseGlob(fmt.Sprintf("%s/template/*.html", conf.DocumentRoot))) http.Handle("/", http.FileServer(http.Dir(conf.DocumentRoot))) h := GeoipHandler() http.HandleFunc("/csv/", h) http.HandleFunc("/xml/", h) http.HandleFunc("/json/", h) http.HandleFunc("/map/", h) server := http.Server{ Addr: conf.Addr, Handler: httpxtra.Handler{ Logger: logger, XHeaders: conf.XHeaders, }, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, } log.Printf("FreeGeoIP server starting on %s (xheaders=%t)", conf.Addr, conf.XHeaders) if e := httpxtra.ListenAndServe(server); e != nil { log.Println(e.Error()) } }
func runServer(mux *http.ServeMux, c *serverConfig) { h := httpxtra.Handler{ Handler: mux, XHeaders: c.XHeaders, } if c.Log { h.Logger = httpLogger } s := http.Server{ Addr: c.Addr, Handler: h, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, } if len(c.KeyFile) > 0 && len(c.CertFile) > 0 { log.Printf("Starting HTTPS server on tcp/%s "+ "log=%t xheaders=%t cert=%s key=%s", c.Addr, c.Log, c.XHeaders, c.CertFile, c.KeyFile, ) log.Fatal(s.ListenAndServeTLS(c.CertFile, c.KeyFile)) } else { log.Printf("Starting HTTP server on tcp/%s "+ "log=%t xheaders=%t", c.Addr, c.Log, c.XHeaders, ) log.Fatal(httpxtra.ListenAndServe(s)) } }
func main() { cf := flag.String("config", "freegeoip.conf", "set config file") flag.Parse() if buf, err := ioutil.ReadFile(*cf); err != nil { log.Fatal(err) } else { conf = &Settings{} if err := xml.Unmarshal(buf, conf); err != nil { log.Fatal(err) } } runtime.GOMAXPROCS(runtime.NumCPU()) log.Printf("FreeGeoIP server starting. debug=%t", conf.Debug) http.Handle("/", http.FileServer(http.Dir(conf.DocumentRoot))) h := GeoipHandler() http.HandleFunc("/csv/", h) http.HandleFunc("/xml/", h) http.HandleFunc("/json/", h) wg := new(sync.WaitGroup) for _, l := range conf.Listen { if l.Addr == "" { continue } wg.Add(1) h := httpxtra.Handler{XHeaders: l.XHeaders} if l.Log { h.Logger = logger } s := http.Server{ Addr: l.Addr, Handler: h, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, } if l.KeyFile == "" { log.Printf("Listening HTTP on %s "+ "log=%t xheaders=%t", l.Addr, l.Log, l.XHeaders) go func() { log.Fatal(httpxtra.ListenAndServe(s)) }() } else { log.Printf("Listening HTTPS on %s "+ "log=%t xheaders=%t cert=%s key=%s", l.Addr, l.Log, l.XHeaders, l.CertFile, l.KeyFile) go func() { log.Fatal(s.ListenAndServeTLS( l.CertFile, l.KeyFile, )) }() } } wg.Wait() }
func main() { var err error cfgfile := flag.String("config", "server.conf", "set config file") flag.Parse() Config, err = ReadConfig(*cfgfile) if err != nil { log.Fatal(err) } // Set up databases Redis = redis.New(Config.Redis) MySQL, err = sql.Open("mysql", Config.MySQL) if err != nil { log.Fatal(err) } // Set up routing and print server info route() hello() // Run HTTP and HTTPS servers wg := &sync.WaitGroup{} if Config.HTTP.Addr != "" { wg.Add(1) log.Printf("Starting HTTP server on %s", Config.HTTP.Addr) go func() { // Use httpxtra's listener to support Unix sockets. server := http.Server{ Addr: Config.HTTP.Addr, Handler: httpxtra.Handler{ Logger: logger, XHeaders: Config.HTTP.XHeaders, }, } log.Fatal(httpxtra.ListenAndServe(server)) //wg.Done() }() } if Config.HTTPS.Addr != "" { wg.Add(1) log.Printf("Starting HTTPS server on %s", Config.HTTPS.Addr) go func() { server := http.Server{ Addr: Config.HTTPS.Addr, Handler: httpxtra.Handler{Logger: logger}, } log.Fatal(server.ListenAndServeTLS( Config.HTTPS.CrtFile, Config.HTTPS.KeyFile)) //wg.Done() }() } wg.Wait() }
func main() { http.Handle("/", http.FileServer(http.Dir(staticpath))) h := GeoipHandler() http.HandleFunc("/csv/", h) http.HandleFunc("/xml/", h) http.HandleFunc("/json/", h) server := http.Server{ Addr: addr, Handler: httpxtra.Handler{Logger: logger}, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, } log.Println("FreeGeoIP server starting") if e := httpxtra.ListenAndServe(server); e != nil { log.Println(e.Error()) } }
func main() { http.HandleFunc("/", IndexHandler) // Setup the custom handler handler := httpxtra.Handler{ Logger: logger, XHeaders: true, } // Setup the server s := http.Server{ Addr: "./test.sock", // Listen on Unix Socket Handler: handler, // Custom httpxtra.Handler } // ListenAndServe fails with "address already in use" if the socket // file exists. syscall.Unlink("./test.sock") // Use our custom listener log.Fatal(httpxtra.ListenAndServe(s)) }
func main() { var err error cfgfile := flag.String("config", "config.xml", "set config file") sessKey := flag.Bool("keygen", false, "dump random key and exit") flag.Parse() if *sessKey { fmt.Println(RandHex(16)) return } Config, err = ReadConfig(*cfgfile) if err != nil { log.Fatal(err) } // Load templates Tmpl = template.Must(template.ParseGlob(Config.TemplatesDirectory)) if err != nil { log.Fatal(err) } // Set up databases Redis = redis.New(Config.Redis) MySQL, err = sql.Open("mysql", Config.MySQL) if err != nil { log.Fatal(err) } // Set up session keys Session = sessions.NewCookieStore( Config.Session.AuthKey, Config.Session.CryptKey) // Set up routing and print server info route() hello() // Run HTTP and HTTPS servers wg := &sync.WaitGroup{} if Config.HTTP.Addr != "" { wg.Add(1) log.Printf("Starting HTTP server on %s", Config.HTTP.Addr) go func() { // Use httpxtra's listener to support Unix sockets. server := http.Server{ Addr: Config.HTTP.Addr, Handler: httpxtra.Handler{ Logger: logger, XHeaders: Config.HTTP.XHeaders, }, } log.Fatal(httpxtra.ListenAndServe(server)) wg.Done() }() } if Config.HTTPS.Addr != "" { wg.Add(1) log.Printf("Starting HTTPS server on %s", Config.HTTPS.Addr) go func() { server := http.Server{ Addr: Config.HTTPS.Addr, Handler: httpxtra.Handler{Logger: logger}, } log.Fatal(server.ListenAndServeTLS( Config.HTTPS.CrtFile, Config.HTTPS.KeyFile)) wg.Done() }() } wg.Wait() }
func main() { cf := flag.String("config", "freegeoip.conf", "set config file") prof := flag.Bool("profile", false, "run cpu and mem profiling") flag.Parse() if buf, err := ioutil.ReadFile(*cf); err != nil { log.Fatal(err) } else { conf = &ConfigFile{} if err := xml.Unmarshal(buf, conf); err != nil { log.Fatal(err) } } if *prof { profile() } runtime.GOMAXPROCS(runtime.NumCPU()) log.Printf("FreeGeoIP server starting. debug=%t", conf.Debug) if conf.Debug && len(conf.DebugSrv) > 0 { go func() { // server for expvar's /debug/vars only log.Printf("Starting DEBUG HTTP server on %s", conf.DebugSrv) log.Fatal(http.ListenAndServe(conf.DebugSrv, nil)) }() } mux := http.NewServeMux() mux.Handle("/", http.FileServer(http.Dir(conf.DocumentRoot))) h := LookupHandler() mux.HandleFunc("/csv/", h) mux.HandleFunc("/xml/", h) mux.HandleFunc("/json/", h) wg := new(sync.WaitGroup) for _, l := range conf.Listen { if l.Addr == "" { continue } wg.Add(1) h := httpxtra.Handler{Handler: mux, XHeaders: l.XHeaders} if l.Log { h.Logger = logger } s := http.Server{ Addr: l.Addr, Handler: h, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, } if l.KeyFile == "" && l.CertFile == "" { log.Printf("Starting HTTP server on %s "+ "log=%t xheaders=%t", l.Addr, l.Log, l.XHeaders) go func() { log.Fatal(httpxtra.ListenAndServe(s)) }() } else { log.Printf("Starting HTTPS server on %s "+ "log=%t xheaders=%t cert=%s key=%s", l.Addr, l.Log, l.XHeaders, l.CertFile, l.KeyFile) go func() { log.Fatal(s.ListenAndServeTLS( l.CertFile, l.KeyFile, )) }() } } wg.Wait() }