-
Notifications
You must be signed in to change notification settings - Fork 25
/
link.go
52 lines (43 loc) · 1.46 KB
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package domain
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/kentonh/gPanel/pkg/database"
)
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)
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
domainDatabaseData.PublicPort = PublicPort
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
}