// starts http handlers for HTML content based on the given configuration file
// optional parameters:  default.contentDirectory (location of html content to be served at https://example.com/ or https://example.com/html/index.html
func StartHtmlHandler() {
	if contentDir, _ := configFile.GetString("default", "contentDirectory"); contentDir != "" {
		logger.Printf("StartHtmlHandler(): serving HTML content from [%v]", contentDir)
		http.Handle("/html/", http.StripPrefix("/html/", http.FileServer(http.Dir(contentDir))))
		http.Handle("/", http.RedirectHandler("/html/", http.StatusTemporaryRedirect))
	}
}
Example #2
0
func main() {
	port := "0.0.0.0:8080"
	room = NewRoom()
	staticDir := http.Dir("/projects/go/chat/static")
	staticServer := http.FileServer(staticDir)

	http.HandleFunc("/", Home)
	http.HandleFunc("/feed", FeedMux)
	http.HandleFunc("/login", LoginMux)
	http.Handle("/static/", http.StripPrefix("/static", staticServer))
	fmt.Printf("Serving at %s ----------------------------------------------------", port)
	http.ListenAndServe(port, nil)
}
Example #3
0
//Start the web server
func runServer(port string, s chan int) {

	http.HandleFunc("/", viewHandle)
	http.HandleFunc("/logview/", logViewHandle)

	pwd, _ := os.Getwd()
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(pwd+"/static/"))))

	err := http.ListenAndServe(":"+port, nil)
	if err != nil {
		println(err.String())
	}

	s <- 1
}
Example #4
0
// Initialize HTTP server for frontend
func ListenAndServeHttp(backend appchilada.Backend) os.Error {
	http.HandleFunc("/", indexHandler(backend))
	http.HandleFunc("/show/", showHandler(backend))

	if dir, err := os.Getwd(); err != nil {
		return err
	} else {
		dir = dir + string(os.PathSeparator) + "assets"
		log.Printf("Opening webserver in %s", dir)
		// Handle static files in /public served on /public (stripped prefix)
		http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir(dir))))
	}
	log.Printf("Opening webserver on :8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		return err
	}
	return nil
}
Example #5
0
func main() {
	runtime.GOMAXPROCS(8)
	port := "0.0.0.0:8000"
	room = NewRoom()
	rolloffs = make([]*RollOff, 0)
	staticDir := http.Dir("/projects/go/chat/static")
	staticServer := http.FileServer(staticDir)
	homeTemplate = template.Must(template.ParseFile("templates/index.html"))

	http.HandleFunc("/", LogWrap(Home, "Home"))
	http.HandleFunc("/feed", LogWrap(FeedMux, "FeedMux"))
	http.HandleFunc("/login", LogWrap(LoginMux, "LoginMux"))
	http.HandleFunc("/users", LogWrap(GetUsers, "GetUsers"))
	http.HandleFunc("/roll", LogWrap(Roll, "Roll"))
	http.HandleFunc("/roll-off", LogWrap(NewRollOff, "NewRollOff"))
	http.HandleFunc("/roll-off-entry/", LogWrap(EnterRollOff, "EnterRollOff"))
	http.Handle("/static/", http.StripPrefix("/static", staticServer))
	fmt.Printf("Serving at %s ----------------------------------------------------\n", port)
	http.ListenAndServe(port, nil)
}
Example #6
0
func main() {
	flag.Parse()

	// The counter is published as a variable directly.
	ctr := new(Counter)
	http.Handle("/counter", ctr)
	expvar.Publish("counter", ctr)

	http.Handle("/", http.HandlerFunc(Logger))
	http.Handle("/go/", http.StripPrefix("/go/", http.FileServer(http.Dir(*webroot))))
	http.Handle("/flags", http.HandlerFunc(FlagServer))
	http.Handle("/args", http.HandlerFunc(ArgServer))
	http.Handle("/go/hello", http.HandlerFunc(HelloServer))
	http.Handle("/chan", ChanCreate())
	http.Handle("/date", http.HandlerFunc(DateServer))
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		log.Panicln("ListenAndServe:", err)
	}
}
Example #7
0
func StartServer(listen string, world *World) {

	http.HandleFunc("/state", func(w http.ResponseWriter, req *http.Request) {
		player, err := world.Player(req.URL.Query()["player"][0])
		if err != nil {
			panic(err)
		}
		io.WriteString(w, player.State())
	})

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		index, _ := ioutil.ReadFile("public/index.html")
		io.WriteString(w, string(index))
	})

	http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./public"))))

	err := http.ListenAndServe(listen, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err.String())
	}
}
Example #8
0
// Serve files from 'root', stripping 'prefix' from the URL being requested
func FileServer(root, prefix string) Component {
	handler := http.StripPrefix(prefix, http.FileServer(http.Dir(root)))
	return NewHandlerComponent(handler)
}
Example #9
0
func main() {
	// Debug URLs to reload or restart the system
	http.Handle("/debug/gc", http.HandlerFunc(GCServer))
	http.Handle("/debug/stats", http.HandlerFunc(StatsServer))
	http.Handle("/debug/exit", http.HandlerFunc(ExitServer))

	// Go 'http' package
	http.Handle("/go/hello", http.HandlerFunc(HelloServer))
	http.Handle("/go/example/", http.StripPrefix("/go", http.FileServer(http.Dir("../http-data"))))
	http.Handle("/go/ipsum.txt", http.StripPrefix("/go", http.FileServer(http.Dir("../http-data"))))

	// Webpipes with Erlang chains
	http.Handle("/webpipe/erlang/hello", webpipes.Chain(
		webpipes.TextStringSource(helloworld),
		webpipes.OutputPipe,
	))
	http.Handle("/webpipe/erlang/example/", webpipes.Chain(
		webpipes.FileServer("../http-data", "/webpipe/erlang"),
		webpipes.OutputPipe,
	))
	http.Handle("/webpipe/erlang/ipsum.txt", webpipes.Chain(
		webpipes.FileServer("../http-data", "/webpipe/erlang"),
		webpipes.OutputPipe,
	))

	// Webpipes with Proc chains
	http.Handle("/webpipe/proc/hello", webpipes.NetworkHandler(
		webpipes.TextStringSource(helloworld),
		webpipes.OutputPipe,
	))
	http.Handle("/webpipe/proc/example/", webpipes.NetworkHandler(
		webpipes.FileServer("../http-data", "/webpipe/proc"),
		webpipes.OutputPipe,
	))
	http.Handle("/webpipe/proc/ipsum.txt", webpipes.NetworkHandler(
		webpipes.FileServer("../http-data", "/webpipe/proc"),
		webpipes.OutputPipe,
	))

	// CGI Examples
	pwd, pwderr := os.Getwd()
	if pwderr != nil {
		log.Fatalf("Cannot find pwd: %s", pwderr)
	}

	cgipath := path.Clean(path.Join(pwd, "../http-data/cgi-bin"))

	cgiscripts := []string{"echo_post.py", "hello.py", "printenv.py", "test.sh"}
	for _, script := range cgiscripts {
		http.Handle(path.Join("/cgi-bin", script), webpipes.Chain(
			webpipes.CGIServer(path.Join(cgipath, script), "/cgi-bin/"),
			webpipes.OutputPipe,
		))
	}

	http.Handle("/wiki/", webpipes.Chain(
		webpipes.CGIServer("/tmp/gorows-sputnik/sputnik.cgi", "/wiki/"),
		webpipes.OutputPipe,
	))

	http.Handle("/zip/", webpipes.Chain(
		webpipes.FileServer("../http-data", "/zip/"),
		webpipes.CompressionPipe,
		webpipes.OutputPipe,
	))

	//	var second int64 = 1e9
	server := &http.Server{
		Addr:    ":12345",
		Handler: http.DefaultServeMux,
		//		ReadTimeout: 5 * second,
		//		WriteTimeout: 5 * second,
	}

	log.Printf("Starting test server on %s", server.Addr)
	log.Printf("Running on %d processes\n", runtime.GOMAXPROCS(0))
	err := server.ListenAndServe()
	if err != nil {
		log.Fatalf("Error: %s", err.String())
	}
}
Example #10
0
func main() {
	flag.Parse()
	readTemplates()

	if *root == "" {
		var err os.Error
		*root, err = os.Getwd()
		if err != nil {
			log.Fatalf("Failed to getwd: %v", err)
		}
	}

	fixupGitwebFiles()

	latestGits := filepath.Join(*root, "latestgits")
	os.Mkdir(latestGits, 0700)
	if *gerritHost != "" {
		go rsyncFromGerrit(latestGits)
	}

	mux := http.DefaultServeMux
	mux.Handle("/favicon.ico", http.FileServer(http.Dir(filepath.Join(*root, "static"))))
	mux.Handle("/robots.txt", http.FileServer(http.Dir(filepath.Join(*root, "static"))))
	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(*root, "static")))))
	mux.Handle("/talks/", http.StripPrefix("/talks/", http.FileServer(http.Dir(filepath.Join(*root, "talks")))))

	gerritUrl, _ := url.Parse("http://gerrit-proxy:8000/")
	var gerritHandler http.Handler = http.NewSingleHostReverseProxy(gerritUrl)
	if *httpsAddr != "" {
		proxyHandler := gerritHandler
		gerritHandler = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
			if req.TLS != nil {
				proxyHandler.ServeHTTP(rw, req)
				return
			}
			http.Redirect(rw, req, "https://camlistore.org"+req.URL.RawPath, http.StatusFound)
		})
	}
	mux.Handle("/r/", gerritHandler)

	testCgi := &cgi.Handler{Path: filepath.Join(*root, "test.cgi"),
		Root: "/test.cgi",
	}
	mux.Handle("/test.cgi", testCgi)
	mux.Handle("/test.cgi/foo", testCgi)

	mux.Handle("/code", http.RedirectHandler("/code/", http.StatusFound))
	if *gitwebScript != "" {
		env := os.Environ()
		env = append(env, "GITWEB_CONFIG="+filepath.Join(*root, "gitweb-camli.conf"))
		env = append(env, "CAMWEB_ROOT="+filepath.Join(*root))
		env = append(env, "CAMWEB_GITDIR="+latestGits)
		mux.Handle("/code/", &fixUpGitwebUrls{&gitwebHandler{
			Cgi: &cgi.Handler{
				Path: *gitwebScript,
				Root: "/code/",
				Env:  env,
			},
			Static: http.StripPrefix("/code/", http.FileServer(http.Dir(*gitwebFiles))),
		}})
	}
	mux.HandleFunc("/", mainHandler)

	var handler http.Handler = &noWwwHandler{Handler: mux}
	if *logDir != "" || *logStdout {
		handler = NewLoggingHandler(handler, *logDir, *logStdout)
	}

	errch := make(chan os.Error)

	httpServer := &http.Server{
		Addr:         *httpAddr,
		Handler:      handler,
		ReadTimeout:  connTimeoutNanos,
		WriteTimeout: connTimeoutNanos,
	}
	go func() {
		errch <- httpServer.ListenAndServe()
	}()

	if *httpsAddr != "" {
		log.Printf("Starting TLS server on %s", *httpsAddr)
		httpsServer := new(http.Server)
		*httpsServer = *httpServer
		httpsServer.Addr = *httpsAddr
		go func() {
			errch <- httpsServer.ListenAndServeTLS(*tlsCertFile, *tlsKeyFile)
		}()
	}

	log.Fatalf("Serve error: %v", <-errch)
}