forked from kevacoin-project/keva_ipfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
137 lines (115 loc) · 3.45 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"crypto/tls"
"encoding/json"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/checksum0/go-electrum/electrum"
"github.com/gin-gonic/gin"
)
var cfg ServerConfig
func loadConfig(cfg *ServerConfig) {
configFileName := "config.json"
configFileName, _ = filepath.Abs(configFileName)
log.Println("Loading config: ", configFileName)
configFile, err := os.Open(configFileName)
if err != nil {
log.Fatalln("File error: ", err.Error())
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&cfg); err != nil {
log.Fatalln("Config error: ", err.Error())
}
log.Println("- config Payment address: ", cfg.Payment_address)
if len(cfg.Payment_address) == 0 {
log.Fatalln("Config variable Payment_address required.")
}
log.Println("- config Min_payment: ", cfg.Min_payment)
if cfg.Min_payment < 0 {
log.Fatalln("Invalid config variable Min_payment value.")
}
log.Println("- config Electrum_port: ", cfg.Electrum_port)
if len(cfg.Electrum_host) == 0 {
log.Fatalln("Config variable Electrum_host required.")
}
log.Println("- config Electrum_host: ", cfg.Electrum_host)
if cfg.Electrum_port == 0 || cfg.Electrum_port < 0 {
log.Fatalln("Config variable Electrum_port required.")
}
log.Println("- config Tls_enabled: ", cfg.Tls_enabled)
if cfg.Tls_enabled == true {
if len(cfg.Tls_cert) == 0 || len(cfg.Tls_key) == 0 {
log.Fatalln("Config variables Tls_cert and Tls_key required.")
}
} else {
log.Println("- config *Warning*: TLS/SSL not enabled. Set tls_enabled to true to enable TLS/SSL.")
}
}
func setupServer() *gin.Engine {
var err error = nil
electrumServer := electrum.NewServer()
portStr := strconv.Itoa(cfg.Electrum_port)
if cfg.Tls_enabled {
conf := &tls.Config{}
if err = electrumServer.ConnectSSL(cfg.Electrum_host + ":" + portStr, conf); err != nil {
log.Fatalln("Electrum connection error: ", err.Error())
}
} else {
if err = electrumServer.ConnectTCP(cfg.Electrum_host + ":" + portStr); err != nil {
log.Fatalln("Electrum connection error: ", err.Error())
}
}
// Timed "server.ping" call to prevent disconnection.
go func() {
for {
if err = electrumServer.Ping(); err != nil {
// Log error, don't treat as fatal
log.Println("Electrum keep alive error: ", err.Error())
}
time.Sleep(60 * time.Second)
}
}()
router := gin.New()
v1 := router.Group("/v1")
{
// Get payment info
v1.GET("/payment_info", func(c *gin.Context) {
getPaymentInfo(c, cfg.Payment_address, cfg.Min_payment)
})
// Upload media
v1.POST("/media", func(c *gin.Context) {
uploadMedia(c)
})
// Add to IPFS
v1.POST("/pin", func(c *gin.Context) {
publishMediaIPFS(electrumServer, c, cfg.Payment_address, cfg.Min_payment)
})
}
return router
}
func main() {
var router *gin.Engine
// Setting log file
file, err := os.OpenFile("debug.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalln("Error creating log file: ", err.Error())
}
defer file.Close()
log.SetOutput(file)
// Setting gin to release mode; comment to use environment variable or systems default
gin.SetMode(gin.ReleaseMode)
loadConfig(&cfg)
router = setupServer()
// The port used by the server is the eletrumx port plus 10.
portStr := strconv.Itoa(cfg.Electrum_port + 10)
if cfg.Tls_enabled == true {
router.RunTLS(":" + portStr, cfg.Tls_cert, cfg.Tls_key)
} else {
router.Run(":" + portStr)
}
log.Println("Listening on port: " + portStr)
}