wings/api/api.go

49 lines
1.0 KiB
Go
Raw Normal View History

package api
import (
"fmt"
"net/http"
2017-07-06 18:55:26 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/pterodactyl/wings/config"
)
2017-09-30 23:35:44 +00:00
type InternalAPI struct {
2017-07-06 18:55:26 +00:00
router *gin.Engine
}
2017-09-30 23:35:44 +00:00
func NewAPI() InternalAPI {
return InternalAPI{}
}
2017-09-30 23:35:44 +00:00
// Configure the API and begin listening on the configured IP and Port.
func (api *InternalAPI) Listen() {
if !viper.GetBool(config.Debug) {
2017-07-06 18:55:26 +00:00
gin.SetMode(gin.ReleaseMode)
}
api.router = gin.Default()
2017-10-01 00:25:45 +00:00
api.router.RedirectTrailingSlash = false
2017-07-06 18:55:26 +00:00
api.router.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
})
api.router.OPTIONS("/", func(c *gin.Context) {
c.Header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "X-Access-Token")
})
2017-10-09 19:01:24 +00:00
api.RegisterRoutes()
listenString := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort))
2017-07-06 18:55:26 +00:00
api.router.Run(listenString)
log.Info("Now listening on %s", listenString)
log.Fatal(http.ListenAndServe(listenString, nil))
}