Skip to content

Commit

Permalink
go fmt project
Browse files Browse the repository at this point in the history
  • Loading branch information
ixqbar committed Jan 1, 2018
1 parent 43cc6cc commit 021ba55
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 64 deletions.
39 changes: 19 additions & 20 deletions src/proxy/client.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package proxy

import (
"github.com/gorilla/websocket"
"time"
"sync"
"errors"
"fmt"
"github.com/gorilla/websocket"
"sync"
"time"
)

type Client struct {
sync.Mutex
UUID string
conn *websocket.Conn
UUID string
conn *websocket.Conn
joinTime int64
alive bool
over chan bool
alive bool
over chan bool
}

func (obj *Client) PushMessage(message []byte) error {
Expand All @@ -33,7 +33,7 @@ func (obj *Client) Close() {
Logger.Printf("client %s[%s] SetReadDeadline failed %s", obj.conn.RemoteAddr(), obj.UUID, err)
}
obj.alive = false
<- obj.over
<-obj.over
}

func (obj *Client) Remove() {
Expand All @@ -44,28 +44,27 @@ func (obj *Client) Remove() {
obj.over <- true
}


type RequestClients struct {
sync.Mutex
num int
num int
Clients map[string]*Client
}

var Clients = NewRequestClients()

func NewClient(uuid string, conn *websocket.Conn) *Client {
return &Client{
UUID:uuid,
conn:conn,
joinTime:time.Now().Unix(),
over:make(chan bool),
alive:true,
UUID: uuid,
conn: conn,
joinTime: time.Now().Unix(),
over: make(chan bool),
alive: true,
}
}

func NewRequestClients() *RequestClients {
return &RequestClients{
Clients:make(map[string]*Client),
Clients: make(map[string]*Client),
}
}

Expand All @@ -79,7 +78,7 @@ func (obj *RequestClients) AddNewClient(uuid string, conn *websocket.Conn) *Clie
return obj.Clients[uuid]
}

