Objective
Work needs, the first time to use Go to combat projects.
Requirements: Implement a WEBAPI transit gateway with Golang, and upload some resource files to Fastdfs Distributed File Storage System via HTTP protocol.
First, Fastdfs and Golang docking code
Github:https://github.com/weilaihui/fdfs_client
Source code can clone down to see, go syntax is very simple
Basic use: (There is a test case code in CLIENT_TEST.GO)
Package Mainimport ("FMT" "Io/ioutil" "github.com/weilaihui/fdfs_client") Func main () {ff, _:= Ioutil. ReadFile ("1.jpg") fmt. Println ("image len:", Len (FF))/*hosts: = []string{"10.0.1.32"} port: = 22122 Minconns: = ten Maxconns: = connpool,_: = fdfs_client. Newconnectionpool (Hosts, Port, Minconns, Maxconns)*/Path:="client.conf"FDS, Error:=fdfs_client. Newfdfsclient (PATH)ifFDS = =Nil {fmt. Println ("Conn Error:%s", error)varTeststringFMT. SCANLN (&test)return} uploadresponse, err:= FDs. Uploadbybuffer (FF,"jpg") ifUploadresponse = =Nil {fmt. Println ("Upload error:%s", Err)varTeststringFMT. SCANLN (&test)return} fmt. Println ("Group Name:", Uploadresponse.groupname) fmt. Println ("Remote file ID:", Uploadresponse.remotefileid)varTeststringFMT. SCANLN (&test)}
Second, the simple WebAPI gateway
Beego frame Go Circle is very famous, domestic university works, considering this project is small, temporarily not used up.
The go implementation of an API gateway is also fairly straightforward:
Package Mainimport ("FMT" "net/http") Func Main () {http. Handlefunc ("/", func (rw http. Responsewriter, req *http. Request) {rw. Write ([]byte("Hello Go Web") }) http. Handlefunc ("/upload", upload) http. Listenandserve ("localhost:8888", nil) fmt. Println ("End.")}func upload (rw http. Responsewriter, req*http. Request) {fmt. Println ("Header", req. Header) fmt. Println ("Content-type", req. Header.get ("Content-type") FMT. Println ("Body", req. Body)
Get the full contents of body
Len: = req. ContentLength Body: = Make ([]byte, Len) req. Body.read (body) RW. Write ([]byte ("Response Body ...."))
}
PS: The above code is only the use of their own notes, because the first go not familiar, only for learning.
File Upload Transit, if it is a larger file, then the use of the data fragmented transmission method.
Go language Implementation FASTDFS Distributed storage System WEBAPI Gateway