@@ -25,7 +25,6 @@ import (
2525 "fmt"
2626 "io"
2727 "math/rand"
28- "net/http"
2928 "os"
3029 "os/user"
3130 "path/filepath"
@@ -36,7 +35,6 @@ import (
3635 "time"
3736
3837 sqlite3 "github.com/CovenantSQL/go-sqlite3-encrypt"
39- "github.com/rakyll/statik/fs"
4038 "github.com/sirupsen/logrus"
4139 "github.com/xo/dburl"
4240 "github.com/xo/usql/drivers"
@@ -51,11 +49,10 @@ import (
5149 "github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
5250 "github.com/CovenantSQL/CovenantSQL/crypto/hash"
5351 "github.com/CovenantSQL/CovenantSQL/proto"
52+ "github.com/CovenantSQL/CovenantSQL/sqlchain/observer"
5453 "github.com/CovenantSQL/CovenantSQL/types"
5554 "github.com/CovenantSQL/CovenantSQL/utils"
5655 "github.com/CovenantSQL/CovenantSQL/utils/log"
57-
58- _ "github.com/CovenantSQL/CovenantSQL/cmd/cql/statik" // to embed the shardchain-explorer
5956)
6057
6158const name = "cql"
@@ -72,11 +69,14 @@ var (
7269 singleTransaction bool
7370 showVersion bool
7471 variables varsFlag
72+ stopCh = make (chan struct {})
73+ logLevel string
7574
7675 // Shard chain explorer stuff
77- tmpPath string // background observer and explorer block and log file path
78- cLog * logrus.Logger // console logger
79- bgLogLevel string // background log level
76+ tmpPath string // background observer and explorer block and log file path
77+ cLog * logrus.Logger // console logger
78+ bgLogLevel string // background log level
79+ explorerAddr string // explorer Web addr
8080
8181 // DML variables
8282 createDB string // as a instance meta json string or simply a node count
8888 waitTxConfirmation bool // wait for transaction confirmation before exiting
8989
9090 waitTxConfirmationMaxDuration time.Duration
91- explorerAddr string
9291)
9392
9493type userPermission struct {
@@ -230,6 +229,7 @@ func usqlRegister() {
230229}
231230
232231func init () {
232+ flag .StringVar (& logLevel , "log-level" , "" , "Service log level" )
233233 flag .StringVar (& dsn , "dsn" , "" , "Database url" )
234234 flag .StringVar (& command , "command" , "" , "Run only single command (SQL or usql internal command) and exit" )
235235 flag .StringVar (& fileName , "file" , "" , "Execute commands from file and exit" )
@@ -246,6 +246,7 @@ func init() {
246246 // Explorer
247247 flag .StringVar (& tmpPath , "tmp-path" , "" , "Explorer temp file path, use os.TempDir for default" )
248248 flag .StringVar (& bgLogLevel , "bg-log-level" , "" , "Background service log level" )
249+ flag .StringVar (& explorerAddr , "web" , "" , "Address to serve a database chain explorer, e.g. :8546" )
249250
250251 // DML flags
251252 flag .StringVar (& createDB , "create" , "" , "Create database, argument can be instance requirement json or simply a node count requirement" )
@@ -255,20 +256,18 @@ func init() {
255256 flag .BoolVar (& getBalance , "get-balance" , false , "Get balance of current account" )
256257 flag .StringVar (& getBalanceWithTokenName , "token-balance" , "" , "Get specific token's balance of current account, e.g. Particle, Wave, and etc." )
257258 flag .BoolVar (& waitTxConfirmation , "wait-tx-confirm" , false , "Wait for transaction confirmation" )
258-
259- flag .StringVar (& explorerAddr , "web" , "" , "Address to serve a database chain explorer, e.g. :8546" )
260259}
261260
262- func serverAsShardChainExplorer (addr string ) {
263- statikFS , err := fs . New ( )
261+ func serverAsShardChainExplorer (webAddr string ) {
262+ service , httpServer , err := observer . StartObserver ( webAddr , version )
264263 if err != nil {
265- log .WithError (err ).Fatal ("unable to create statik fs " )
264+ log .WithError (err ).Fatal ("start observer failed " )
266265 }
266+ <- stopCh
267267
268- http .Handle ("/" , http .FileServer (statikFS ))
269- if err := http .ListenAndServe (addr , nil ); err != nil {
270- log .WithError (err ).Error ("http.ListenAndServe" )
271- }
268+ _ = observer .StopObserver (service , httpServer )
269+
270+ log .Info ("observer stopped" )
272271}
273272
274273func main () {
@@ -277,6 +276,7 @@ func main() {
277276 rand .Seed (time .Now ().UnixNano ())
278277
279278 flag .Parse ()
279+ log .SetStringLevel (logLevel , log .InfoLevel )
280280 if tmpPath == "" {
281281 tmpPath = os .TempDir ()
282282 }
@@ -298,17 +298,25 @@ func main() {
298298 }
299299 cLog .Infof ("cql build: %#v\n " , version )
300300
301+ configFile = utils .HomeDirExpand (configFile )
302+
301303 if explorerAddr != "" {
302- serverAsShardChainExplorer (explorerAddr )
303- return
304- }
304+ var err error
305+ conf .GConf , err = conf .LoadConfig (configFile )
306+ if err != nil {
307+ log .WithField ("config" , configFile ).WithError (err ).Fatal ("load config failed" )
308+ }
305309
306- configFile = utils .HomeDirExpand (configFile )
307- // init covenantsql driver
308- if err = client .Init (configFile , []byte (password )); err != nil {
309- cLog .WithError (err ).Error ("init covenantsql client failed" )
310- os .Exit (- 1 )
310+ serverAsShardChainExplorer (explorerAddr )
311+ defer close (stopCh )
311312 return
313+ } else {
314+ // init covenantsql driver
315+ if err = client .Init (configFile , []byte (password )); err != nil {
316+ cLog .WithError (err ).Error ("init covenantsql client failed" )
317+ os .Exit (- 1 )
318+ return
319+ }
312320 }
313321
314322 // TODO(leventeliu): discover more specific confirmation duration from config. We don't have
0 commit comments