loraserverソースコード解析(一)構築

19892 ワード

ソースアドレス


loraserver lorawan netserver、gateway通信app serverとインタラクティブ
lorawan lorawan通信データのシーケンス化に使用
 
golangのインストール
Golangのインストール
 
ソースのダウンロード
go get -u github.com/brocaar/loraserver
go get -u github.com/brocaar/lorawan

私の$GOPATHは~/go/gopathなので
最終的には~/go/gopath/src/githubにダウンロードされましたcom/loraserverと~/go/gopath/src/github.com/lorawan
 

依存ライブラリのインストール


loraserver lorawanはサードパーティ製ライブラリを大量に使用しています
loraserverディレクトリの下にあるMakefileのrequirementsには、インストールが必要なサードパーティ製ライブラリがすべて羅列されています(まだ不完全なようですよ)
dev-requirements:                                                                                                                                     
    go get -u github.com/kisielk/errcheck                                                                                                             
    go get -u golang.org/x/lint/golint                                                                                                                
    go get -u github.com/smartystreets/goconvey                                                                                                       
    go get -u golang.org/x/tools/cmd/stringer                                                                                                         
    go get -u github.com/golang/protobuf/protoc-gen-go                                                                                                
    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger                                                                               
    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway                                                                          
    go get -u github.com/elazarl/go-bindata-assetfs/...                                                                                               
    go get -u github.com/jteeuwen/go-bindata/...                                                                                                      
    go get -u github.com/golang/dep/cmd/dep                                                                                                           
    go get -u github.com/goreleaser/goreleaser                                                                                                        
    go get -u github.com/goreleaser/nfpm 

壁に登ることができれば、直接make dev-requirementsにインストールしてもいいかもしれませんが、必ずしも全部入っているとは限りません.
壁に登れないからorg  google.golang.orgなどのサイトはgo getでインストールできませんが、次のようなエラーが表示されます.
"golang.org/x/net": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/net?go-get=1": Get http://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused

幸いなことにgolang.org   google.golang.org依存ライブラリはgithubにあります
すべてhttps://github.com/golangこの中
git clone gitbubのコードで曲線をインストールできます.具体的な操作方法は参照してください.
logrusインストール
go stringerの使い方
GoConveyテストフレームワーク使用ガイド
go redisクライアントこのredigoはgomodule倉庫に移動されましたloraserverをコンパイルするためにダウンロードしなければなりませんhttps://github.com/garyburd/redigoのredigo.  
redigo
go grpc protobufインストール
 

私のインストール状況は大体こうです。

