示例#1
0
func TestOpenStorage(t *testing.T) {
	err := storage.OpenStorage("rter", "j2pREch8", "tcp", "localhost:3306", "rter")

	if err != nil {
		t.Fatal(err)
	}
}
示例#2
0
文件: rter.go 项目: falahhaprak/rter
func main() {
	flag.Parse()

	setupLogger()
	setupRterDir()

	var uploadPath = filepath.Join(*rterDir, "uploads")
	var wwwPath = filepath.Join(*rterDir, "www")

	log.Println("Launching rtER Server")

	err := storage.OpenStorage("rter", "j2pREch8", "tcp", "localhost:3306", "rter")

	if err != nil {
		log.Fatalf("Failed to open connection to database %v", err)
	}
	defer storage.CloseStorage()

	// First setup the subrouters

	r := mux.NewRouter().StrictSlash(true)

	sr := streaming.NewStreamingRouter()

	sr.Debug(*sockDebugFlag)

	if *sockDebugFlag { // Should be run after setupLogger() since it depends on setting up logfile
		log.Println("\t-Sock Debug Flag Set")
	}

	r.PathPrefix("/1.0/streaming").Handler(http.StripPrefix("/1.0/streaming", sr)) // Must register more specific paths first

	r.PathPrefix("/1.0").Handler(http.StripPrefix("/1.0", rest.CRUDRouter())) // Less specific paths later

	// Hand static files

	r.PathPrefix("/uploads").Handler(http.StripPrefix("/uploads", http.FileServer(http.Dir(uploadPath))))

	r.PathPrefix("/css").Handler(http.StripPrefix("/css", http.FileServer(http.Dir(filepath.Join(wwwPath, "css")))))
	r.PathPrefix("/js").Handler(http.StripPrefix("/js", http.FileServer(http.Dir(filepath.Join(wwwPath, "js")))))
	r.PathPrefix("/vendor").Handler(http.StripPrefix("/vendor", http.FileServer(http.Dir(filepath.Join(wwwPath, "vendor")))))
	r.PathPrefix("/asset").Handler(http.StripPrefix("/asset", http.FileServer(http.Dir(filepath.Join(wwwPath, "asset")))))
	r.PathPrefix("/template").Handler(http.StripPrefix("/template", http.FileServer(http.Dir(filepath.Join(wwwPath, "template")))))
	r.PathPrefix("/welcome").Handler(http.StripPrefix("/welcome", http.FileServer(http.Dir(filepath.Join(wwwPath, "welcome")))))

	r.HandleFunc("/favicon.ico",
		func(w http.ResponseWriter, r *http.Request) {
			http.ServeFile(w, r, filepath.Join(wwwPath, "asset", "favicon.ico"))
		},
	).Methods("GET")

	if *serveLogFlag { // Should be run after setupLogger() since it depends on setting up logfile
		if *logfile == "" {
			log.Println("\t-Serve Log Disable (No Log File)")
		} else {
			log.Println("\t-Serve Log Enabled")
			r.HandleFunc("/log",
				func(w http.ResponseWriter, r *http.Request) {
					http.ServeFile(w, r, *logfile)
				},
			).Methods("GET")
		}
	}

	// Specific Handlers

	r.HandleFunc("/auth", auth.AuthHandlerFunc).Methods("POST") // Authentication service
	r.HandleFunc("/multiup",                                    // Legacy support for android prototype app
		func(w http.ResponseWriter, r *http.Request) {
			legacy.MultiUploadHandler(*rterDir, uploadPath, w, r)
		},
	)

	r.HandleFunc("/", // Web client
		func(w http.ResponseWriter, r *http.Request) {
			http.ServeFile(w, r, filepath.Join(wwwPath, "index.html"))
		},
	)

	// Server final setup and adjustement

	r.NotFoundHandler = http.HandlerFunc(Debug404) // Catch all 404s

	var rootHandler http.Handler = r

	if *gzipFlag { // Wrap rootHandler with on the fly gzip compressor
		log.Print("\t-GZIP Enabled. Warning Websockets are flaky with gzip")
		rootHandler = compressor.GzipHandler(rootHandler)
	}

	if *probeLevel > 0 { // Wrap rootHandler with debugging probe
		log.Print("\t-Probe Enabled, Level ", *probeLevel)
		rootHandler = ProbeHandler(*probeLevel, rootHandler)
	}

	http.Handle("/", rootHandler)

	// Launch Server

	waits := make([]chan bool, 0) // Prevent from quitting till server routines finish

	if *httpsFlag { // HTTPS
		httpsChan := make(chan bool)
		waits = append(waits, httpsChan)

		go func() {
			log.Println(fmt.Sprintf("\t-Using HTTPS on port %v", *httpsPort))
			log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%v", *httpsPort), "cert.pem", "key.pem", nil))

			httpsChan <- true
		}()
	}

	if *httpFlag { // HTTP
		httpChan := make(chan bool)
		waits = append(waits, httpChan)

		go func() {
			log.Println(fmt.Sprintf("\t-Using HTTP on port %v", *httpPort))
			log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", *httpPort), nil))

			httpChan <- true
		}()
	}

	for _, w := range waits {
		<-w // Wait for all the ListenAndServe routines to finish
	}
}