Exemplo n.º 1
0
func (srv *WebServer) Run(env *Environment) error {
	applog.Infof("Starting application")

	disp := newDispatcher(srv, env)
	http.Handle("/", disp)

	applog.Infof("Listening to incoming connection on %s", env.Listen)

	if err := http.ListenAndServe(env.Listen, nil); err != nil {
		applog.Criticalf(err.Error())
		return err
	}

	return nil
}
Exemplo n.º 2
0
func (disp *dispatcher) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	dispatched := false

	startTime := time.Now()

	applog.Infof("Started %s %s", req.Method, req.URL)

	localPath := path.Join(disp.env.PublicFolder, req.URL.Path)

	applog.Debugf("Checking for existance of %s", localPath)

	if info, err := os.Stat(localPath); err == nil {
		if info.IsDir() {
			indexPath := path.Join(localPath, "index.html")

			applog.Debugf("%s is a directory, checking for existance of %s", localPath, indexPath)

			if _, err := os.Stat(indexPath); err == nil {
				applog.Debugf("Serving static file %s", indexPath)
				disp.publicHandler.ServeHTTP(w, req)
				dispatched = true
			}

		} else {
			applog.Debugf("Serving static file %s", localPath)
			disp.publicHandler.ServeHTTP(w, req)
			dispatched = true
		}
	}

	if !dispatched {
		applog.Debugf("Dispatching %s", req.URL)
		disp.appHandler.ServeHTTP(w, req)
	}

	duration := time.Now().Sub(startTime)
	applog.Infof("Finished processing %s %s (%s)", req.Method, req.URL, duration.String())
}