2017-06-18 22:01:44 +00:00
|
|
|
package api
|
2017-06-26 09:07:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2017-07-06 18:55:26 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2017-06-26 18:44:37 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-31 18:13:01 +00:00
|
|
|
"github.com/spf13/viper"
|
2017-06-26 09:07:53 +00:00
|
|
|
|
2017-07-06 18:55:26 +00:00
|
|
|
"github.com/Pterodactyl/wings/config"
|
2017-06-26 09:07:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// API is a grouping struct for the api
|
|
|
|
type API struct {
|
2017-07-06 18:55:26 +00:00
|
|
|
router *gin.Engine
|
2017-06-26 09:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPI creates a new Api object
|
|
|
|
func NewAPI() API {
|
|
|
|
return API{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen starts the api http server
|
|
|
|
func (api *API) Listen() {
|
2017-08-31 18:13:01 +00:00
|
|
|
if !viper.GetBool(config.Debug) {
|
2017-07-06 18:55:26 +00:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
api.router = gin.Default()
|
|
|
|
|
2017-08-02 20:02:34 +00:00
|
|
|
api.registerRoutes()
|
2017-06-26 09:07:53 +00:00
|
|
|
|
2017-08-31 18:13:01 +00:00
|
|
|
listenString := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort))
|
2017-06-26 09:07:53 +00:00
|
|
|
|
2017-07-06 18:55:26 +00:00
|
|
|
api.router.Run(listenString)
|
|
|
|
|
2017-06-26 09:07:53 +00:00
|
|
|
log.Info("Now listening on %s", listenString)
|
|
|
|
log.Fatal(http.ListenAndServe(listenString, nil))
|
|
|
|
}
|
2017-07-06 18:55:26 +00:00
|
|
|
|
|
|
|
func getRoot(c *gin.Context) {
|
|
|
|
c.String(http.StatusOK, "hello!")
|
|
|
|
}
|