basic api server initialization

This commit is contained in:
Jakob Schrettenbrunner 2017-07-06 20:55:26 +02:00
parent e356ee5fa8
commit 9c364970f0

View File

@ -2,16 +2,17 @@ package api
import (
"fmt"
"html"
"net/http"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/schrej/wings/config"
"github.com/Pterodactyl/wings/config"
)
// API is a grouping struct for the api
type API struct {
router *gin.Engine
}
// NewAPI creates a new Api object
@ -21,12 +22,22 @@ func NewAPI() API {
// Listen starts the api http server
func (api *API) Listen() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
if !config.Get().Debug {
gin.SetMode(gin.ReleaseMode)
}
api.router = gin.Default()
api.router.GET("/", getRoot)
listenString := fmt.Sprintf("%s:%d", config.Get().Web.ListenHost, config.Get().Web.ListenPort)
api.router.Run(listenString)
log.Info("Now listening on %s", listenString)
log.Fatal(http.ListenAndServe(listenString, nil))
}
func getRoot(c *gin.Context) {
c.String(http.StatusOK, "hello!")
}