Exemple #1
0
func main() {
	http.HandleFunc("/", IndexHandler)

	// Setup the custom handler
	handler := httpxtra.Handler{
		Logger:   logger,
		XHeaders: true,
	}

	// Setup the server
	server := 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
	if e := httpxtra.ListenAndServe(server); e != nil {
		fmt.Println(e.Error())
	}
}
Exemple #2
0
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(24))
		return
	}
	Config, err = ReadConfig(*cfgfile)
	if err != nil {
		log.Fatal(err)
	}
	// Load templates
	Tmpl, err = LoadTemplates(Config.TemplatePath, "_base.html")
	if err != nil {
		log.Fatal(err)
	}
	// Set up databases
	Redis = redis.New(Config.Redis)
	MySQL, err = sql.Open("mymysql", Config.MySQL)
	if err != nil {
		log.Fatal(err)
	}
	// Set up session keys
	Session = sessions.NewCookieStore(Config.SessionKey)
	// Public handlers
	http.HandleFunc("/", IndexHandler)
	http.HandleFunc("/static/", StaticHandler)
	http.HandleFunc("/legal.txt", StaticHandler)
	http.HandleFunc("/favicon.ico", StaticHandler)
	// Sign Up
	http.HandleFunc("/signup/", https(unauthenticated(SignUpHandler)))
	http.HandleFunc("/signup/confirm/", SignUpConfirmHandler)
	// Sign In and Out
	http.HandleFunc("/signin/", https(unauthenticated(SignInHandler)))
	http.HandleFunc("/signout/", SignOutHandler)
	// Lost password
	http.HandleFunc("/recovery/", https(unauthenticated(RecoveryHandler)))
	http.HandleFunc("/recovery/confirm/", RecoveryConfirmHandler)
	// Signed In handlers
	http.HandleFunc("/main/", authenticated(MainHandler))
	http.HandleFunc("/settings/", https(authenticated(SettingsHandler)))
	// Custom Handler
	handler := httpxtra.Handler{
		Logger:   logger,
		XHeaders: Config.XHeaders,
	}
	// HTTP Server
	server := http.Server{
		Addr:    Config.Addr,
		Handler: handler,
	}
	numCPU := runtime.NumCPU()
	label := "CPU"
	if numCPU > 1 {
		label += "s"
	}
	runtime.GOMAXPROCS(numCPU)
	log.Printf("%s v%s (%d %s)", APPNAME, VERSION, numCPU, label)
	wg := &sync.WaitGroup{}
	if Config.Addr != "" {
		wg.Add(1)
		log.Printf("Starting HTTP server on %s", Config.Addr)
		go func() {
			// Use our listener to support Unix sockets.
			log.Fatal(httpxtra.ListenAndServe(server))
			wg.Done()
		}()
	}
	if Config.SSL.Addr != "" {
		wg.Add(1)
		log.Printf("Starting HTTPS server on %s", Config.SSL.Addr)
		go func() {
			https := server
			https.Addr = Config.SSL.Addr
			// No Unix sockets for HTTPS. duh!
			log.Fatal(https.ListenAndServeTLS(
				Config.SSL.CertFile, Config.SSL.KeyFile))
			wg.Done()
		}()
	}
	wg.Wait()
}
Exemple #3
0
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()
}