working on router

This commit is contained in:
George Shaw 2017-12-11 12:01:57 -06:00
parent 3d7fd0d893
commit 3db26321ef
7 changed files with 21 additions and 16 deletions

View file

@ -22,7 +22,7 @@ function ListDomains(bundle_name) {
if(xhr.status == 200) {
jsonResponse = JSON.parse(xhr.response)
jQuery.each(jsonResponse, function(k, v) {
list.append('<div class="row mt-2"><div class="col-6 d-flex align-items-center"><p class="mb-0">'+v+'</p></div><div class="col-6 d-flex justify-content-end"><div class="btn-group" role="group"><button class="btn btn-outline-danger _js_delete-registered-domain" data="'+v+'">Delete</button></div></div></div>');
list.append('<div class="row mt-2"><div class="col-6 d-flex align-items-center"><p class="mb-0">'+k+'</p></div><div class="col-6 d-flex justify-content-end"><div class="btn-group" role="group"><button class="btn btn-outline-danger _js_delete-registered-domain" data="'+k+'">Delete</button></div></div></div>');
});
}
else if(xhr.status == 204) {

View file

@ -308,7 +308,7 @@
</div>
</div>
<div class="row mt-5">
<div class="row mt-5 pb-3">
<div class="col-12">
<div class="card">
<div class="card-body">

View file

@ -8,7 +8,7 @@ import (
"encoding/json"
)
func Link(res http.ResponseWriter, req *http.Request, logger *log.Logger) bool {
func Link(res http.ResponseWriter, req *http.Request, logger *log.Logger, PublicPort int) 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)
@ -37,6 +37,7 @@ func Link(res http.ResponseWriter, req *http.Request, logger *log.Logger) bool {
var domainDatabaseData database.Struct_Domain
domainDatabaseData.BundleName = linkDomainReqData.Bundle
domainDatabaseData.PublicPort = PublicPort
err = ds.Put(database.BUCKET_DOMAINS, []byte(linkDomainReqData.Domain), domainDatabaseData)
if err != nil {

View file

@ -7,10 +7,11 @@ import (
type Struct_Domain struct {
BundleName string `json:"name"`
PublicPort int `json:"port"`
}
func (ds *Datastore) ListDomains(bundle string) (map[string]string, error) {
filtered := make(map[string]string)
func (ds *Datastore) ListDomains(bundle string) (map[string]Struct_Domain, error) {
filtered := make(map[string]Struct_Domain)
var holder Struct_Domain
ds.handle.View(func(tx *bolt.Tx) error {
@ -21,7 +22,7 @@ func (ds *Datastore) ListDomains(bundle string) (map[string]string, error) {
json.Unmarshal(v, &holder)
if bundle == "*" || holder.BundleName == bundle {
filtered[holder.BundleName] = string(k)
filtered[string(k)] = holder
}
}

View file

@ -62,7 +62,7 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b
case "/domain/list":
return true, domain.List(res, req, con.APILogger)
case "/domain/link":
return true, domain.Link(res, req, con.APILogger)
return true, domain.Link(res, req, con.APILogger, con.Public.Port)
case "/domain/unlink":
return true, domain.Unlink(res, req, con.APILogger)
default:

View file

@ -5,6 +5,7 @@ import (
"strconv"
"time"
"github.com/Ennovar/gPanel/pkg/database"
"net/url"
)
type Router struct {
@ -33,7 +34,9 @@ func (r *Router) Start() {
go server.ListenAndServe()
}
func (r *Router) Route(domain string, res http.ResponseWriter) {
func (r *Router) ServeHTTP(res http.ResponseWriter, req *http.Request) {
domain := req.Host
ds, err := database.Open("server/" + database.DB_DOMAINS)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
@ -48,12 +51,12 @@ func (r *Router) Route(domain string, res http.ResponseWriter) {
return
}
cURL, err := url.Parse("localhost:"+strconv.Itoa(client.PublicPort)+"/"+req.URL.Path[1:])
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
res.Write([]byte(domain + " is linked to bundle " + client.BundleName))
}
func (r *Router) ServeHTTP(res http.ResponseWriter, req *http.Request) {
d := req.Host
r.Route(d, res)
res.Write([]byte(cURL.String()))
}

View file

@ -409,7 +409,7 @@
</div>
</div>
<div class="row mt-5">
<div class="row mt-5 pb-3">
<div class="col-12">
<div class="card">
<div class="card-body">