-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (113 loc) · 3.21 KB
/
main.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
package main
import (
"bufio"
"context"
"crypto/rand"
"crypto/tls"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"time"
swapcrypto "github.com/alxarno/swap/crypto"
logger "github.com/alxarno/swap/logger"
db "github.com/alxarno/swap/db2"
"github.com/alxarno/swap/settings"
engine "github.com/alxarno/swap/src/messages"
)
func removeContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
func main() {
_, err := settings.GetSettings()
if err != nil {
logger.Logger.Println("Settings loading fatal -> ", err.Error())
bufio.NewReader(os.Stdin).ReadBytes('\n')
return
}
logger.Init(settings.ServiceSettings.Backend.FileLogs)
swapcrypto.InitCert()
err = db.BeginDB(nil)
if err != nil {
logger.Logger.Println("DB setup fatal-> ", err.Error())
bufio.NewReader(os.Stdin).ReadBytes('\n')
return
}
engine.StartCoreMessenger()
engine.ConnectActionsToDB()
router := newRouter()
myAddres := ""
addrs, err := net.InterfaceAddrs()
if err != nil {
logger.Logger.Print("Some network error -> ", err.Error())
os.Stderr.WriteString("Oops: " + err.Error() + "\n")
os.Exit(1)
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
myAddres += ipnet.IP.String()
myAddres += ":" + settings.ServiceSettings.Backend.Host + "/" + settings.ServiceSettings.Backend.SSLHost + "\t"
}
}
}
tlsconfig := tls.Config{Certificates: []tls.Certificate{*swapcrypto.Cert}}
tlsconfig.Rand = rand.Reader
tlsserver := http.Server{
TLSConfig: &tlsconfig,
Handler: router,
Addr: ":" + settings.ServiceSettings.Backend.SSLHost,
}
r := http.NewServeMux()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+strings.Split(r.Host, ":")[0]+":"+settings.ServiceSettings.Backend.SSLHost+r.URL.String(), http.StatusPermanentRedirect)
})
httpserver := http.Server{
Addr: ":" + settings.ServiceSettings.Backend.Host,
Handler: r,
}
go func() {
if err := httpserver.ListenAndServe(); err != http.ErrServerClosed {
logger.Logger.Printf("Listen Error HTTP: %v\n", err)
}
}()
go func() {
if err := tlsserver.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
logger.Logger.Printf("Listen Error TLS: %v\n", err)
}
}()
printLogo()
os.Stderr.WriteString("Swap started on \t" + myAddres + "\n")
logger.Logger.Println("Swap started")
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
logger.Logger.Println("Swap shutdowned")
httpctx, httpcancel := context.WithTimeout(context.Background(), 5*time.Second)
httpsctx, httpscancel := context.WithTimeout(context.Background(), 10*time.Second)
defer httpcancel()
defer httpscancel()
if err = httpserver.Shutdown(httpctx); err != nil {
logger.Logger.Printf("Could not gracefully shutdown the http server %v\n", err)
}
if err = tlsserver.Shutdown(httpsctx); err != nil {
logger.Logger.Printf("Could not gracefully shutdown the https server %v\n", err)
}
}