subdomains now work

This commit is contained in:
George Shaw 2018-01-23 15:50:18 -06:00
parent f802fd8eac
commit 05ef046e55
3 changed files with 46 additions and 7 deletions

View file

@ -42,7 +42,7 @@ func New(dir, name string, accPort, pubPort int) *Controller {
DocumentRoot: "account/",
Name: name,
Port: accPort,
Public: public.New("/home/"+name+"/", pubPort),
Public: public.New("/home/"+name+"/", dir, pubPort),
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
AccountLogger: accountLogger,

View file

@ -12,6 +12,7 @@ import (
type Controller struct {
Directory string
AccountDirectory string
Port int
GracefulShutdownTimeout time.Duration
Status int
@ -23,15 +24,16 @@ var controller Controller
var server http.Server
// New function returns a new PublicWeb type.
func New(dir string, port int) *Controller {
func New(dir, accountDir string, port int) *Controller {
ph, lh, err := getLogHandles(dir)
if err != nil {
log.Fatalf("Error trying to start logging instances within %v: %v", dir, err.Error())
}
controller = Controller{
Directory: dir,
Port: port,
Directory: dir,
AccountDirectory: accountDir,
Port: port,
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
PublicLogger: ph,

View file

@ -12,6 +12,7 @@ import (
"strings"
"time"
"github.com/Ennovar/gPanel/pkg/database"
"github.com/Ennovar/gPanel/pkg/routing"
)
@ -92,10 +93,46 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
path := req.URL.Path[1:]
if len(path) == 0 {
path = con.Directory + "document_root/" + "index.html"
if strings.HasPrefix(req.Host, "www") {
if len(path) == 0 {
path = con.Directory + "document_root/" + "index.html"
} else {
path = con.Directory + "document_root/" + path
}
} else {
path = con.Directory + "document_root/" + path
if strings.Count(req.Host, ".") == 2 {
subdomain := strings.SplitN(req.Host, ".", 2)[0] //Remove sub-domain
ds, err := database.Open(con.AccountDirectory + database.DB_MAIN)
if err != nil || ds == nil {
con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
var sdRoot database.StructSubdomain
err = ds.Get(database.BUCKET_SUBDOMAINS, []byte(subdomain), &sdRoot)
if err != nil {
con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
_ = ds.Close()
if len(path) == 0 {
path = con.Directory + "document_root/" + sdRoot.Root + "/index.html"
} else {
path = con.Directory + "document_root/" + sdRoot.Root + "/" + path
}
} else {
if len(path) == 0 {
path = con.Directory + "document_root/" + "index.html"
} else {
path = con.Directory + "document_root/" + path
}
}
}
contentType, err := routing.GetContentType(path)