Skip to content

Commit

Permalink
Merge pull request #76 from hermitkim1/refactor-project-to-apply-pack…
Browse files Browse the repository at this point in the history
…age-style

Refactor project to apply Golang package style
  • Loading branch information
hermitkim1 authored Mar 16, 2021
2 parents fa81e29 + 9828a1b commit aded4ec
Show file tree
Hide file tree
Showing 33 changed files with 151 additions and 137 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
*.exe
poc-cb-net/cmd/agent/agent
poc-cb-net/cmd/server/server
poc-cb-net/configs/

# Ignore configuration directory
# to avoid leaking sensitive information and frequent update
poc-cb-net/configs/**
# Provide templates of configuration files
# for users to copy and edit
!poc-cb-net/configs/template*
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
golang.org/x/tools v0.1.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)
15 changes: 8 additions & 7 deletions poc-cb-net/cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal/app"
dataobjects "github.com/cloud-barista/cb-larva/poc-cb-net/internal/data-objects"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal/cb-network"
dataobjects "github.com/cloud-barista/cb-larva/poc-cb-net/internal/cb-network/data-objects"
cblog "github.com/cloud-barista/cb-log"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/sirupsen/logrus"
Expand All @@ -16,7 +16,7 @@ import (
)

// CBNet represents a network for the multi-cloud.
var CBNet *internal.CBNetwork
var CBNet *cbnet.CBNetwork
var channel chan bool

// CBLogger represents a logger to show execution processes according to the logging level.
Expand Down Expand Up @@ -77,12 +77,13 @@ func main() {
CBLogger.Tracef("Random number: %d\t", n)

// Create CBNetwork instance with port, which is tunneling port
CBNet = internal.NewCBNetwork("cbnet0", 20000)
CBNet = cbnet.NewCBNetwork("cbnet0", 20000)

// Load a config of MQTTBroker
config, _ := dataobjects.LoadConfigMQTTBroker()
// Load config
configPath := filepath.Join("..", "..", "configs", "config.yaml")
config, _ := dataobjects.LoadConfigs(configPath)
// Create a endpoint link of MQTTBroker
server := "tcp://" + config.MQTTBrokerHost + ":" + config.MQTTBrokerPort
server := "tcp://" + config.MQTTBroker.Host + ":" + config.MQTTBroker.Port

// Create a ClientOptions struct setting the broker address, clientid, turn
// off trace output and set the default message handler
Expand Down
36 changes: 20 additions & 16 deletions poc-cb-net/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal"
dataobjects "github.com/cloud-barista/cb-larva/poc-cb-net/internal/data-objects"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal/cb-network"
dataobjects "github.com/cloud-barista/cb-larva/poc-cb-net/internal/cb-network/data-objects"
cblog "github.com/cloud-barista/cb-log"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/labstack/echo"
Expand All @@ -18,7 +18,7 @@ import (
"path/filepath"
)

var dscp *internal.DynamicSubnetConfigurator
var dscp *cbnet.DynamicSubnetConfigurator

// CBLogger represents a logger to show execution processes according to the logging level.
var CBLogger *logrus.Logger
Expand Down Expand Up @@ -75,24 +75,26 @@ func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c
}

// RunEchoServer represents a function to run echo server.
func RunEchoServer(config dataobjects.ConfigMQTTBroker) {
func RunEchoServer(config dataobjects.Config) {

webPath := "../../web"
CBLogger.Debug("Start.........")
e := echo.New()

e.Static("/", webPath+"/assets")
e.Static("/js", webPath+"/assets/js")
e.Static("/css", webPath + "/assets/css")
e.Static("/introspect", webPath + "/assets/introspect")

renderer := &TemplateRenderer{
templates: template.Must(template.ParseGlob("public/*.html")),
templates: template.Must(template.ParseGlob(webPath+"/public/*.html")),
}
e.Renderer = renderer

e.Static("/", "assets")
e.Static("/js", "assets/js")
e.Static("/css", "assets/css")
e.Static("/introspect", "assets/introspect")

e.GET("/", func(c echo.Context) error {
return c.Render(http.StatusOK, "index.html", map[string]interface{}{
"host": config.MQTTBrokerHost,
"port": config.MQTTBrokerPortForWebsocket,
"host": config.MQTTBroker.Host,
"port": config.MQTTBroker.PortForWebsocket,
})
})

Expand All @@ -111,12 +113,14 @@ func main() {
CBLogger.Tracef("Random number: %d\t", n)

// Create DynamicSubnetConfigurator instance
dscp = internal.NewDynamicSubnetConfigurator()
dscp = cbnet.NewDynamicSubnetConfigurator()

// Load config
configPath := filepath.Join("..", "..", "configs", "config.yaml")
config, _ := dataobjects.LoadConfigs(configPath)

// Load a config of MQTTBroker
config, _ := dataobjects.LoadConfigMQTTBroker()
// Create a endpoint link of MQTTBroker
server := "tcp://" + config.MQTTBrokerHost + ":" + config.MQTTBrokerPort
server := "tcp://" + config.MQTTBroker.Host + ":" + config.MQTTBroker.Port

// Create a ClientOptions struct setting the broker address, clientID, turn
// off trace output and set the default message handler
Expand Down
19 changes: 19 additions & 0 deletions poc-cb-net/configs/template)log_conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#### Config for CB-Log Lib. ####

cblog:
## true | false
loopcheck: true # This temp method for development is busy wait. cf) cblogger.go:levelSetupLoop().

## debug | info | warn | error
loglevel: debug # If loopcheck is true, You can set this online.

## true | false
logfile: false

## Config for File Output ##
logfileinfo:
filename: ./log/cblogs.log
# filename: $CBLOG_ROOT/log/cblogs.log
maxsize: 10 # megabytes
maxbackups: 50
maxage: 31 # days
4 changes: 4 additions & 0 deletions poc-cb-net/configs/template)mqtt-broker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mqtt_broker:
host: "xxx"
port: "xxx"
port_for_websocket: "xxx"
4 changes: 2 additions & 2 deletions poc-cb-net/internal/app/pitcher-and-catcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

import (
"fmt"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal/cb-network"
"log"
"net"
"os"
Expand Down Expand Up @@ -31,7 +31,7 @@ func MessageCatcher(conn *net.UDPConn) {
}

// PitcherAndCatcher represents a function to send messages continuously.
func PitcherAndCatcher(CBNet *internal.CBNetwork, channel chan bool) {
func PitcherAndCatcher(CBNet *cbnet.CBNetwork, channel chan bool) {
fmt.Println("Blocked till Networking Rule setup")
<-channel

Expand Down
Loading

0 comments on commit aded4ec

Please sign in to comment.