started implimenting domain manager

This commit is contained in:
George Shaw 2017-12-08 15:11:18 -06:00
parent 1ba13fd5d9
commit aeff7c00c8
13 changed files with 405 additions and 1 deletions

50
pkg/api/domain/link.go Normal file
View file

@ -0,0 +1,50 @@
package domain
import (
"net/http"
"log"
"strconv"
"github.com/Ennovar/gPanel/pkg/database"
"encoding/json"
)
func Link(res http.ResponseWriter, req *http.Request, logger *log.Logger) bool {
if req.Method != "POST" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var linkDomainReqData struct {
Domain string `json:"domain"`
Bundle string `json:"name"`
}
err := json.NewDecoder(req.Body).Decode(&linkDomainReqData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
ds, err := database.Open("server/"+database.DB_DOMAINS)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
var domainDatabaseData database.Struct_Domain
domainDatabaseData.BundleName = linkDomainReqData.Bundle
err = ds.Put(database.BUCKET_DOMAINS, []byte(linkDomainReqData.Domain), domainDatabaseData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusNoContent)
return true
}

59
pkg/api/domain/list.go Normal file
View file

@ -0,0 +1,59 @@
package domain
import (
"net/http"
"log"
"strconv"
"encoding/json"
"github.com/Ennovar/gPanel/pkg/database"
)
func List(res http.ResponseWriter, req *http.Request, logger *log.Logger) bool {
if req.Method != "POST" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var listDomainsReqData struct {
Bundle string `json:"name"`
}
err := json.NewDecoder(req.Body).Decode(&listDomainsReqData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
ds, err := database.Open("server/" + database.DB_DOMAINS)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
domains, err := ds.ListDomains(listDomainsReqData.Bundle)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
if len(domains) > 0 {
b, err := json.Marshal(domains)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusOK)
res.Write(b)
return true
}
res.WriteHeader(http.StatusNoContent)
return true
}

46
pkg/api/domain/unlink.go Normal file
View file

@ -0,0 +1,46 @@
package domain
import (
"net/http"
"log"
"strconv"
"encoding/json"
"github.com/Ennovar/gPanel/pkg/database"
)
func Unlink(res http.ResponseWriter, req *http.Request, logger *log.Logger) bool {
if req.Method != "DELETE" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var unlinkDomainRequestData struct {
Domain string `json:"domain"`
}
err := json.NewDecoder(req.Body).Decode(&unlinkDomainRequestData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
ds, err := database.Open("server/"+database.DB_DOMAINS)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
err = ds.Delete(database.BUCKET_DOMAINS, []byte(unlinkDomainRequestData.Domain))
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusNoContent)
return true
}

View file

@ -0,0 +1,23 @@
package settings
import (
"net/http"
"strconv"
"strings"
"log"
)
func BundleName(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool {
if req.Method != "GET" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusNotFound) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
dir = strings.Replace(dir, "bundles/bundle_", "", 1)
dir = strings.Replace(dir, "/", "", 1)
res.WriteHeader(http.StatusOK)
res.Write([]byte(dir))
return true
}