Example #1
0
File: conf.go Project: jexm/wego
func configWatcher() {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		panic("Failed start app watcher: " + err.Error())
	}

	go func() {
		for {
			select {
			case event := <-watcher.Event:
				switch filepath.Ext(event.Name) {
				case ".ini":
					if checkEventTime(event.Name) {
						continue
					}
					log.Info(event)

					if err := Cfg.Reload(); err != nil {
						log.Error("Conf Reload: ", err)
					}

					if err := i18n.ReloadLangs(); err != nil {
						log.Error("Conf Reload: ", err)
					}

					reloadConfig()
					log.Info("Config Reloaded")

				case ".json":
					if checkEventTime(event.Name) {
						continue
					}
					if event.Name == CompressConfPath {
						settingCompress()
						log.Info("Compress Reloaded")
					}
				}
			}
		}
	}()

	if err := watcher.WatchFlags("conf", fsnotify.FSN_MODIFY); err != nil {
		log.Error(err)
	}

	if err := watcher.WatchFlags("conf/global", fsnotify.FSN_MODIFY); err != nil {
		log.Error(err)
	}
}
Example #2
0
func main() {
	flag.Parse()

	log.Info("home dir is", *homeDir)

	if *isHelp {
		help()
		return
	}

	err := models.Init()
	if err != nil {
		panic(err)
	}

	err = InitI18n([]string{"en-US", "zh-CN"})
	if err != nil {
		panic(err)
	}

	t := InitTango(*isDebug)

	listen := fmt.Sprintf(":%d", *port)
	if *https {
		t.RunTLS(filepath.Join(*homeDir, "cert.pem"), filepath.Join(*homeDir, "key.pem"), listen)
	} else {
		t.Run(listen)
	}
}
Example #3
0
func Image(ctx *tango.Context) {
	token := ctx.Params().Get(":path")

	// split token and file ext
	var filePath string
	if i := strings.IndexRune(token, '.'); i == -1 {
		return
	} else {
		filePath = token[i+1:]
		token = token[:i]
	}

	// decode token to file path
	var image models.Image
	if err := image.DecodeToken(token); err != nil {
		log.Info(err)
		return
	}

	// file real path
	filePath = attachment.GenImagePath(&image) + filePath

	// if x-send on then set header and http status
	// fall back use proxy serve file
	if setting.ImageXSend {
		//ext := filepath.Ext(filePath)
		// TODO:
		//ctx.Header().ContentType(ext)
		ctx.Header().Set(setting.ImageXSendHeader, "/"+filePath)
		ctx.WriteHeader(http.StatusOK)
	} else {
		// direct serve file use go
		ctx.ServeFile(filePath)
	}
}
Example #4
0
File: main.go Project: 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)
	}
}