This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push:
new 070185a Refactor: add frontend server pkg (#1159)
070185a is described below
commit 070185a383c10df556d408eb3523a80d6fae920c
Author: little-cui <[email protected]>
AuthorDate: Mon Oct 11 14:07:27 2021 +0800
Refactor: add frontend server pkg (#1159)
---
frontend/main.go | 30 +++---------------------------
frontend/{main.go => server/config.go} | 18 ++++++++----------
frontend/{ => server}/server.go | 8 ++++----
frontend/{ => server}/server_test.go | 20 +++++++++++++-------
4 files changed, 28 insertions(+), 48 deletions(-)
diff --git a/frontend/main.go b/frontend/main.go
index 7118b74..e2a6b78 100644
--- a/frontend/main.go
+++ b/frontend/main.go
@@ -17,36 +17,12 @@
package main
import (
- "flag"
- "fmt"
-
- "net"
- "net/url"
- "strconv"
-
- "github.com/astaxie/beego"
+ "github.com/apache/servicecomb-service-center/frontend/server"
)
-type Config struct {
- frontendAddr string
- scAddr string
-}
-
func main() {
- frontendIp := beego.AppConfig.String("frontend_host_ip")
- frontendPort := beego.AppConfig.DefaultInt("frontend_host_port", 30103)
-
- scIp := beego.AppConfig.DefaultString("httpaddr", "127.0.0.1")
- scPort := beego.AppConfig.DefaultInt("httpport", 30100)
-
- // command line flags
- port := flag.Int("port", frontendPort, "port to serve on")
- flag.Parse()
-
- cfg := Config{}
- cfg.scAddr = fmt.Sprintf("http://%s/",
net.JoinHostPort(url.PathEscape(scIp), strconv.Itoa(scPort)))
- cfg.frontendAddr = net.JoinHostPort(frontendIp, strconv.Itoa(*port))
+ cfg := server.DefaultConfig()
// run frontend web server
- Serve(cfg)
+ server.Serve(cfg)
}
diff --git a/frontend/main.go b/frontend/server/config.go
similarity index 84%
copy from frontend/main.go
copy to frontend/server/config.go
index 7118b74..c1a73f2 100644
--- a/frontend/main.go
+++ b/frontend/server/config.go
@@ -14,12 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package main
+
+package server
import (
"flag"
"fmt"
-
"net"
"net/url"
"strconv"
@@ -28,11 +28,11 @@ import (
)
type Config struct {
- frontendAddr string
- scAddr string
+ FrontendAddr string
+ SCAddr string
}
-func main() {
+func DefaultConfig() Config {
frontendIp := beego.AppConfig.String("frontend_host_ip")
frontendPort := beego.AppConfig.DefaultInt("frontend_host_port", 30103)
@@ -44,9 +44,7 @@ func main() {
flag.Parse()
cfg := Config{}
- cfg.scAddr = fmt.Sprintf("http://%s/",
net.JoinHostPort(url.PathEscape(scIp), strconv.Itoa(scPort)))
- cfg.frontendAddr = net.JoinHostPort(frontendIp, strconv.Itoa(*port))
-
- // run frontend web server
- Serve(cfg)
+ cfg.SCAddr = fmt.Sprintf("http://%s/",
net.JoinHostPort(url.PathEscape(scIp), strconv.Itoa(scPort)))
+ cfg.FrontendAddr = net.JoinHostPort(frontendIp, strconv.Itoa(*port))
+ return cfg
}
diff --git a/frontend/server.go b/frontend/server/server.go
similarity index 94%
rename from frontend/server.go
rename to frontend/server/server.go
index 02157d0..9c9ed6f 100644
--- a/frontend/server.go
+++ b/frontend/server/server.go
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package main
+package server
import (
"log"
@@ -43,14 +43,14 @@ func Serve(c Config) {
scProxy(c, e)
- log.Printf("Error: %s", e.Start(c.frontendAddr))
+ log.Printf("Error: %s", e.Start(c.FrontendAddr))
}
// setup proxy for requests to service center
func scProxy(c Config, e *echo.Echo) {
- scUrl, err := url.Parse(c.scAddr)
+ scUrl, err := url.Parse(c.SCAddr)
if err != nil {
- log.Fatalf("Error parsing service center address:%s, err:%s",
c.scAddr, err)
+ log.Fatalf("Error parsing service center address:%s, err:%s",
c.SCAddr, err)
}
targets := []*middleware.ProxyTarget{
diff --git a/frontend/server_test.go b/frontend/server/server_test.go
similarity index 91%
rename from frontend/server_test.go
rename to frontend/server/server_test.go
index 9b52a0b..2c46fbe 100644
--- a/frontend/server_test.go
+++ b/frontend/server/server_test.go
@@ -14,17 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package main
+package server
import (
"io/ioutil"
"net/http"
+ "os"
+ "path/filepath"
"sync"
"testing"
- "github.com/stretchr/testify/assert"
-
"github.com/labstack/echo/v4"
+ "github.com/stretchr/testify/assert"
)
const (
@@ -32,12 +33,17 @@ const (
FrontAddr = "127.0.0.1:30104"
)
+func init() {
+ wd, _ := os.Getwd()
+ _ = os.Chdir(filepath.Join(wd, "../"))
+}
+
func TestStatic(t *testing.T) {
var wg sync.WaitGroup
cfg := Config{
- scAddr: "http://" + SCAddr,
- frontendAddr: FrontAddr,
+ SCAddr: "http://" + SCAddr,
+ FrontendAddr: FrontAddr,
}
wg.Add(1)
@@ -88,8 +94,8 @@ func TestDirectoryTraversal(t *testing.T) {
var wg sync.WaitGroup
cfg := Config{
- scAddr: "http://" + SCAddr,
- frontendAddr: FrontAddr,
+ SCAddr: "http://" + SCAddr,
+ FrontendAddr: FrontAddr,
}
wg.Add(1)