Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attach mode, info #29

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GO_BIN := ${GOPATH}/bin

GOBUILD := go build -v

VERSION := v1.2.0
VERSION := local
COMMIT := $(shell git rev-parse HEAD)
ifeq ($(OS),Windows_NT)
TIMESTAMP := $(shell powershell.exe scripts\get_timestamp.ps1)
Expand Down
182 changes: 101 additions & 81 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,72 @@ package main

import (
"fmt"
"github.com/ExchangeUnion/xud-docker-api/launcher"
"github.com/ExchangeUnion/xud-docker-api/logging"
"github.com/ExchangeUnion/xud-docker-api/service"
"github.com/docker/docker/pkg/homedir"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
socketio "github.com/googollee/go-socket.io"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)

var (
logger = initLogger()
router = initRouter()
sioServer *socketio.Server

port uint16
tls bool
network string
)

func initLogger() *logrus.Entry {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
logrus.SetFormatter(&logging.Formatter{
})
logrus.SetOutput(os.Stdout)
logger := logrus.NewEntry(logrus.StandardLogger())
return logger
}

func initRouter() *gin.Engine {
r := gin.Default()
setupCors(r)
return r
type MyWriter struct {
}

type Config struct {
Port int
func (MyWriter) Write(p []byte) (int, error) {
logger.Debugf("%s", strings.TrimSpace(string(p)))
return len(p), nil
}

func loadConfig() (*Config, error) {
logger.Debug("Loading config")

err := godotenv.Load("/root/config.sh")
if err != nil {
logger.Debug("Skip /root/config.sh")
}
func initRouter() *gin.Engine {
r := gin.New()

var port int

app := &cli.App{
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port, p",
Value: 8080,
},
},
Action: func(c *cli.Context) error {
port = c.Int("port")
return nil
},
}
// Configuring Gin middlewares
//r.Use(ginlogrus.Logger(logrus.StandardLogger()))
//r.Use(gin.LoggerWithWriter(MyWriter{}))
r.Use(logging.LoggerOverLogrus())
r.Use(gin.Recovery())

err = app.Run(os.Args)
if err != nil {
logger.Fatal(err)
}
r.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.JSON(404, gin.H{"message": "not found"})
} else {
// redirect other non-API requests to ui/index.html to fit SPA requirements
c.File("ui/index.html")
}
})
r.NoMethod(func(c *gin.Context) {
c.JSON(405, gin.H{"message": "method not allowed"})
})

config := Config{Port: port}
setupCors(r)

return &config, nil
return r
}

