Created
July 5, 2018 06:41
-
-
Save tianweidut/d1e0a8e726030646477598d91421c075 to your computer and use it in GitHub Desktop.
unix domain socket for golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"github.com/sirupsen/logrus" | |
"net/http" | |
"io/ioutil" | |
) | |
func dial(){ | |
addr, err := net.ResolveUnixAddr("unix", "test.sock") | |
if err != nil{ | |
logrus.Fatal(err.Error()) | |
} | |
conn, err := net.DialUnix("unix", nil, addr) | |
if err != nil{ | |
logrus.Fatal(err.Error()) | |
} | |
defer conn.Close() | |
r := make([]byte, 0, 10240) | |
buf := make([]byte, 10) | |
for{ | |
n, err := conn.Read(buf[:]) | |
if err != nil{ | |
break | |
} | |
r = append(r, buf[0:n]...) | |
} | |
logrus.Info(string(r)) | |
} | |
func httpRequest(){ | |
client := &http.Client{ | |
Transport: &http.Transport{ | |
Dial: func(network, addr string) (net.Conn, error) { | |
return net.Dial("unix", "test.sock") | |
}, | |
}, | |
} | |
resp, err := client.Get("http://unix/metrics") | |
if err != nil{ | |
logrus.Errorf("get failed :%q", err.Error()) | |
return | |
} | |
defer resp.Body.Close() | |
content, err := ioutil.ReadAll(resp.Body) | |
if err != nil{ | |
logrus.Errorf("failed read: %q", err.Error()) | |
return | |
} | |
logrus.Info(string(content)) | |
} | |
func main(){ | |
httpRequest() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/gorilla/mux" | |
"net/http" | |
"github.com/sirupsen/logrus" | |
"encoding/json" | |
"net" | |
"os" | |
) | |
func outputJson(w http.ResponseWriter, statusCode int, data interface{}) { | |
header := w.Header() | |
header.Set("Content-type", "application/json; charset=UTF-8") | |
w.WriteHeader(statusCode) | |
err := json.NewEncoder(w).Encode(data) | |
if err != nil { | |
logrus.Errorf("failed to outputJson: %v", err) | |
} | |
} | |
func main(){ | |
r := mux.NewRouter() | |
r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | |
outputJson(writer, http.StatusOK, &struct { | |
Status bool | |
Message string | |
}{ | |
true, | |
"ok", | |
}) | |
}) | |
r.HandleFunc("/metrics", func(writer http.ResponseWriter, request *http.Request) { | |
outputJson(writer, http.StatusOK, &struct { | |
Status bool | |
Message string | |
Metrics []string | |
}{ | |
true, | |
"ok", | |
[]string{"1","2","3"}, | |
}) | |
}) | |
r.HandleFunc("/failed", func(writer http.ResponseWriter, request *http.Request) { | |
outputJson(writer, http.StatusInternalServerError, &struct { | |
Status bool | |
Message string | |
}{ | |
false, | |
"failed", | |
}) | |
}) | |
path := "test.sock" | |
os.Remove(path) | |
srv := &http.Server{ | |
Handler: r, | |
} | |
l, err := net.Listen("unix", path) | |
if err != nil{ | |
logrus.Fatal(err.Error()) | |
} | |
logrus.Fatal(srv.Serve(l)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"github.com/golang/go/src/pkg/net" | |
"github.com/sirupsen/logrus" | |
"encoding/json" | |
) | |
type StatusResult struct { | |
Status bool | |
Message string | |
} | |
func main(){ | |
path := "test.sock" | |
os.Remove(path) | |
l, err := net.Listen("unix", path) | |
if err != nil{ | |
logrus.Error(err.Error()) | |
return | |
} | |
for { | |
logrus.Info("wait conn...") | |
conn, err := l.Accept() | |
if err != nil{ | |
logrus.Error(err.Error()) | |
continue | |
} | |
go echo(conn) | |
} | |
} | |
func echo(conn net.Conn){ | |
defer conn.Close() | |
r := StatusResult{ | |
true, | |
"test test ", | |
} | |
out, _ := json.Marshal(&r) | |
_, err := conn.Write(out) | |
if err != nil{ | |
logrus.Error(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment