mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Added basic webserver support.
This commit is contained in:
32
config.go
32
config.go
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -25,8 +26,17 @@ var (
|
||||
WarningSMSGlobalDefaultOrEmptyValues = "WARNING -- SMS Support disabled due to default or empty Username/Password values."
|
||||
WarningSSMSGlobalSMSContactDefaultOrEmptyValues = "WARNING -- SMS contact #%d Name/Number disabled due to default or empty values."
|
||||
WarningSSMSGlobalSMSNoContacts = "WARNING -- SMS Support disabled due to no enabled contacts."
|
||||
WarningWebserverCredentialValuesEmpty = "WARNING -- Webserver support disabled due to empty Username/Password values."
|
||||
WarningWebserverListenAddressInvalid = "WARNING -- Webserver support disabled due to invalid listen address."
|
||||
)
|
||||
|
||||
type Webserver struct {
|
||||
Enabled bool
|
||||
AdminUsername string
|
||||
AdminPassword string
|
||||
ListenAddress string
|
||||
}
|
||||
|
||||
type SMSGlobal struct {
|
||||
Enabled bool
|
||||
Username string
|
||||
@@ -42,6 +52,7 @@ type Config struct {
|
||||
Name string
|
||||
Cryptocurrencies string
|
||||
SMS SMSGlobal `json:"SMSGlobal"`
|
||||
Webserver Webserver `json:"Webserver"`
|
||||
Exchanges []Exchanges
|
||||
}
|
||||
|
||||
@@ -156,6 +167,27 @@ func CheckExchangeConfigValues() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckWebserverValues() error {
|
||||
if bot.config.Webserver.AdminUsername == "" || bot.config.Webserver.AdminPassword == "" {
|
||||
return errors.New(WarningWebserverCredentialValuesEmpty)
|
||||
}
|
||||
|
||||
if !StringContains(bot.config.Webserver.ListenAddress, ":") {
|
||||
return errors.New(WarningWebserverListenAddressInvalid)
|
||||
}
|
||||
|
||||
portStr := SplitStrings(bot.config.Webserver.ListenAddress, ":")[1]
|
||||
port, err := strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
return errors.New(WarningWebserverListenAddressInvalid)
|
||||
}
|
||||
|
||||
if port < 0 || port > 65355 {
|
||||
return errors.New(WarningWebserverListenAddressInvalid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadConfig() (Config, error) {
|
||||
file, err := ioutil.ReadFile(CONFIG_FILE)
|
||||
|
||||
|
||||
@@ -14,6 +14,12 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"Webserver": {
|
||||
"Enabled": false,
|
||||
"AdminUsername": "admin",
|
||||
"AdminPassword": "Password",
|
||||
"ListenAddress": ":9050",
|
||||
},
|
||||
"Exchanges": [
|
||||
{
|
||||
"Name": "ANX",
|
||||
|
||||
19
main.go
19
main.go
@@ -69,6 +69,25 @@ func main() {
|
||||
log.Println("SMS support disabled.")
|
||||
}
|
||||
|
||||
if bot.config.Webserver.Enabled {
|
||||
err := CheckWebserverValues()
|
||||
if err != nil {
|
||||
log.Println(err) // non fatal event
|
||||
bot.config.Webserver.Enabled = false
|
||||
} else {
|
||||
log.Println("HTTP Webserver support enabled.")
|
||||
err = StartWebserver()
|
||||
if err != nil {
|
||||
log.Println("Unable to start Webserver: ", err)
|
||||
} else {
|
||||
log.Printf("HTTP server enabled and running at http://%s:%d\n", GetWebserverHost(), GetWebserverPort())
|
||||
}
|
||||
}
|
||||
}
|
||||
if !bot.config.Webserver.Enabled {
|
||||
log.Println("HTTP Webserver support disabled.")
|
||||
}
|
||||
|
||||
AdjustGoMaxProcs()
|
||||
log.Printf("Available Exchanges: %d. Enabled Exchanges: %d.\n", len(bot.config.Exchanges), GetEnabledExchanges())
|
||||
log.Println("Bot Exchange support:")
|
||||
|
||||
4
web/footer.html
Normal file
4
web/footer.html
Normal file
@@ -0,0 +1,4 @@
|
||||
{{define "footer"}}
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
13
web/header.html
Normal file
13
web/header.html
Normal file
@@ -0,0 +1,13 @@
|
||||
{{define "header"}}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{.title}}</title>
|
||||
<!-- Latest compiled and minified CSS -->
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
|
||||
<!-- Latest compiled and minified JavaScript -->
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
{{end}}
|
||||
5
web/index.html
Normal file
5
web/index.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{template "header" .}}
|
||||
|
||||
<p>Index</p>
|
||||
|
||||
{{template "footer" .}}
|
||||
54
webserver.go
Normal file
54
webserver.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetWebserverHost() string {
|
||||
host := SplitStrings(bot.config.Webserver.ListenAddress, ":")[0]
|
||||
if host == "" {
|
||||
return "localhost"
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
func GetWebserverPort() int {
|
||||
portStr := SplitStrings(bot.config.Webserver.ListenAddress, ":")[1]
|
||||
port, _ := strconv.Atoi(portStr)
|
||||
return port
|
||||
}
|
||||
|
||||
func StartWebserver() error {
|
||||
http.HandleFunc("/", index)
|
||||
var err error
|
||||
go func() {
|
||||
err = http.ListenAndServe(bot.config.Webserver.ListenAddress, nil)
|
||||
}()
|
||||
return err
|
||||
}
|
||||
|
||||
func ServerHTTPError(w http.ResponseWriter, err error) {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
tmpl, err := template.ParseFiles("web/index.html", "web/header.html", "web/footer.html")
|
||||
if err != nil {
|
||||
ServerHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
tmplValues := map[string]interface{}{"title": "Home"}
|
||||
tmpl.Execute(w, tmplValues)
|
||||
if err != nil {
|
||||
ServerHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user