add all old routes and handlers

add html index page when not authenticated
fixed auth's handling of empty permissions
This commit is contained in:
Jakob Schrettenbrunner 2017-08-02 22:02:34 +02:00
parent 3a65f409d1
commit 059ea8a047
10 changed files with 298 additions and 3 deletions

View File

@ -28,7 +28,7 @@ func (api *API) Listen() {
api.router = gin.Default()
api.router.GET("/", getRoot)
api.registerRoutes()
listenString := fmt.Sprintf("%s:%d", config.Get().Web.ListenHost, config.Get().Web.ListenPort)

View File

@ -70,8 +70,7 @@ func AuthHandler(permission string) gin.HandlerFunc {
requestToken := c.Request.Header.Get(accessTokenHeader)
requestServer := c.Request.Header.Get(accessServerHeader)
var server control.Server
if requestToken == "" {
if requestToken == "" && permission != "" {
log.Debug("Token missing in request.")
c.JSON(http.StatusBadRequest, responseError{"Missing required " + accessTokenHeader + " header."})
c.Abort()

30
api/handlers.go Normal file
View File

@ -0,0 +1,30 @@
package api
import (
"net/http"
"github.com/Pterodactyl/wings/constants"
"github.com/gin-gonic/gin"
)
// handleGetIndex handles GET /
func handleGetIndex(c *gin.Context) {
auth, _ := c.Get(ContextVarAuth)
if auth := auth.(AuthorizationManager); auth.hasPermission("c:info") {
}
c.Header("Content-Type", "text/html")
c.String(http.StatusOK, constants.IndexPage)
}
// handlePutConfig handles PUT /config
func handlePutConfig(c *gin.Context) {
}
// handlePatchConfig handles PATCH /config
func handlePatchConfig(c *gin.Context) {
}

59
api/handlers_server.go Normal file
View File

@ -0,0 +1,59 @@
package api
import "github.com/gin-gonic/gin"
func handleGetServers(c *gin.Context) {
}
func handlePostServers(c *gin.Context) {
}
func handleDeleteServers(c *gin.Context) {
}
func handleGetServer(c *gin.Context) {
}
func handlePatchServer(c *gin.Context) {
}
func handlePutServer(c *gin.Context) {
}
func handlePostServerReinstall(c *gin.Context) {
}
func handlePostServerPassword(c *gin.Context) {
}
func handlePostServerRebuild(c *gin.Context) {
}
func handlePutServerPower(c *gin.Context) {
}
func handlePostServerCommand(c *gin.Context) {
}
func handleGetServerLog(c *gin.Context) {
}
func handlePostServerSuspend(c *gin.Context) {
}
func handlePostServerUnsuspend(c *gin.Context) {
}

View File

@ -0,0 +1,51 @@
package api
import "github.com/gin-gonic/gin"
func handlePostFilesFolder(c *gin.Context) {
}
func handleGetDirectory(c *gin.Context) {
}
func handlePostFileCopy(c *gin.Context) {
}
func handlePostFileMove(c *gin.Context) {
}
func handlePostFileDelete(c *gin.Context) {
}
func handlePostFileCompress(c *gin.Context) {
}
func handlePostFileDecompress(c *gin.Context) {
}
func handleGetFileStat(c *gin.Context) {
}
func handleGetFile(c *gin.Context) {
}
func handlePostFile(c *gin.Context) {
}
func handleDeleteFile(c *gin.Context) {
}
func handleGetDownloadFile(c *gin.Context) {
}

46
api/handlers_test.go Normal file
View File

@ -0,0 +1,46 @@
package api
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestHandleGetIndex(t *testing.T) {
router := gin.New()
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.GET("/", handleGetIndex)
router.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
}
func TestHandlePutConfig(t *testing.T) {
router := gin.New()
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/", strings.NewReader("{}"))
router.PUT("/", handlePutConfig)
router.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
}
func TestHandlePatchConfig(t *testing.T) {
router := gin.New()
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("PATCH", "/", strings.NewReader("{}"))
router.PATCH("/", handlePatchConfig)
router.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
}

18
api/routes.go Normal file
View File

@ -0,0 +1,18 @@
package api
import (
"github.com/gin-gonic/gin"
)
func (api *API) registerRoutes() {
api.router.GET("/", AuthHandler(""), handleGetIndex)
api.router.PUT("/config", AuthHandler("c:config"), handlePutConfig)
api.router.PATCH("/config", AuthHandler("c:config"), handlePatchConfig)
api.registerServerRoutes()
api.registerServerFileRoutes()
}
func handle(c *gin.Context) {
}

34
api/routes_server.go Normal file
View File

@ -0,0 +1,34 @@
package api
func (api *API) registerServerRoutes() {
// Big Picture Actions
api.router.GET("/servers", AuthHandler("c:list"), handleGetServers)
api.router.POST("/servers", AuthHandler("c:create"), handlePostServers)
api.router.DELETE("/servers", AuthHandler("g:server:delete"), handleDeleteServers)
// Server Actions
api.router.GET("/server", AuthHandler("s:get"), handleGetServer)
api.router.PATCH("/server", AuthHandler("s:config"), handlePatchServer)
api.router.PUT("/server", AuthHandler("s:config"), handlePutServer)
api.router.POST("/server/reinstall", AuthHandler("s:install-server"), handlePostServerReinstall)
api.router.POST("/server/password", AuthHandler(""), handlePostServerPassword)
api.router.POST("/server/rebuild", AuthHandler("g:server:rebuild"), handlePostServerRebuild)
api.router.PUT("/server/power", AuthHandler("s:power"), handlePutServerPower)
api.router.POST("/server/command", AuthHandler("s:command"), handlePostServerCommand)
api.router.GET("/server/log", AuthHandler("s:console"), handleGetServerLog)
api.router.POST("/server/suspend", AuthHandler(""), handlePostServerSuspend)
api.router.POST("/server/unsuspend", AuthHandler(""), handlePostServerUnsuspend)
}

View File

@ -0,0 +1,29 @@
package api
func (api *API) registerServerFileRoutes() {
// TODO: better and more consistent route names.
api.router.POST("/server/file/folder", AuthHandler("s:files:create"), handlePostFilesFolder)
api.router.GET("/server/directory/:directory", AuthHandler("s:files:get"), handleGetDirectory)
api.router.POST("/server/file/copy", AuthHandler("s:files:copy"), handlePostFileCopy)
api.router.POST("/server/file/{move,rename}", AuthHandler("s:files:move"), handlePostFileMove)
api.router.POST("/server/file/delete", AuthHandler("s:files:delete"), handlePostFileDelete)
api.router.POST("/server/file/compress", AuthHandler("s:files:compress"), handlePostFileCompress)
api.router.POST("/server/file/decompress", AuthHandler("s:files:decompress"), handlePostFileDecompress)
api.router.GET("/server/file/stat/:file", AuthHandler("s:files:"), handleGetFileStat)
api.router.GET("/server/file/f/:file", AuthHandler("s:files:read"), handleGetFile)
api.router.POST("/server/file/save", AuthHandler("s:files:post"), handlePostFile)
api.router.DELETE("/server/file/f/:file", AuthHandler("s:files:delete"), handleDeleteFile)
api.router.GET("/server/file/download/:token", handleGetDownloadFile)
}

29
constants/index_page.go Normal file
View File

@ -0,0 +1,29 @@
package constants
const IndexPage = `
<!doctype html>
<html>
<head>
<title>Pterodactly wings</title>
<style>
body {
background: #222;
color: #074cd5;
}
pre {
margin: auto;
}
</style
</head>
<body>
<pre>
____
__ Pterodactyl _____/___/_______ _______ ______
\_____\ \/\/ / / / __ / ___/
\___\ / / / / /_/ /___ /
\___/\___/___/___/___/___ /______/
/_______/
</pre>
</body>
</html>
`