func (obj *RequestClients) RemoveClient(uuid string) {
func (obj *RequestClients) RemoveClient(uuid string) {
obj.Lock()
defer obj.Unlock()

Expand All @@ -99,7 +98,7 @@ func (obj *RequestClients) PushMessage(uuid string, message []byte) error {
return obj.Clients[uuid].PushMessage(message)
}

func (obj *RequestClients) BroadcastMessage(message []byte) error {
func (obj *RequestClients) BroadcastMessage(message []byte) error {
obj.Lock()
defer obj.Unlock()

Expand All @@ -120,7 +119,7 @@ func (obj *RequestClients) Number() int {
return obj.num
}

func (obj *RequestClients) GetClient(uuid string) (*Client) {
func (obj *RequestClients) GetClient(uuid string) *Client {
obj.Lock()
defer obj.Unlock()

Expand All @@ -136,4 +135,4 @@ func (obj *RequestClients) RemoveAll() {
val.Close()
delete(obj.Clients, key)
}
}
}
16 changes: 8 additions & 8 deletions src/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
)

type ProxyParams struct {
Key string `xml:"key"`
Key string `xml:"key"`
Value string `xml:"value"`
}
}

type ProxyConfig struct {
AdminServerAddress string `xml:"admin_server"`
HttpServerAddress string `xml:"http_server"`
FcgiServerAddress string `xml:"fcgi_server"`
ScriptFileName string `xml:"script_filename"`
QueryString string `xml:"query_string"`
HeaderParams []ProxyParams `xml:"header_params>param"`
AdminServerAddress string `xml:"admin_server"`
HttpServerAddress string `xml:"http_server"`
FcgiServerAddress string `xml:"fcgi_server"`
ScriptFileName string `xml:"script_filename"`
QueryString string `xml:"query_string"`
HeaderParams []ProxyParams `xml:"header_params>param"`
}

var Config *ProxyConfig
Expand Down
2 changes: 1 addition & 1 deletion src/proxy/const.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package proxy

const VERSION = "0.0.3"
const VERSION = "0.0.3"
6 changes: 3 additions & 3 deletions src/proxy/logger.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package proxy

import (
"log"
"github.com/jonnywang/go-kits/redis"
"log"
)

func init() {
func init() {
redis.Logger.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}

var Logger = redis.Logger
var Logger = redis.Logger
30 changes: 16 additions & 14 deletions src/proxy/redis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package proxy

import (
"context"
"errors"
"github.com/jonnywang/go-kits/redis"
"os"
"os/signal"
"sync"
"syscall"
"github.com/jonnywang/go-kits/redis"
"context"
"time"
)

var (
Expand All @@ -26,7 +25,7 @@ func (obj *proxyRedisHandle) Init() error {
}

func (obj *proxyRedisHandle) Shutdown() {

Logger.Print("redis server will shutdown")
}

func (obj *proxyRedisHandle) Version() (string, error) {
Expand All @@ -37,7 +36,7 @@ func (obj *proxyRedisHandle) Number() (int, error) {
return Clients.Number(), nil
}

func (obj *proxyRedisHandle) Del(clientUUID string) (error) {
func (obj *proxyRedisHandle) Del(clientUUID string) error {
if clientUUID == "*" {
Clients.RemoveAll()
} else {
Expand All @@ -50,7 +49,7 @@ func (obj *proxyRedisHandle) Del(clientUUID string) (error) {
return nil
}

func (obj *proxyRedisHandle) Set(clientUUID string, message []byte) (error) {
func (obj *proxyRedisHandle) Set(clientUUID string, message []byte) error {
if clientUUID == "*" {
Clients.BroadcastMessage(message)
} else {
Expand Down Expand Up @@ -82,23 +81,24 @@ func Run() {
return
}

sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
redisStop := make(chan int)
stopSignal := make(chan os.Signal)
signal.Notify(stopSignal, syscall.SIGTERM, syscall.SIGINT)

ctx, cancel := context.WithCancel(context.Background())

httpServer := WebSocket(ctx)
httpServer, httpStop := NewWebSocket()

go func() {
<-sigs
<-stopSignal
Logger.Print("catch exit signal")
cancel()
server.Stop(10)
proxyRedisHandle.Shutdown()
server.Stop(10)
err := httpServer.Shutdown(ctx)
if err != nil {
Logger.Print(err)
}
redisStop <- 1
}()

Logger.Printf("redis protocol server run at %s", Config.AdminServerAddress)
Expand All @@ -108,6 +108,8 @@ func Run() {
Logger.Print(err)
}

time.Sleep(time.Duration(5) * time.Second)
}
<-redisStop
<-httpStop

Logger.Print("all server shutdown")
}
16 changes: 8 additions & 8 deletions src/proxy/utils.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package proxy

import (
"os"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"crypto/md5"
"io"
"encoding/hex"
"os"
"strings"
)

func CheckFileIsDirectory(path string) (bool, error) {
func CheckFileIsDirectory(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
Expand Down Expand Up @@ -62,17 +62,17 @@ func GetFileMD5sum(file string) (string, error) {
return hex.EncodeToString(h.Sum(nil)), nil
}

func HasIntersection(a []string, b []string) bool {
func HasIntersection(a []string, b []string) bool {
if len(a) == 0 || len(b) == 0 {
return false
}

t := strings.Join(b, "%") + "%"
for _,v := range a {
if strings.Contains(t, v + "%") {
for _, v := range a {
if strings.Contains(t, v+"%") {
return true
}
}

return false
}
}
17 changes: 9 additions & 8 deletions src/proxy/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package proxy

import (
"bytes"
"context"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/tomasen/fcgi_client"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"net/url"
)

var upgrader = websocket.Upgrader{
Expand Down Expand Up @@ -80,7 +79,6 @@ func proxyHttpHandle(w http.ResponseWriter, r *http.Request) {
remoteInfo := strings.Split(conn.RemoteAddr().String(), ":")
env["REMOTE_ADDR"] = remoteInfo[0]
env["REMOTE_PORT"] = remoteInfo[1]

env["PROXY_UUID"] = clientUUID

body := bytes.NewReader(nil)
Expand All @@ -93,7 +91,7 @@ func proxyHttpHandle(w http.ResponseWriter, r *http.Request) {
}

if messageType != websocket.TextMessage {
Logger.Printf("client %s[%s] read err message type", conn.RemoteAddr(),clientUUID)
Logger.Printf("client %s[%s] read err message type", conn.RemoteAddr(), clientUUID)
break
}

Expand Down Expand Up @@ -130,7 +128,7 @@ func proxyHttpHandle(w http.ResponseWriter, r *http.Request) {
}
}

func WebSocket(ctx context.Context) *http.Server {
func NewWebSocket() (*http.Server, chan int) {
http.HandleFunc("/", defaultHttpHandle)
http.HandleFunc("/proxy", proxyHttpHandle)

Expand All @@ -142,14 +140,17 @@ func WebSocket(ctx context.Context) *http.Server {
MaxHeaderBytes: 1 << 20,
}

httpStop := make(chan int)

go func() {
Logger.Printf("http server will run at %s", Config.HttpServerAddress)
err := httpServer.ListenAndServe()
if err != nil {
if err != nil && err != http.ErrServerClosed {
Logger.Print(err)
}
Logger.Printf("http server will stop at %s", Config.HttpServerAddress)
Logger.Printf("http server stop at %s", Config.HttpServerAddress)
httpStop <- 1
}()

return httpServer
return httpServer, httpStop
}
4 changes: 2 additions & 2 deletions test/fcgiServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"net"
"net/http"
"net/http/fcgi"
"net"
)

type FastCGIServer struct{}
Expand All @@ -19,4 +19,4 @@ func main() {
l, _ := net.Listen("tcp", "127.0.0.1:9003")
b := new(FastCGIServer)
fcgi.Serve(l, b)
}
}

0 comments on commit 021ba55

Please sign in to comment.