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() { 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() }