refactored web component
This commit is contained in:
parent
73ea58378a
commit
3015b873cb
|
@ -45,7 +45,7 @@ func HandleRequest(line string, conn *connection.RedisConnection) {
|
||||||
hostname = query_name[:len(query_name)-len(DdnsDomain)]
|
hostname = query_name[:len(query_name)-len(DdnsDomain)]
|
||||||
}
|
}
|
||||||
|
|
||||||
if hostname == "" || ! conn.HostExist(hostname) {
|
if hostname == "" || !conn.HostExist(hostname) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
ddns.go
4
ddns.go
|
@ -15,11 +15,15 @@ func HandleErr(err error) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DdnsDomain string
|
DdnsDomain string
|
||||||
|
DdnsWebListenSocket string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&DdnsDomain, "domain", "",
|
flag.StringVar(&DdnsDomain, "domain", "",
|
||||||
"The subdomain which should be handled by DDNS")
|
"The subdomain which should be handled by DDNS")
|
||||||
|
|
||||||
|
flag.StringVar(&DdnsWebListenSocket, "listen", ":8080",
|
||||||
|
"Which socket should the web service use to bind itself")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateCommandArgs() {
|
func ValidateCommandArgs() {
|
||||||
|
|
47
web.go
47
web.go
|
@ -10,16 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunWebService(conn *connection.RedisConnection) {
|
func RunWebService(conn *connection.RedisConnection) {
|
||||||
|
|
||||||
// Add index template from bindata
|
|
||||||
index_content, err := Asset("templates/index.html")
|
|
||||||
HandleErr(err)
|
|
||||||
|
|
||||||
html, err := template.New("index.html").Parse(string(index_content))
|
|
||||||
HandleErr(err)
|
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.HTMLTemplates = html
|
r.HTMLTemplates = BuildTemplate()
|
||||||
|
|
||||||
r.GET("/", func(g *gin.Context) {
|
r.GET("/", func(g *gin.Context) {
|
||||||
g.HTML(200, "index.html", gin.H{"domain": DdnsDomain})
|
g.HTML(200, "index.html", gin.H{"domain": DdnsDomain})
|
||||||
|
@ -37,7 +29,9 @@ func RunWebService(conn *connection.RedisConnection) {
|
||||||
hostname := c.Params.ByName("hostname")
|
hostname := c.Params.ByName("hostname")
|
||||||
|
|
||||||
if conn.HostExist(hostname) {
|
if conn.HostExist(hostname) {
|
||||||
c.String(403, "This hostname has already been registered.")
|
c.JSON(403, gin.H{
|
||||||
|
"error": "This hostname has already been registered.",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,31 +52,39 @@ func RunWebService(conn *connection.RedisConnection) {
|
||||||
token := c.Params.ByName("token")
|
token := c.Params.ByName("token")
|
||||||
|
|
||||||
if !conn.HostExist(hostname) {
|
if !conn.HostExist(hostname) {
|
||||||
c.String(404,
|
c.JSON(404, gin.H{
|
||||||
"This hostname has not been registered or is expired.")
|
"error": "This hostname has not been registered or is expired.",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
host := conn.GetHost(hostname)
|
host := conn.GetHost(hostname)
|
||||||
|
|
||||||
if host.Token != token {
|
if host.Token != token {
|
||||||
c.String(403,
|
c.JSON(403, gin.H{
|
||||||
"You have supplied the wrong token to manipulate this host")
|
"error": "You have supplied the wrong token to manipulate this host",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := GetRemoteAddr(c.Req)
|
ip, err := GetRemoteAddr(c.Req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(500, "Your sender IP address is not in the right format")
|
c.JSON(400, gin.H{
|
||||||
|
"error": "Your sender IP address is not in the right format",
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
host.Ip = ip
|
host.Ip = ip
|
||||||
conn.SaveHost(host)
|
conn.SaveHost(host)
|
||||||
|
|
||||||
c.String(200, fmt.Sprintf("Your current IP is %s", ip))
|
c.JSON(200, gin.H{
|
||||||
|
"current_ip": ip,
|
||||||
|
"status": "Successfuly updated",
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run(":8080")
|
r.Run(DdnsWebListenSocket)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the Remote Address of the client. At First we try to get the
|
// Get the Remote Address of the client. At First we try to get the
|
||||||
|
@ -98,3 +100,14 @@ func GetRemoteAddr(req *http.Request) (string, error) {
|
||||||
return ip, err
|
return ip, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get index template from bindata
|
||||||
|
func BuildTemplate() *template.Template {
|
||||||
|
index_content, err := Asset("templates/index.html")
|
||||||
|
HandleErr(err)
|
||||||
|
|
||||||
|
html, err := template.New("index.html").Parse(string(index_content))
|
||||||
|
HandleErr(err)
|
||||||
|
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue