示例#1
0
文件: server.go 项目: jiakuan/livedev
func NewServer(context build.Context, s ServerConf) (*Server, error) {
	var (
		srv    = new(Server)
		ignore *regexp.Regexp
		paths  []string
	)

	if len(s.Resources.Paths) > 0 {
		for _, s := range s.Resources.Paths {
			if p := strings.TrimSpace(s); len(p) > 0 {
				paths = append(paths, p)
			}
		}

		if len(paths) > 0 {
			if s := strings.TrimSpace(s.Resources.Ignore); len(s) > 0 {
				if pattern, err := regexp.Compile(s); err == nil {
					ignore = pattern
				} else {
					return nil, fmt.Errorf(`Fatal error: Invalid pattern "%s" : %v`, s, err)
				}
			}

			srv.resources = &Resource{ignore, paths}
		}
	}

	srv.target = strings.TrimSpace(s.Target)
	srv.targetDir = filepath.Dir(s.Target)
	srv.bin = strings.TrimSpace(s.Bin)
	srv.builder = s.Builder

	if !hasPrefix(srv.target, filepath.SplitList(context.GOPATH)) {
		//Target is not in the $GOPATH
		//Try to guess the import root(workspace) from the path
		roots := ImportRoots(srv.target)

		if len(roots) > 0 {
			context.GOPATH = strings.Join(append(roots, context.GOPATH), string(filepath.ListSeparator))
		}
	}

	srv.context = context

	if len(srv.builder) == 0 {
		cmd := filepath.Join(context.GOROOT, "bin", "go")
		srv.builder = append(srv.builder, cmd, "build", "-o", srv.bin)
	}

	srv.port = s.Port
	srv.startup = s.Startup
	srv.host = s.Host
	return srv, nil
}
示例#2
0
文件: server.go 项目: qrtz/livedev
func newServer(context build.Context, conf serverConfig, proxyPort int, w *watcher.Watcher) (*Server, error) {
	var err error
	srv := new(Server)
	srv.conf = conf
	srv.proxyPort = proxyPort

	srv.resources, err = newResource(conf.Resources.Paths, conf.Resources.Ignore)

	if err != nil {
		return nil, err
	}

	srv.assets, err = newResource(conf.Assets.Paths, conf.Assets.Ignore)

	if err != nil {
		return nil, err
	}

	srv.startupTimeout = conf.StartupTimeout

	srv.target = strings.TrimSpace(conf.Target)
	srv.targetDir = filepath.Dir(conf.Target)
	srv.bin = strings.TrimSpace(conf.Bin)
	srv.builder = conf.Builder

	if !hasPrefix(srv.target, filepath.SplitList(context.GOPATH)) {
		// Target is not in the $GOPATH
		// Try to guess the import root(workspace) from the path
		roots := importRoots(srv.target)

		if len(roots) > 0 {
			context.GOPATH = strings.Join(append(roots, context.GOPATH), string(filepath.ListSeparator))
		}
	}

	srv.context = context

	if len(srv.builder) == 0 {
		gobin := "go"

		if len(context.GOROOT) > 0 && fileExists(context.GOROOT) {
			gobin = filepath.Join(context.GOROOT, "bin", gobin)
		}

		srv.builder = append(srv.builder, gobin, "build", "-o", srv.bin)
	}

	srv.watcher = w
	srv.watcherEvents = make(chan watcher.Event, 1)
	srv.ready = make(chan error, 1)
	srv.busy = make(chan bool, 1)
	srv.stopped = make(chan bool, 1)
	srv.started = make(chan bool, 1)
	srv.done = make(chan error, 1)
	srv.exit = make(chan bool, 1)
	srv.updateListeners = newUpdateListeners()
	srv.port = conf.Port
	srv.startup = conf.Startup
	srv.host = conf.Host
	srv.stdout = logger.NewLogWriter(os.Stdout, srv.host+"> ", log.LstdFlags)
	srv.stderr = new(logger.BufferedLogWriter)
	return srv, nil
}