Example #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	role := config.mustGetString("postgres", "role")
	database := config.mustGetString("postgres", "database")
	ip := config.mustGetString("postgres", "ip")
	password := config.mustGetString("postgres", "password")
	services.InitDBConnection(role, password, database, ip)

	api_key := config.mustGetString("parsemap", "api_key")
	r := gin.New()
	r.Use(gzip.Gzip(gzip.DefaultCompression))
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(gin.ErrorLogger())

	v2 := r.Group("/v2")
	services.GetHandlersV2(v2, api_key)

	var wg sync.WaitGroup
	wg.Add(2)
	go startServer(r, &wg)
	go startTLSServer(r, &wg)
	wg.Wait()
}
Example #2
0
// Router returns prebuilt with routes http.Handler
func Router(c *ctx.AptlyContext) http.Handler {
	context = c

	router := gin.Default()
	router.Use(gin.ErrorLogger())

	if context.Flags().Lookup("no-lock").Value.Get().(bool) {
		// We use a goroutine to count the number of
		// concurrent requests. When no more requests are
		// running, we close the database to free the lock.
		requests := make(chan int)
		acks := make(chan error)

		go acquireDatabase(requests, acks)
		go cacheFlusher(requests, acks)

		router.Use(func(c *gin.Context) {
			requests <- ACQUIREDB
			err := <-acks
			if err != nil {
				c.Fail(500, err)
				return
			}
			defer func() {
				requests <- RELEASEDB
				err = <-acks
				if err != nil {
					c.Fail(500, err)
					return
				}
			}()
			c.Next()
		})

	} else {
		go cacheFlusher(nil, nil)
	}

	root := router.Group("/api")

	{
		root.GET("/version", apiVersion)
	}

	{
		root.GET("/repos", apiReposList)
		root.POST("/repos", apiReposCreate)
		root.GET("/repos/:name", apiReposShow)
		root.PUT("/repos/:name", apiReposEdit)
		root.DELETE("/repos/:name", apiReposDrop)

		root.GET("/repos/:name/packages", apiReposPackagesShow)
		root.POST("/repos/:name/packages", apiReposPackagesAdd)
		root.DELETE("/repos/:name/packages", apiReposPackagesDelete)

		root.POST("/repos/:name/file/:dir/:file", apiReposPackageFromFile)
		root.POST("/repos/:name/file/:dir", apiReposPackageFromDir)

		root.POST("/repos/:name/snapshots", apiSnapshotsCreateFromRepository)
	}

	{
		root.POST("/mirrors/:name/snapshots", apiSnapshotsCreateFromMirror)
	}

	{
		root.GET("/files", apiFilesListDirs)
		root.POST("/files/:dir", apiFilesUpload)
		root.GET("/files/:dir", apiFilesListFiles)
		root.DELETE("/files/:dir", apiFilesDeleteDir)
		root.DELETE("/files/:dir/:name", apiFilesDeleteFile)
	}

	{
		root.GET("/publish", apiPublishList)
		root.POST("/publish", apiPublishRepoOrSnapshot)
		root.POST("/publish/:prefix", apiPublishRepoOrSnapshot)
		root.PUT("/publish/:prefix/:distribution", apiPublishUpdateSwitch)
		root.DELETE("/publish/:prefix/:distribution", apiPublishDrop)
	}

	{
		root.GET("/snapshots", apiSnapshotsList)
		root.POST("/snapshots", apiSnapshotsCreate)
		root.PUT("/snapshots/:name", apiSnapshotsUpdate)
		root.GET("/snapshots/:name", apiSnapshotsShow)
		root.GET("/snapshots/:name/packages", apiSnapshotsSearchPackages)
		root.DELETE("/snapshots/:name", apiSnapshotsDrop)
		root.GET("/snapshots/:name/diff/:withSnapshot", apiSnapshotsDiff)
	}

	{
		root.GET("/packages/:key", apiPackagesShow)
	}

	{
		root.GET("/graph.:ext", apiGraph)
	}

	return router
}
Example #3
0
// Router returns prebuilt with routes http.Handler
func Router(c *ctx.AptlyContext) http.Handler {
	context = c

	go cacheFlusher()

	router := gin.Default()
	router.Use(gin.ErrorLogger())

	root := router.Group("/api")

	{
		root.GET("/version", apiVersion)
	}

	{
		root.GET("/repos", apiReposList)
		root.POST("/repos", apiReposCreate)
		root.GET("/repos/:name", apiReposShow)
		root.PUT("/repos/:name", apiReposEdit)
		root.DELETE("/repos/:name", apiReposDrop)

		root.GET("/repos/:name/packages", apiReposPackagesShow)
		root.POST("/repos/:name/packages", apiReposPackagesAdd)
		root.DELETE("/repos/:name/packages", apiReposPackagesDelete)

		root.POST("/repos/:name/file/:dir/:file", apiReposPackageFromFile)
		root.POST("/repos/:name/file/:dir", apiReposPackageFromDir)

		root.POST("/repos/:name/snapshots", apiSnapshotsCreateFromRepository)
	}

	{
		root.POST("/mirrors/:name/snapshots", apiSnapshotsCreateFromMirror)
	}

	{
		root.GET("/files", apiFilesListDirs)
		root.POST("/files/:dir", apiFilesUpload)
		root.GET("/files/:dir", apiFilesListFiles)
		root.DELETE("/files/:dir", apiFilesDeleteDir)
		root.DELETE("/files/:dir/:name", apiFilesDeleteFile)
	}

	{
		root.GET("/publish", apiPublishList)
		root.POST("/publish", apiPublishRepoOrSnapshot)
		root.POST("/publish/:prefix", apiPublishRepoOrSnapshot)
		root.PUT("/publish/:prefix/:distribution", apiPublishUpdateSwitch)
		root.DELETE("/publish/:prefix/:distribution", apiPublishDrop)
	}

	{
		root.GET("/snapshots", apiSnapshotsList)
		root.POST("/snapshots", apiSnapshotsCreate)
		root.PUT("/snapshots/:name", apiSnapshotsUpdate)
		root.GET("/snapshots/:name", apiSnapshotsShow)
		root.GET("/snapshots/:name/packages", apiSnapshotsSearchPackages)
		root.DELETE("/snapshots/:name", apiSnapshotsDrop)
		root.GET("/snapshots/:name/diff/:withSnapshot", apiSnapshotsDiff)
	}

	{
		root.GET("/packages/:key", apiPackagesShow)
	}

	{
		root.GET("/graph.:ext", apiGraph)
	}

	return router
}
Example #4
0
func ErrorLoggerMiddleware() Middleware {
	return Middleware{Func: gin.ErrorLogger()}
}
Example #5
0
func main() {
	gin.SetMode(config.HTTP.Mode)
	router := gin.New()

	// middlewares
	authRequired := middlewares.AuthRequired(store.GetAuthToken, config.AuthToken.Lifetime)
	catpchaValidationRequired := middlewares.CaptchaValidationRequired(geetest.Validate)

	// globally use middlewares
	router.Use(
		middlewares.RecoveryWithWriter(os.Stderr),
		middlewares.Logger(geo),
		middlewares.CORS(),
		gin.ErrorLogger(),
	)

	// version 1 api endpoints
	v1Endpoints := router.Group("/v1")

	// user endpoints
	v1UserEndpoints := v1Endpoints.Group("/users")
	v1UserEndpoints.GET("", authRequired, v1.UserInfo(store.GetUserByID))
	v1UserEndpoints.POST("", v1.Signup(validateAddressFunc(config.Coin.Type), store.CreateUser, store.GetUserByID))
	v1UserEndpoints.PUT("/:id/status", v1.VerifyEmail(store.GetSessionByToken, store.GetUserByID, store.UpdateUserStatus))
	v1UserEndpoints.GET("/referees", authRequired, v1.RefereeList(store.GetReferees, store.GetNumberOfReferees))

	// auth token endpoints
	v1AuthTokenEndpoints := v1Endpoints.Group("/auth_tokens")
	v1AuthTokenEndpoints.POST("", v1.Login(store.GetUserByEmail, store.CreateAuthToken))
	v1AuthTokenEndpoints.DELETE("", authRequired, v1.Logout(store.DeleteAuthToken))

	// session endpoints
	v1SessionEndpoints := v1Endpoints.Group("/sessions")
	emailVerificationTemplate := template.Must(template.ParseFiles(config.Template.EmailVerificationTemplate))
	v1SessionEndpoints.POST("", authRequired,
		v1.RequestVerifyEmail(store.GetUserByID, store.UpsertSession, mailer.SendEmail, emailVerificationTemplate, config.App.Name, config.App.URL),
	)

	// income endpoints
	v1IncomeEndpoints := v1Endpoints.Group("/incomes", authRequired)
	v1IncomeEndpoints.POST("/rewards", catpchaValidationRequired,
		v1.GetReward(store.GetUserByID,
			memoryCache.GetLatestTotalReward,
			memoryCache.GetLatestConfig,
			memoryCache.GetRewardRatesByType,
			createRewardIncome,
			memoryCache.InsertIncome,
			connsHub.Broadcast),
	)
	v1IncomeEndpoints.GET("/rewards", v1.RewardList(store.GetRewardIncomes, store.GetNumberOfRewardIncomes))
	v1IncomeEndpoints.GET("/offerwalls", v1.OfferwallList(store.GetOfferwallIncomes, store.GetNumberOfOfferwallIncomes))

	// withdrawal endpoint
	v1Endpoints.GET("/withdrawals", authRequired, v1.WithdrawalList(store.GetWithdrawals, store.GetNumberOfWithdrawals, constructTxURL))

	// captcha endpoint
	v1Endpoints.GET("/captchas", v1.RegisterCaptcha(geetest.Register, geetest.CaptchaID))

	// offerwall endpoint
	v1OfferwallEndpoints := v1Endpoints.Group("/offerwalls")

	superrewardsAuthRequired := middlewares.SuperrewardsAuthRequired(config.Offerwall.Superrewards.WhitelistIps)
	v1OfferwallEndpoints.GET("/superrewards", superrewardsAuthRequired,
		v1.SuperrewardsCallback(
			config.Offerwall.Superrewards.SecretKey,
			store.GetUserByID,
			store.GetNumberOfSuperrewardsOffers,
			memoryCache.GetLatestConfig,
			store.CreateSuperrewardsIncome,
			connsHub.Broadcast,
		),
	)

	ptcwallAuthRequired := middlewares.PtcwallAuthRequired(config.Offerwall.Ptcwall.PostbackPassword, config.Offerwall.Ptcwall.WhitelistIps)
	v1OfferwallEndpoints.GET("/ptcwall", ptcwallAuthRequired,
		v1.PtcwallCallback(
			store.GetUserByID,
			memoryCache.GetLatestConfig,
			store.CreatePtcwallIncome,
			connsHub.Broadcast,
		),
	)

	v1OfferwallEndpoints.POST("/clixwall",
		v1.ClixwallCallback(
			config.Offerwall.Clixwall.SecretPassword,
			store.GetUserByID,
			store.GetNumberOfClixwallOffers,
			memoryCache.GetLatestConfig,
			store.CreateClixwallIncome,
			connsHub.Broadcast,
		),
	)

	personaly := config.Offerwall.Personaly
	personalyAuthRequired := middlewares.PersonalyAuthRequired(personaly.WhitelistIps, personaly.AppHash, personaly.SecretKey)
	v1OfferwallEndpoints.GET("/personaly", personalyAuthRequired, v1.PersonalyCallback(
		store.GetUserByID,
		store.GetNumberOfPersonalyOffers,
		memoryCache.GetLatestConfig,
		store.CreatePersonalyIncome,
		connsHub.Broadcast,
	))

	kiwiwallAuthRequired := middlewares.KiwiwallAuthRequired(config.Offerwall.Kiwiwall.WhitelistIps, config.Offerwall.Kiwiwall.SecretKey)
	v1OfferwallEndpoints.GET("/kiwiwall", kiwiwallAuthRequired, v1.KiwiwallCallback(
		store.GetUserByID,
		store.GetNumberOfKiwiwallOffers,
		memoryCache.GetLatestConfig,
		store.CreateKiwiwallIncome,
		connsHub.Broadcast,
	))

	adscendMediaAuthRequired := middlewares.AdscendMediaAuthRequired(config.Offerwall.AdscendMedia.WhitelistIps)
	v1OfferwallEndpoints.GET("/adscend_media", adscendMediaAuthRequired, v1.AdscendMediaCallback(
		store.GetUserByID,
		store.GetAdscendMediaOffer,
		store.ChargebackIncome,
		memoryCache.GetLatestConfig,
		store.CreateAdscendMediaIncome,
		connsHub.Broadcast,
	))

	adgateMediaAuthRequired := middlewares.AdgateMediaAuthRequired(config.Offerwall.AdgateMedia.WhitelistIps)
	v1OfferwallEndpoints.GET("/adgate_media", adgateMediaAuthRequired, v1.AdgateMediaCallback(
		store.GetUserByID,
		store.GetNumberOfAdgateMediaOffers,
		memoryCache.GetLatestConfig,
		store.CreateAdgateMediaIncome,
		connsHub.Broadcast,
	))

	offertoroAuthRequired := middlewares.OffertoroAuthRequired(config.Offerwall.Offertoro.SecretKey)
	v1OfferwallEndpoints.GET("/offertoro", offertoroAuthRequired, v1.OffertoroCallback(
		store.GetUserByID,
		store.GetNumberOfOffertoroOffers,
		memoryCache.GetLatestConfig,
		store.CreateOffertoroIncome,
		connsHub.Broadcast,
	))

	// websocket endpoint
	v1Endpoints.GET("/websocket",
		v1.Websocket(
			connsHub.Len,
			memoryCache.GetLatestIncomes,
			connsHub.Broadcast,
			hub.WrapPutWebsocketConn(connsHub.PutConn)),
	)

	logrus.WithFields(logrus.Fields{
		"http_address": config.HTTP.Address,
	}).Info("service up")
	if err := router.Run(config.HTTP.Address); err != nil {
		logrus.WithError(err).Fatal("failed to start service")
	}
}