gPanel/pkg/api/api_handler.go

48 lines
1.7 KiB
Go

// Package api handles all API calls
package api
import (
"net/http"
"strings"
"github.com/Ennovar/gPanel/pkg/api/logs"
"github.com/Ennovar/gPanel/pkg/api/server"
"github.com/Ennovar/gPanel/pkg/api/user"
"github.com/Ennovar/gPanel/pkg/public"
)
// HandleAPI function takes a path and determines if it is an API call, if it is it will
// call the specified API. It returns two booleans, the first being if it is an API call and
// the second is the response of the API call's function.
//
// The path of an API is formatted as such: document_roots/webhost/api/some/thing. The path is split
// by the sequence of characters "api", returning the first half of the URL which is discarded, and the
// second half which will look like /some/thing. The second half is processed to see whether or not it is
// a valid API, and then handled accordingly from there.
func HandleAPI(res http.ResponseWriter, req *http.Request, path string, publicServer *public.Controller) (bool, bool) {
splitUrl := strings.SplitN(path, "api", 2)
suspectApi := strings.ToLower(splitUrl[len(splitUrl)-1])
switch suspectApi {
case "/user/auth":
return true, user.Auth(res, req)
case "/user/register":
return true, user.Register(res, req)
case "/user/logout":
return true, user.Logout(res, req)
case "/server/status":
return true, server.Status(res, req, publicServer)
case "/server/start":
return true, server.Start(res, req, publicServer)
case "/server/shutdown":
return true, server.Shutdown(res, req, publicServer)
case "/server/restart":
return true, server.Restart(res, req, publicServer)
case "/server/maintenance":
return true, server.Maintenance(res, req, publicServer)
case "/logs/read":
return true, logs.Read(res, req)
default:
return false, false
}
}