示例#1
0
文件: models.go 项目: harryyeh/switch
func init() {
	var err error
	x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
		setting.Cfg.Section("database").Key("USER").String(),
		setting.Cfg.Section("database").Key("PASSWD").String(),
		setting.Cfg.Section("database").Key("HOST").String(),
		setting.Cfg.Section("database").Key("NAME").String()))
	if err != nil {
		log.Fatal(4, "Fail to init new engine: %v", err)
	}

	x.SetLogger(nil)

	if err = x.Sync(new(Package), new(Revision), new(Downloader),
		new(Block), new(BlockRule)); err != nil {
		log.Fatal(4, "Fail to sync database: %v", err)
	}

	statistic()
	c := cron.New()
	c.AddFunc("@every 5m", statistic)
	c.AddFunc("@every 1h", cleanExpireRevesions)
	c.Start()

	go cleanExpireRevesions()
	if setting.ProdMode {
		go uploadArchives()
		ticker := time.NewTicker(time.Hour)
		go func() {
			for _ = range ticker.C {
				uploadArchives()
			}
		}()
	}
}
示例#2
0
func init() {
	log.NewLogger(0, "console", `{"level": 0}`)

	sources := []interface{}{"conf/app.ini"}
	if com.IsFile("custom/app.ini") {
		sources = append(sources, "custom/app.ini")
	}

	var err error
	Cfg, err = macaron.SetConfig(sources[0], sources[1:]...)
	if err != nil {
		log.Fatal(4, "Fail to set configuration: %v", err)
	}

	AppName = Cfg.Section("").Key("APP_NAME").String()

	if Cfg.Section("").Key("RUN_MODE").MustString("dev") == "prod" {
		ProdMode = true
		macaron.Env = macaron.PROD
		macaron.ColorLog = false
	}

	HttpPort = Cfg.Section("server").Key("HTTP_PORT").MustInt(8084)
	ArchivePath = Cfg.Section("server").Key("ARCHIVE_PATH").MustString("data/archives")
	os.MkdirAll(ArchivePath, os.ModePerm)

	MaxUploadSize = Cfg.Section("server").Key("MAX_UPLOAD_SIZE").MustInt64(5)

	GithubCredentials = "client_id=" + Cfg.Section("github").Key("CLIENT_ID").String() +
		"&client_secret=" + Cfg.Section("github").Key("CLIENT_SECRET").String()

	conf.UP_HOST = Cfg.Section("qiniu").Key("UP_HOST").MustString(conf.UP_HOST)
	BucketName = Cfg.Section("qiniu").Key("BUCKET_NAME").String()
	BucketUrl = Cfg.Section("qiniu").Key("BUCKET_URL").String()
	conf.ACCESS_KEY = Cfg.Section("qiniu").Key("ACCESS_KEY").String()
	conf.SECRET_KEY = Cfg.Section("qiniu").Key("SECRET_KEY").String()

	AccessToken = Cfg.Section("admin").Key("ACCESS_TOKEN").String()
}
示例#3
0
文件: switch.go 项目: harryyeh/switch
func main() {
	log.Info("%s %s", setting.AppName, APP_VER)
	log.Info("Run Mode: %s", strings.Title(macaron.Env))

	m := macaron.New()
	m.Use(macaron.Logger())
	m.Use(macaron.Recovery())
	m.Use(macaron.Static("public", macaron.StaticOptions{
		SkipLogging: true,
	}))
	m.Use(pongo2.Pongoers(pongo2.Options{
		Directory:  "templates/web",
		IndentJSON: macaron.Env != macaron.PROD,
	}, "templates/admin"))
	m.Use(i18n.I18n())
	m.Use(session.Sessioner())
	m.Use(middleware.Contexter())

	// Routes.
	m.Get("/", routers.Home)
	m.Route("/download", "GET,POST", routers.Download)
	m.Get("/favicon.ico", func(ctx *middleware.Context) {
		ctx.Redirect("/img/favicon.png")
	})
	// m.Get("/search", routers.Search)
	// m.Get("/about", routers.About)

	// Package.
	m.Get("/*", routers.Package)
	m.Get("/badge/*", routers.Badge)

	// Admin.
	m.Post("/admin/auth", admin.AuthPost)
	m.Group("/admin", func() {
		m.Get("", admin.Dashboard)

		m.Group("/packages", func() {
			m.Get("", admin.Revisions)
			m.Get("/larges", admin.LargeRevisions)
		})

		m.Group("/blocks", func() {
			m.Get("", admin.Blocks)
			m.Combo("/new").Get(admin.BlockPackage).Post(admin.BlockPackagePost)
			m.Get("/:id:int/delete", admin.UnblockPackage)

			m.Group("/rules", func() {
				m.Get("", admin.BlockRules)
				m.Combo("/new").Get(admin.NewBlockRule).Post(admin.NewBlockRulePost)
				m.Get("/:id:int/run", admin.RunRule)
				m.Get("/:id:int/delete", admin.DeleteBlockRule)
			})
		})
	}, admin.Auth)

	// API.
	m.Group("/api", func() {
		m.Group("/v1", func() {
			m.Group("", func() {
				m.Get("/download", v1.Download)
				m.Get("/revision", v1.GetRevision)
			}, v1.PackageFilter())
		})
	})

	// Robots.txt
	m.Get("/robots.txt", func() string {
		return `User-agent: *
Disallow: /api/
Disallow: /download`
	})

	m.NotFound(routers.NotFound)

	listenAddr := fmt.Sprintf("0.0.0.0:%d", setting.HttpPort)
	log.Info("Listen: http://%s", listenAddr)
	if err := http.ListenAndServe(listenAddr, m); err != nil {
		log.Fatal(4, "Fail to start server: %v", err)
	}
}