basic api server initialization
This commit is contained in:
parent
e356ee5fa8
commit
9c364970f0
21
api/api.go
21
api/api.go
|
@ -2,16 +2,17 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/schrej/wings/config"
|
"github.com/Pterodactyl/wings/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// API is a grouping struct for the api
|
// API is a grouping struct for the api
|
||||||
type API struct {
|
type API struct {
|
||||||
|
router *gin.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAPI creates a new Api object
|
// NewAPI creates a new Api object
|
||||||
|
@ -21,12 +22,22 @@ func NewAPI() API {
|
||||||
|
|
||||||
// Listen starts the api http server
|
// Listen starts the api http server
|
||||||
func (api *API) Listen() {
|
func (api *API) Listen() {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
if !config.Get().Debug {
|
||||||
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
|
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)
|
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.Info("Now listening on %s", listenString)
|
||||||
log.Fatal(http.ListenAndServe(listenString, nil))
|
log.Fatal(http.ListenAndServe(listenString, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRoot(c *gin.Context) {
|
||||||
|
c.String(http.StatusOK, "hello!")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user