~/go/gopath/src/github.com/brocaar/loraserver $ make dev-requirements
go get -u github.com/kisielk/errcheck
package golang.org/x/tools/go/packages: unrecognized import path "golang.org/x/tools/go/packages" (https fetch: Get https://golang.org/x/tools/go/packages?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
Makefile:49: recipe for target 'dev-requirements' failed
make: *** [dev-requirements] Error 1
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u golang.org/x/lint/golint
package golang.org/x/lint/golint: unrecognized import path "golang.org/x/lint/golint" (https fetch: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/smartystreets/goconvey
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u golang.org/x/tools/cmd/stringer
package golang.org/x/tools/cmd/stringer: unrecognized import path "golang.org/x/tools/cmd/stringer" (https fetch: Get https://golang.org/x/tools/cmd/stringer?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/golang/protobuf/protoc-gen-go
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
package google.golang.org/genproto/googleapis/api/annotations: unrecognized import path "google.golang.org/genproto/googleapis/api/annotations" (https fetch: Get https://google.golang.org/genproto/googleapis/api/annotations?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
package google.golang.org/genproto/googleapis/api/annotations: unrecognized import path "google.golang.org/genproto/googleapis/api/annotations" (https fetch: Get https://google.golang.org/genproto/googleapis/api/annotations?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/jteeuwen/go-bindata/...
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/golang/dep/cmd/dep
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/goreleaser/goreleaser
package golang.org/x/sync/errgroup: unrecognized import path "golang.org/x/sync/errgroup" (https fetch: Get https://golang.org/x/sync/errgroup?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
package golang.org/x/oauth2: unrecognized import path "golang.org/x/oauth2" (https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
~/go/gopath/src/github.com/brocaar/loraserver $ go get -u github.com/goreleaser/nfpm

壁に囲まれた3つのライブラリを解決し始めました
に着くhttps://github.com/golang上を探して、後ろの経路は同じです
 

github.com/kisielk/errcheck 

cd $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/tools.git                                                                          
Cloning into 'tools'...                                                                                                                               
remote: Enumerating objects: 107, done.                                                                                                               
remote: Counting objects: 100% (107/107), done.                                                                                                       
remote: Compressing objects: 100% (84/84), done.                                                                                                      
remote: Total 24321 (delta 28), reused 70 (delta 17), pack-reused 24214                                                                               
Receiving objects: 100% (24321/24321), 11.77 MiB | 191.00 KiB/s, done.                                                                                
Resolving deltas: 100% (16861/16861), done.                                                                                                           
Checking connectivity... done. 

  2      ,    ~~~~~                                                                                                                       
~/go/gopath/src/golang.org/x $ go get -u golang.org/x/tools/go/packages
~/go/gopath/src/golang.org/x $ go get -u github.com/kisielk/errcheck

 


golang.org/x/lint/golint

~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/lint.git                                                                           
Cloning into 'lint'...                                                                                                                                
remote: Enumerating objects: 749, done.                                                                                                               
remote: Total 749 (delta 0), reused 0 (delta 0), pack-reused 749                                                                                      
Receiving objects: 100% (749/749), 333.46 KiB | 187.00 KiB/s, done.                                                                                   
Resolving deltas: 100% (442/442), done.                                                                                                               
Checking connectivity... done.                                                                                                                        
~/go/gopath/src/golang.org/x $ go get -u -v golang.org/x/lint/golint                                                                                  
Fetching https://golang.org/x/lint/golint?go-get=1                                                                                                    
https fetch failed: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                             
golang.org/x/lint (download)                                                                                                                          
Fetching https://golang.org/x/lint?go-get=1                                                                                                           
https fetch failed: Get https://golang.org/x/lint?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                                    
Fetching https://golang.org/x/tools/go/ast/astutil?go-get=1                                                                                           
https fetch failed: Get https://golang.org/x/tools/go/ast/astutil?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                    
golang.org/x/tools (download)                                                                                                                         
Fetching https://golang.org/x/tools/go/gcexportdata?go-get=1                                                                                          
https fetch failed: Get https://golang.org/x/tools/go/gcexportdata?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                                   
Fetching https://golang.org/x/tools/go/internal/gcimporter?go-get=1                                                                                   
https fetch failed: Get https://golang.org/x/tools/go/internal/gcimporter?go-get=1: dial tcp 216.239.37.1:443: i/o timeout                            
golang.org/x/tools/go/ast/astutil                                                                                                                     
golang.org/x/lint                                                                                                                                     
golang.org/x/lint/golint    

日志を见て、私は分かって、go get-uがそんなに遅いのは、いつも接続できないためで、実は私はすべてダウンロードしました.いいですよ.git cloneでいいのかbinファイルを生成する必要があるなら自分で手動でgo install
 

golang.org/x/tools/cmd/stringer

x/tools  ,       git clone   ,       ,       

~/go/gopath/src/golang.org/x $ cd $GOPATH/src/golang.org/x/tools/cmd/stringer/                                                                        
~/go/gopath/src/golang.org/x/tools/cmd/stringer $ go install   

       $GOPATH/bin ,        stringer
~/go/gopath/bin $ ls -tl                                                                                                                              
total 70665                                                                                                                                           
-rwxrwxr-x   1 zlgmcu         zlgmcu    6442218 2019-01-25 11:37 stringer  

 

google.golang.org/genproto/googleapis/api/annotations


これはgrpcを装着したときに発生したエラーです
go grpc protobufのインストールと使い方の簡単な例
 

golang.org/x/sync/errgroup


golang.org/x/oauth2

cd $GOPATH/src/golang.org/x/
~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/sync.git
Cloning into 'sync'...
remote: Enumerating objects: 4, done.        
remote: Counting objects: 100% (4/4), done.        
remote: Compressing objects: 100% (4/4), done.        
remote: Total 103 (delta 0), reused 1 (delta 0), pack-reused 99        
Receiving objects: 100% (103/103), 40.98 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Checking connectivity... done.
~/go/gopath/src/golang.org/x $ git  clone https://github.com/golang/oauth2.git
Cloning into 'oauth2'...
remote: Enumerating objects: 5, done.        
remote: Counting objects: 100% (5/5), done.        
remote: Compressing objects: 100% (2/2), done.        
remote: Total 1686 (delta 4), reused 3 (delta 3), pack-reused 1681        
Receiving objects: 100% (1686/1686), 517.60 KiB | 40.00 KiB/s, done.
Resolving deltas: 100% (1036/1036), done.
Checking connectivity... done.

cloud.google.com/go/pubsub


長い間探して、やっと見つけました.それらはすべて下のリンクの中にあります.
https://github.com/googleapis/google-cloud-go
ダウンロード後、適切なディレクトリにコピーすればよい
mkdir -p $GOPATH/src/cloud.google.com                                                                               
cd $GOPATH/src/cloud.google.com  
~/go/gopath/src/cloud.google.com $ git clone https://github.com/googleapis/google-cloud-go.git                                                         
Cloning into 'google-cloud-go'...                                                                                                                      
remote: Enumerating objects: 427, done.                                                                                                                
remote: Counting objects: 100% (427/427), done.                                                                                                        
remote: Compressing objects: 100% (186/186), done.                                                                                                     
remote: Total 20434 (delta 266), reused 332 (delta 221), pack-reused 20007                                                                             
Receiving objects: 100% (20434/20434), 14.46 MiB | 71.00 KiB/s, done.                                                                                  
Resolving deltas: 100% (14029/14029), done.                                                                                                            
Checking connectivity... done.   
~/go/gopath/src/cloud.google.com $ mv google-cloud-go go

 

google.golang.org/api/iterator


Googleapis go言語バージョンはこちらですhttps://github.com/googleapis/google-api-go-client
~/go/gopath/src/google.golang.org $ git clone https://github.com/googleapis/google-api-go-client.git                                                  
Cloning into 'google-api-go-client'...                                                                                                                
remote: Enumerating objects: 1495, done.                                                                                                              
remote: Counting objects: 100% (1495/1495), done.                                                                                                     
remote: Compressing objects: 100% (973/973), done.                                                                                                    
remote: Total 72438 (delta 687), reused 1013 (delta 363), pack-reused 70943                                                                           
Receiving objects: 100% (72438/72438), 132.27 MiB | 642.00 KiB/s, done.                                                                               
Resolving deltas: 100% (35349/35349), done.                                                                                                           
Checking connectivity... done.                                                                                                                                                                                                                                        
~/go/gopath/src/google.golang.org $ mv google-api-go-client/ api   

 
他の方法は何か欠けているものに似ています
 
redisインストール
ダウンロードhttps://redis.io/download   redis-4.0.10.tar        tar -xvf redis-4.0.10.tar.gz    make       make test   
   sudo make install
 
migrationsの生成
go generate cmd/loraserver/mainを実行します.go
go-bindataを使用してinternal/migrations/migrationsを生成します.gen.go
 
 
すべての依存をインストールした後dep ensure-v
私のコンピュータの下で以下のように表示して、壁にされて、2つの失敗があって、無視すればいいです
The following issues were found in Gopkg.toml:


  ✗ unable to deduce repository and source type for "google.golang.org/grpc": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://google.golang.org/grpc?go-get=1": Get http://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused
  ✗ unable to deduce repository and source type for "golang.org/x/net": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/net?go-get=1": Get http://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:80: connect: connection refused


  ProjectRoot name validation failed

 
loraserverをインストールした後、サードパーティ製ライブラリを追加しました.
~/go/gopath/src/github.com $ ls
alecthomas  fatih       grpc-ecosystem  lib          rubenv
apex        fsnotify    hashicorp       magiconair   satori
aws         garyburd    imdario         masterminds  sirupsen
blakesmith  ghodss      jacobsa         mattn        smartystreets
brocaar     golang      jmoiron         mitchellh    spf13
caarlos0    gomodule    jteeuwen        nsf          
campoy      google      jtolds          pelletier    
eclipse     goreleaser  kardianos       pkg          
elazarl     grpc        kisielk         rogpeppe   
~/go/gopath/src $ ls
github.com  google.golang.org  
golang.org  gopkg.in   
~/go/gopath/src/golang.org/x $ ls
crypto  lint  net  sys  text  tools
~/go/gopath/src/google.golang.org $ ls
genproto  grpc
~/go/gopath/src/gopkg.in $ ls
gorp.v1  yaml.v2

こんなにたくさんのプログラムが
~/go/gopath/bin/linux_386 $ ls
cobra               gocode    protoc-gen-go
dep                 goconvey  protoc-gen-grpc-gateway
errcheck            godef     protoc-gen-swagger
go-bindata          golint    stringer
go-bindata-assetfs  govendor  

 
コンパイルしようとすると構文エラーが表示されます
loraserverは4箇所uuidでerror戻り値を処理していない
dp.DeviceProfile.DeviceProfileID = uuid.NewV4().String()
github.com/satori/go.uuidディレクトリのgenerator.goちょっと直して
/* raw 
func NewV4() (UUID, error) {
	return global.NewV4()
}
*/
func NewV4() (UUID) { // modify by wjs, fix loraserver build error
	uuid,_ := global.NewV4()
	return uuid 
}

このように直すのはよくなくて、正しい方法はloraserverソースコードを変えるべきで、errorの戻り値を判断します.しばらくはこのようにコンパイルに変更しましょう:)
 
 
make build(loraserverディレクトリ)
buildディレクトリの下でloraserverプログラムが生成されます.
~/go/gopath/src/github.com/brocaar/loraserver $ make build

実行可能ファイルを生成
~/go/gopath/src/github.com/brocaar/loraserver/build $ ls

loraserver

loraserverソースコード解析(二)実行