Clean up route handling

This commit is contained in:
Dane Everitt 2017-09-30 18:35:44 -05:00
parent 256c566dfe
commit 1bfc016e1b
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
9 changed files with 163 additions and 173 deletions

View File

@ -11,34 +11,26 @@ import (
"github.com/Pterodactyl/wings/config" "github.com/Pterodactyl/wings/config"
) )
// API is a grouping struct for the api type InternalAPI struct {
type API struct {
router *gin.Engine router *gin.Engine
} }
// NewAPI creates a new Api object func NewAPI() InternalAPI {
func NewAPI() API { return InternalAPI{}
return API{}
} }
// Listen starts the api http server // Configure the API and begin listening on the configured IP and Port.
func (api *API) Listen() { func (api *InternalAPI) Listen() {
listener := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort))
if !viper.GetBool(config.Debug) { if !viper.GetBool(config.Debug) {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
api.router = gin.Default() api.router = gin.Default()
api.RegisterRoutes()
api.registerRoutes() api.router.Run(listener)
log.Info("Now listening on %s", listener)
listenString := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort)) log.Fatal(http.ListenAndServe(listener, nil))
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!")
} }

View File

@ -11,8 +11,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// handleGetIndex handles GET / func GetIndex(c *gin.Context) {
func handleGetIndex(c *gin.Context) {
auth := GetContextAuthManager(c) auth := GetContextAuthManager(c)
if auth != nil && auth.HasPermission("c:info") { if auth != nil && auth.HasPermission("c:info") {
@ -58,6 +57,6 @@ func handleGetIndex(c *gin.Context) {
} }
// handlePatchConfig handles PATCH /config // handlePatchConfig handles PATCH /config
func handlePatchConfig(c *gin.Context) { func PatchConfiguration(c *gin.Context) {
} }

47
api/file_routes.go Normal file
View File

@ -0,0 +1,47 @@
package api
import "github.com/gin-gonic/gin"
func StoreDirectory(c *gin.Context) {
}
func ListDirectory(c *gin.Context) {
}
func CopyFile(c *gin.Context) {
}
func MoveFile(c *gin.Context) {
}
func CompressFile(c *gin.Context) {
}
func DecompressFile(c *gin.Context) {
}
func StatFile(c *gin.Context) {
}
func ReadFileContents(c *gin.Context) {
}
func WriteFileContents(c *gin.Context) {
}
func DeleteFile(c *gin.Context) {
}
func DownloadFile(c *gin.Context) {
}

View File

@ -1,55 +0,0 @@
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 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

@ -1,51 +0,0 @@
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) {
}

View File

@ -1,9 +1,52 @@
package api package api
func (api *API) registerRoutes() { func (api *InternalAPI) RegisterRoutes() {
api.router.GET("/", AuthHandler(""), handleGetIndex) api.router.GET("/", AuthHandler(""), GetIndex)
api.router.PATCH("/config", AuthHandler("c:config"), handlePatchConfig) api.router.PATCH("/config", AuthHandler("c:config"), PatchConfiguration)
api.registerServerRoutes() // Register routes for v1 of the API. This API should be fully backwards compatable with
api.registerServerFileRoutes() // the existing Nodejs Daemon API.
v1 := api.router.Group("/v1")
{
v1BaseRoutes := v1.Group("/server")
{
v1BaseRoutes.GET("/", AuthHandler("c:list"), ListServers)
v1BaseRoutes.POST("/", AuthHandler("c:create"), StoreServer)
}
v1ServerRoutes := v1.Group("/server/:server")
{
v1ServerRoutes.GET("/", AuthHandler("s:get"), ViewServer)
v1ServerRoutes.GET("/log", AuthHandler("s:console"), GetLogForServer)
v1ServerRoutes.POST("/reinstall", AuthHandler("s:install-server"), ReinstallServer)
v1ServerRoutes.POST("/rebuild", AuthHandler("g:server:rebuild"), RebuildServer)
v1ServerRoutes.POST("/password", AuthHandler(""), SetServerPassword)
v1ServerRoutes.POST("/power", AuthHandler("s:power"), PowerServer)
v1ServerRoutes.POST("/command", AuthHandler("s:command"), SendCommandToServer)
v1ServerRoutes.POST("/suspend", AuthHandler(""), SuspendServer)
v1ServerRoutes.POST("/unsuspend", AuthHandler(""), UnsuspendServer)
v1ServerRoutes.PATCH("/", AuthHandler("s:config"), UpdateServer)
v1ServerRoutes.DELETE("/", AuthHandler("g:server:delete"), DeleteServer)
}
v1ServerFileRoutes := v1.Group("/server/:server/files")
{
v1ServerFileRoutes.GET("/file/:file", AuthHandler("s:files:read"), ReadFileContents)
v1ServerFileRoutes.GET("/stat/:file", AuthHandler("s:files:get"), StatFile)
v1ServerFileRoutes.GET("/dir/:directory", AuthHandler("s:files:get"), ListDirectory)
v1ServerFileRoutes.GET("/download/:token", DownloadFile)
v1ServerFileRoutes.POST("/dir/:directory", AuthHandler("s:files:create"), StoreDirectory)
v1ServerFileRoutes.POST("/file/:file", AuthHandler("s:files:post"), WriteFileContents)
v1ServerFileRoutes.POST("/copy/:file", AuthHandler("s:files:copy"), CopyFile)
v1ServerFileRoutes.POST("/move/:file", AuthHandler("s:files:move"), MoveFile)
v1ServerFileRoutes.POST("/rename/:file", AuthHandler("s:files:move"), MoveFile)
v1ServerFileRoutes.POST("/compress/:file", AuthHandler("s:files:compress"), CompressFile)
v1ServerFileRoutes.POST("/decompress/:file", AuthHandler("s:files:decompress"), DecompressFile)
v1ServerFileRoutes.DELETE("/file/:file", AuthHandler("s:files:delete"), DeleteFile)
}
}
} }

View File

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

View File

@ -1,20 +0,0 @@
package api
func (api *API) registerServerFileRoutes() {
api.router.GET("/servers/:server/files/file/:file", AuthHandler("s:files:read"), handleGetFile)
api.router.GET("/servers/:server/files/stat/:file", AuthHandler("s:files:"), handleGetFileStat)
api.router.GET("/servers/:server/files/dir/:directory", AuthHandler("s:files:get"), handleGetDirectory)
api.router.POST("/servers/:server/files/dir/:directory", AuthHandler("s:files:create"), handlePostFilesFolder)
api.router.POST("/servers/:server/files/file/:file", AuthHandler("s:files:post"), handlePostFile)
api.router.POST("/servers/:server/files/copy/:file", AuthHandler("s:files:copy"), handlePostFileCopy)
api.router.POST("/servers/:server/files/move/:file", AuthHandler("s:files:move"), handlePostFileMove)
api.router.POST("/servers/:server/files/rename/:file", AuthHandler("s:files:move"), handlePostFileMove)
api.router.POST("/servers/:server/files/compress/:file", AuthHandler("s:files:compress"), handlePostFileCompress)
api.router.POST("/servers/:server/files/decompress/:file", AuthHandler("s:files:decompress"), handlePostFileDecompress)
api.router.DELETE("/servers/:server/files/file/:file", AuthHandler("s:files:delete"), handleDeleteFile)
api.router.GET("/servers/:server/files/download/:token", handleGetDownloadFile)
}

55
api/server_routes.go Normal file
View File

@ -0,0 +1,55 @@
package api
import "github.com/gin-gonic/gin"
func ListServers(c *gin.Context) {
}
func StoreServer(c *gin.Context) {
}
func DeleteServer(c *gin.Context) {
}
func ViewServer(c *gin.Context) {
}
func UpdateServer(c *gin.Context) {
}
func ReinstallServer(c *gin.Context) {
}
func SetServerPassword(c *gin.Context) {
}
func RebuildServer(c *gin.Context) {
}
func PowerServer(c *gin.Context) {
}
func SendCommandToServer(c *gin.Context) {
}
func GetLogForServer(c *gin.Context) {
}
func SuspendServer(c *gin.Context) {
}
func UnsuspendServer(c *gin.Context) {
}