예제 #1
0
파일: main.go 프로젝트: mehulsbhatt/gohttp
func main() {
	kingpin.Parse()
	initRouters()

	http.Handle("/", m)

	addr := ":" + strconv.Itoa(gcfg.port)
	p := strconv.Itoa(gcfg.port)
	mesg := "; please visit http://127.0.0.1:" + p
	if gcfg.private {
		addr = "localhost" + addr
		log.Printf("listens on 127.0.0.1@" + p + mesg)
	} else {
		log.Printf("listens on 0.0.0.0@" + p + mesg)
	}

	if gcfg.ftp {
		//log.Println("Enable FTP")
		auths := strings.SplitN(gcfg.ftpAuth, ":", 2)
		if len(auths) != 2 {
			log.Fatal("ftp auth format error")
		}
		auth := FTPAuth{auths[0], auths[1]}
		ftpserv := goftp.NewServer(&goftp.ServerOpts{
			Port:    gcfg.ftpPort,
			Factory: posixfs.NewPosixFSFactory(gcfg.root),
			Auth:    &auth,
		})
		go ftpserv.ListenAndServe()
	}

	var err error
	if gcfg.key != "" && gcfg.cert != "" {
		err = http.ListenAndServeTLS(addr, gcfg.cert, gcfg.key, nil)
	} else {
		err = http.ListenAndServe(addr, nil)
	}
	log.Fatal(err)
}
예제 #2
0
파일: main.go 프로젝트: goftp/ftpd
func main() {
	flag.StringVar(&cfgPath, "config", "config.ini",
		"config file path, default is config.ini and custom.ini")
	flag.Parse()

	if len(cfgPath) <= 0 {
		cfgPath = "config.ini"
		customPath = "custom.ini"
	} else {
		f, _ := filepath.Abs(cfgPath)
		customPath = filepath.Join(filepath.Dir(f), "custom.ini")
	}

	var err error
	cfg, err = goconfig.LoadConfigFile(cfgPath, customPath)
	if err != nil {
		fmt.Println(err)
		return
	}

	log.Info("Loaded config files:", cfgPath, customPath)

	port, _ := cfg.Int("server", "port")
	db, err := leveldb.OpenFile("./authperm.db", nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	var auth = &ldbauth.LDBAuth{db}
	var perm server.Perm
	if cfg.MustValue("perm", "type") == "leveldb" {
		perm = ldbperm.NewLDBPerm(db, "root", "root", os.ModePerm)
	} else {
		perm = server.NewSimplePerm("root", "root")
	}

	typ, _ := cfg.GetValue("driver", "type")
	var factory server.DriverFactory
	if typ == "file" {
		rootPath, _ := cfg.GetValue("file", "rootpath")
		_, err = os.Lstat(rootPath)
		if os.IsNotExist(err) {
			os.MkdirAll(rootPath, os.ModePerm)
		} else if err != nil {
			fmt.Println(err)
			return
		}
		factory = &filedriver.FileDriverFactory{
			rootPath,
			perm,
		}
	} else if typ == "qiniu" {
		accessKey, _ := cfg.GetValue("qiniu", "accessKey")
		secretKey, _ := cfg.GetValue("qiniu", "secretKey")
		bucket, _ := cfg.GetValue("qiniu", "bucket")
		factory = qiniudriver.NewQiniuDriverFactory(accessKey,
			secretKey, bucket)
	} else {
		log.Fatal("no driver type input")
	}

	// start web manage UI
	useweb, _ := cfg.Bool("web", "enable")
	if useweb {
		web.DB = auth
		web.Perm = perm
		web.Factory = factory
		weblisten, _ := cfg.GetValue("web", "listen")
		admin, _ := cfg.GetValue("admin", "user")
		pass, _ := cfg.GetValue("admin", "pass")
		tls, _ := cfg.Bool("web", "tls")
		certFile, _ := cfg.GetValue("web", "certFile")
		keyFile, _ := cfg.GetValue("web", "keyFile")

		go web.Web(weblisten, "static", "templates", admin, pass, tls, certFile, keyFile)
	}

	ftpName, _ := cfg.GetValue("server", "name")
	opt := &server.ServerOpts{
		Name:    ftpName,
		Factory: factory,
		Port:    port,
		Auth:    auth,
	}

	// start ftp server
	ftpServer := server.NewServer(opt)
	log.Info("FTP Server", version)
	err = ftpServer.ListenAndServe()
	if err != nil {
		log.Fatal("Error starting server:", err)
	}
}