示例#1
0
//Setup initializes routers, login and services.
func Setup() {

	if configuration.Debug {
		gin.SetMode(gin.DebugMode)
	} else {
		gin.SetMode(gin.ReleaseMode)
	}

	r := gin.New()

	r.Use(cors.Middleware(cors.Config{
		Origins:         "*",
		Methods:         "GET, PUT, POST, DELETE",
		RequestHeaders:  "Origin, Authorization, Content-Type",
		ExposedHeaders:  "",
		MaxAge:          50 * time.Second,
		Credentials:     true,
		ValidateHeaders: false,
	}))

	r.SetHTMLTemplate(pages.Template)
	r.GET("/", pages.Homepage)

	serviceGroup := r.Group("/api/", AuthRequired())
	{
		serviceGroup.GET("/services/:identifier/update", services.Update)
		serviceGroup.GET("/services/:identifier/reschedule", services.Reschedule)
		serviceGroup.GET("/services/:identifier", services.Show)
		serviceGroup.GET("/services", services.List)
	}

	systemAPI := r.Group("/api/system/", AuthRequired())
	{
		systemAPI.GET("/start", system.Start)
		systemAPI.GET("/stop", system.Stop)
		systemAPI.GET("/reload", system.Reload)
	}

	log.Notice("Starting webserver..")
	log.Info("API listening on http://" + configuration.C.API.Address)

	r.Run(configuration.C.API.Address)
}
示例#2
0
文件: main.go 项目: ovh/tat
			case "error":
				log.SetLevel(log.ErrorLevel)
			}
		}

		// Add a ginrus middleware, which:
		//   - Logs all requests, like a combined access and error log.
		//   - Logs to stdout.
		//   - RFC3339 with UTC time format.
		router.Use(ginrus(log.StandardLogger(), time.RFC3339, true))

		router.Use(cors.Middleware(cors.Config{
			Origins:         "*",
			Methods:         "GET, PUT, POST, DELETE",
			RequestHeaders:  "Origin, Authorization, Content-Type, Accept, Tat_Password, Tat_Username",
			ExposedHeaders:  "Tat_Password, Tat_Username",
			MaxAge:          50 * time.Second,
			Credentials:     true,
			ValidateHeaders: false,
		}))

		if err := store.NewStore(); err != nil {
			log.Fatalf("Error trying to reach mongoDB. Please check your Tat Configuration and access to your MongoDB. Err: %s", err.Error())
		}
		topic.InitDB()
		group.InitDB()
		message.InitDB()

		routerRoot := router.Group("")

		initRoutesGroups(routerRoot, CheckPassword())