diff --git a/api/api.go b/api/api.go index 61cdb95..9f3a0bb 100644 --- a/api/api.go +++ b/api/api.go @@ -11,34 +11,26 @@ import ( "github.com/Pterodactyl/wings/config" ) -// API is a grouping struct for the api -type API struct { +type InternalAPI struct { router *gin.Engine } -// NewAPI creates a new Api object -func NewAPI() API { - return API{} +func NewAPI() InternalAPI { + return InternalAPI{} } -// Listen starts the api http server -func (api *API) Listen() { +// Configure the API and begin listening on the configured IP and Port. +func (api *InternalAPI) Listen() { + listener := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort)) + if !viper.GetBool(config.Debug) { gin.SetMode(gin.ReleaseMode) } api.router = gin.Default() + api.RegisterRoutes() - api.registerRoutes() - - listenString := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort)) - - 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!") + api.router.Run(listener) + log.Info("Now listening on %s", listener) + log.Fatal(http.ListenAndServe(listener, nil)) } diff --git a/api/handlers.go b/api/core_routes.go similarity index 92% rename from api/handlers.go rename to api/core_routes.go index 73b4c95..dff0279 100644 --- a/api/handlers.go +++ b/api/core_routes.go @@ -11,8 +11,7 @@ import ( log "github.com/sirupsen/logrus" ) -// handleGetIndex handles GET / -func handleGetIndex(c *gin.Context) { +func GetIndex(c *gin.Context) { auth := GetContextAuthManager(c) if auth != nil && auth.HasPermission("c:info") { @@ -58,6 +57,6 @@ func handleGetIndex(c *gin.Context) { } // handlePatchConfig handles PATCH /config -func handlePatchConfig(c *gin.Context) { +func PatchConfiguration(c *gin.Context) { } diff --git a/api/file_routes.go b/api/file_routes.go new file mode 100644 index 0000000..a5943e9 --- /dev/null +++ b/api/file_routes.go @@ -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) { + +} diff --git a/api/handlers_server.go b/api/handlers_server.go deleted file mode 100644 index 5d4430e..0000000 --- a/api/handlers_server.go +++ /dev/null @@ -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) { - -} diff --git a/api/handlers_server_files.go b/api/handlers_server_files.go deleted file mode 100644 index d4c5ae3..0000000 --- a/api/handlers_server_files.go +++ /dev/null @@ -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) { - -} diff --git a/api/routes.go b/api/routes.go index a6451c4..cc2063c 100644 --- a/api/routes.go +++ b/api/routes.go @@ -1,9 +1,52 @@ package api -func (api *API) registerRoutes() { - api.router.GET("/", AuthHandler(""), handleGetIndex) - api.router.PATCH("/config", AuthHandler("c:config"), handlePatchConfig) +func (api *InternalAPI) RegisterRoutes() { + api.router.GET("/", AuthHandler(""), GetIndex) + api.router.PATCH("/config", AuthHandler("c:config"), PatchConfiguration) - api.registerServerRoutes() - api.registerServerFileRoutes() + // Register routes for v1 of the API. This API should be fully backwards compatable with + // 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) + } + } } diff --git a/api/routes_server.go b/api/routes_server.go deleted file mode 100644 index 9fa4e8c..0000000 --- a/api/routes_server.go +++ /dev/null @@ -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) -} diff --git a/api/routes_server_files.go b/api/routes_server_files.go deleted file mode 100644 index 747e05c..0000000 --- a/api/routes_server_files.go +++ /dev/null @@ -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) -} diff --git a/api/server_routes.go b/api/server_routes.go new file mode 100644 index 0000000..775b956 --- /dev/null +++ b/api/server_routes.go @@ -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) { + +}