2017-07-10 11:54:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-14 11:18:07 +00:00
|
|
|
"log"
|
2017-07-13 12:01:28 +00:00
|
|
|
"net/http"
|
2017-07-14 11:18:07 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-10 11:54:40 +00:00
|
|
|
|
2017-07-14 11:18:07 +00:00
|
|
|
"git.loafle.net/overflow/overflow_service_websocket/config"
|
2017-07-13 12:01:28 +00:00
|
|
|
"git.loafle.net/overflow/overflow_service_websocket/server"
|
2017-08-10 08:22:41 +00:00
|
|
|
"strings"
|
|
|
|
"github.com/golang/glog"
|
2017-07-10 11:54:40 +00:00
|
|
|
)
|
|
|
|
|
2017-07-28 08:48:16 +00:00
|
|
|
const (
|
|
|
|
version = "1.0.0"
|
|
|
|
website = "https://www.overflow.cloud"
|
|
|
|
banner = `
|
|
|
|
|
|
|
|
██████╗ ██╗ ██╗███████╗██████╗ ███████╗██╗ ██████╗ ██╗ ██╗
|
|
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗██╔════╝██║ ██╔═══██╗██║ ██║
|
|
|
|
██║ ██║██║ ██║█████╗ ██████╔╝█████╗ ██║ ██║ ██║██║ █╗ ██║
|
|
|
|
██║ ██║╚██╗ ██╔╝██╔══╝ ██╔══██╗██╔══╝ ██║ ██║ ██║██║███╗██║
|
|
|
|
╚██████╔╝ ╚████╔╝ ███████╗██║ ██║██║ ███████╗╚██████╔╝╚███╔███╔╝
|
|
|
|
╚═════╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚══╝╚══╝
|
|
|
|
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
func printVersion() {
|
|
|
|
log.Printf("Version: %s\n", version)
|
|
|
|
log.Printf("Url: %s\n", website)
|
|
|
|
log.Println(banner)
|
|
|
|
}
|
|
|
|
|
2017-07-10 11:54:40 +00:00
|
|
|
func main() {
|
2017-07-14 11:18:07 +00:00
|
|
|
|
|
|
|
// Initialize config
|
|
|
|
config := loadConfig()
|
|
|
|
|
2017-07-28 07:52:16 +00:00
|
|
|
addr := config.Server.Addr
|
2017-07-14 11:18:07 +00:00
|
|
|
useTLS := config.Server.Tls
|
|
|
|
sConfig := server.NewOptions(&config.Websocket)
|
|
|
|
// Print banner
|
2017-07-14 13:58:21 +00:00
|
|
|
printVersion()
|
2017-07-14 11:18:07 +00:00
|
|
|
|
|
|
|
// Config TLS
|
|
|
|
|
|
|
|
ws := server.New(sConfig)
|
2017-08-09 08:20:44 +00:00
|
|
|
|
2017-07-13 12:01:28 +00:00
|
|
|
ws.OnConnection(func(c server.Client) {
|
2017-07-14 11:18:07 +00:00
|
|
|
log.Println("Client have been connected")
|
|
|
|
c.OnDisconnect(func(c server.Client) {
|
|
|
|
log.Println("Client have been disconnected")
|
2017-07-13 12:01:28 +00:00
|
|
|
})
|
2017-07-10 11:54:40 +00:00
|
|
|
})
|
|
|
|
|
2017-08-10 08:22:41 +00:00
|
|
|
http.Handle("/rpc", allowCORS(ws.HTTPHandler()))
|
2017-07-14 11:18:07 +00:00
|
|
|
log.Printf("Address: %s, UseTLS: %t", addr, useTLS)
|
|
|
|
|
2017-07-31 08:11:49 +00:00
|
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-07-14 11:18:07 +00:00
|
|
|
}
|
2017-07-10 11:54:40 +00:00
|
|
|
|
2017-08-10 08:22:41 +00:00
|
|
|
//https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/main.go
|
|
|
|
|
|
|
|
func allowCORS(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if origin := r.Header.Get("Origin"); origin != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
|
|
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
|
|
|
|
preflightHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func preflightHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
headers := []string{"Content-Type", "Accept"}
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
|
|
|
|
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
|
|
|
|
glog.Infof("preflight request for %s", r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:18:07 +00:00
|
|
|
func loadConfig() *config.Config {
|
|
|
|
os.Chdir("./")
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
path := filepath.Join(wd, "config.json")
|
2017-07-10 11:54:40 +00:00
|
|
|
|
2017-07-14 11:18:07 +00:00
|
|
|
err := config.InitConfig(path)
|
|
|
|
if nil != err {
|
|
|
|
log.Fatalln("Config error")
|
|
|
|
}
|
|
|
|
return config.GetConfig()
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|