func setupCors(r gin.IRouter) {
Expand All @@ -81,40 +77,20 @@ func setupCors(r gin.IRouter) {
// - Credentials share disabled
// - Preflight requests cached for 12 hours
config := cors.DefaultConfig()
config.AllowOrigins = []string{"https://localhost:3000"}
config.AllowOrigins = []string{"*"}

r.Use(cors.New(config))
}

func main() {

config, err := loadConfig()
if err != nil {
logger.Fatalf("Failed to load config: %s", err)
}

network := os.Getenv("NETWORK")

logger.Debug("Creating service manager")
manager, err := service.NewManager(network)
func initSioServer() {
server, err := NewSioServer(network)
if err != nil {
logger.Fatalf("Failed to create service manager: %s", err)
logger.Fatal(err)
}
defer func() {
err := manager.Close()
if err != nil {
logger.Fatalf("Failed to close service manager: %s", err)
}
}()

server, err := NewSioServer(network)
sioServer = server
initSioConsole()

if err != nil {
logger.Fatal(err)
}

go func() {
err := server.Serve()
defer func() {
Expand All @@ -128,32 +104,76 @@ func main() {
}
}()

logger.Debug("Creating router")
router.GET("/socket.io/", gin.WrapH(server))
router.Handle("WS", "/socket.io/", gin.WrapH(server))
}

r := router
func initLauncherWs() {
router.GET("/launcher", gin.WrapF(launcher.WsHandler))
router.Handle("WS", "/launcher", gin.WrapF(launcher.WsHandler))

r.GET("/socket.io/", gin.WrapH(server))
r.Handle("WS", "/socket.io/", gin.WrapH(server))
go launcher.StartLauncherRegistry()

r.NoRoute(func(c *gin.Context) {
c.File("ui/index.html")
})
r.NoMethod(func(c *gin.Context) {
c.JSON(405, gin.H{"message": "method not allowed"})
})
launcher.ConfigureRouter(router)
}

logger.Debug("Configuring router")
manager.ConfigureRouter(r)
func initServiceManager() {
logger.Debug("Creating service manager")
manager, err := service.NewManager(network)
if err != nil {
logger.Fatalf("Failed to create service manager: %s", err)
}
defer func() {
err := manager.Close()
if err != nil {
logger.Fatalf("Failed to close service manager: %s", err)
}
}()

manager.ConfigureRouter(router)
}

func serve() error {
var err error

addr := fmt.Sprintf(":%d", port)
logger.Infof("Serving at %s", addr)

if tls {
certFile := filepath.Join(homedir.Get(), ".proxy", "tls.crt")
keyFile := filepath.Join(homedir.Get(), ".proxy", "tls.key")
err = http.ListenAndServeTLS(addr, certFile, keyFile, router)
} else {
err = http.ListenAndServe(addr, router)
}
if err != nil {
return err
}
return nil
}

func main() {
var err error

logger.Infof("Serving at :%d", config.Port)
addr := fmt.Sprintf(":%d", config.Port)
network = os.Getenv("NETWORK")

cmd := &cobra.Command{
Use: "proxy",
Short: "The API gateway of xud-docker",
}
cmd.PersistentFlags().Uint16VarP(&port, "port", "p", 8080, "The port to listen")
cmd.PersistentFlags().BoolVar(&tls, "tls", false, "Enable TLS support")
err = cmd.Execute()
if err != nil {
logger.Fatalf("Failed to parse command-line options: %s", err)
}

certFile := path.Join(homedir.Get(), ".proxy", "tls.crt")
keyFile := path.Join(homedir.Get(), ".proxy", "tls.key")
initSioServer()
initLauncherWs()
initServiceManager()

err = http.ListenAndServeTLS(addr, certFile, keyFile, r)
//err = http.ListenAndServe(addr, r)
err = serve()
if err != nil {
logger.Fatalf("Failed to start the server: %s", err)
logger.Fatalf("Failed to serve: %s", err)
}
}
24 changes: 17 additions & 7 deletions cmd/proxy/socketio.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,32 @@ func NewSioServer(network string) (*socketio.Server, error) {
logger.Fatal(err)
}
server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
logger.Infof("[SocketIO] New client connected: ID=%v, RemoteAddr=%v", s.ID(), s.RemoteAddr())
logger.Debugf("[SocketIO/%s] CONNECT: RemoteAddr=%v", s.ID(), s.RemoteAddr())
t := s.RemoteHeader().Get("X-Type")
if t != "" {
logger.Debugf("[SocketIO/%s] Type=%s", s.ID(), t)
logger.Debugf("[SocketIO/%s] User-Agent=%s", s.ID(), s.RemoteHeader().Get("User-Agent"))
s.Join("launchers")
s.Emit("welcome! registered launcher id")
}
return nil
})
server.OnError("/", func(s socketio.Conn, e error) {
removeConsoles(s.ID())
logger.Errorf("[SocketIO] Client %v got an error: %v", s.ID(), e)
if s != nil {
//removeConsoles(s.ID())
//logger.Debugf("[SocketIO/%s] ERROR: %s", s.ID(), e)
logger.Debugf("[SocketIO/%v] ERROR: %s", s.ID(), e)
} else {
logger.Debugf("[SocketIO] ERROR: %s", e)
}
})

server.OnDisconnect("/", func(s socketio.Conn, reason string) {
removeConsoles(s.ID())
logger.Infof("[SocketIO] Client %v disconnected: %v", s.ID(), reason)
logger.Infof("[SocketIO/%s] DISCONNECTED: %s", s.ID(), reason)
})

server.OnEvent("/", "test", func(s socketio.Conn, data string) {
logger.Debugf("test: %v", data)
logger.Debugf("[SocketIO/%s] TEST: %s", s.ID(), data)
})

return server, nil
Expand Down
32 changes: 18 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,42 @@ go 1.15

require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/containerd/containerd v1.4.3 // indirect
github.com/creack/pty v1.1.11
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/docker v20.10.1+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/contrib v0.0.0-20201101042839-6a891bf89f19
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.3
github.com/google/uuid v1.1.2
github.com/googollee/go-socket.io v1.4.4
github.com/joho/godotenv v1.3.0
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/onsi/gomega v1.10.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.6.1 // indirect
github.com/ugorji/go v1.1.13 // indirect
github.com/urfave/cli/v2 v2.2.0
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
github.com/toorop/gin-logrus v0.0.0-20200831135515-d2ee50d38dae // indirect
github.com/ugorji/go v1.2.2 // indirect
github.com/ybbus/jsonrpc v2.1.2+incompatible
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/sys v0.0.0-20201106081118-db71ae66460a // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
google.golang.org/grpc v1.32.0
google.golang.org/protobuf v1.25.0 // indirect
google.golang.org/protobuf v1.25.0
gopkg.in/ini.v1 v1.61.0
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gotest.tools/v3 v3.0.3 // indirect
)
Loading