框架结构优化

This commit is contained in:
John
2017-12-29 22:11:03 +08:00
parent 4b4e514914
commit c867fafdda
6 changed files with 31 additions and 11 deletions

20
g/g.go Normal file
View File

@ -0,0 +1,20 @@
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
//
package g
import "gitee.com/johng/gf/g/net/ghttp"
const HTTP = 1
// 核心对象Server
// 框架支持多服务器对象通过传入不同的name进行区分
func HttpServer(names...string) *ghttp.Server {
name := "default"
if len(names) > 0 {
name = names[0]
}
return ghttp.GetServer(name)
}

View File

@ -12,14 +12,14 @@ import (
)
// tcp server结构体
type gTcpServer struct {
type Server struct {
address string
listener *net.TCPListener
handler func (net.Conn)
}
// 创建一个tcp server对象
func NewServer (address string, handler func (net.Conn)) *gTcpServer {
func NewServer(address string, handler func (net.Conn)) *Server {
tcpaddr, err := net.ResolveTCPAddr("tcp4", address)
if err != nil {
glog.Fatalln(err)
@ -30,6 +30,6 @@ func NewServer (address string, handler func (net.Conn)) *gTcpServer {
glog.Fatalln(err)
return nil
}
return &gTcpServer{ address, listen, handler}
return &Server{ address, listen, handler}
}

View File

@ -11,7 +11,7 @@ import (
)
// 执行监听
func (s *gTcpServer) Run() {
func (s *Server) Run() {
if s == nil || s.listener == nil {
glog.Println("start running failed: socket address bind failed")
return

View File

@ -12,14 +12,14 @@ import (
)
// tcp server结构体
type gUdpServer struct {
type Server struct {
address string
listener *net.UDPConn
handler func (*net.UDPConn)
}
// 创建一个tcp server对象
func NewServer (address string, handler func (*net.UDPConn)) *gUdpServer {
func NewServer (address string, handler func (*net.UDPConn)) *Server {
tcpaddr, err := net.ResolveUDPAddr("udp4", address)
if err != nil {
glog.Println(err)
@ -30,6 +30,6 @@ func NewServer (address string, handler func (*net.UDPConn)) *gUdpServer {
glog.Println(err)
return nil
}
return &gUdpServer{ address, listen, handler}
return &Server{ address, listen, handler}
}

View File

@ -9,7 +9,7 @@ package gudp
import "gitee.com/johng/gf/g/os/glog"
// 执行监听
func (s *gUdpServer) Run() {
func (s *Server) Run() {
if s == nil || s.listener == nil {
glog.Println("start running failed: socket address bind failed")
return

View File

@ -1,11 +1,11 @@
package main
import (
"gitee.com/johng/gf/g/frame/ginstance"
"gitee.com/johng/gf/g"
_ "gitee.com/johng/gf/geg/frame/mvc/controller/user"
)
func main() {
ginstance.Server().SetPort(8199)
ginstance.Server().Run()
g.HttpServer().SetPort(8199)
g.HttpServer().Run()
}