11package main
22
33import (
4- "bufio"
54 "bytes"
5+ "context"
66 "flag"
77 "fmt"
8+ "io"
89 "net"
910 "net/http"
1011 "os"
12+ "os/signal"
1113 "path/filepath"
1214 "regexp"
15+ "strconv"
1316 "strings"
1417 "text/template"
1518 "time"
@@ -21,27 +24,41 @@ import (
2124 "github.com/vscode-lcode/lcode/v2/bash"
2225 "github.com/vscode-lcode/lcode/v2/bash/webdav"
2326 "github.com/vscode-lcode/lcode/v2/hub"
27+ "github.com/vscode-lcode/lcode/v2/util/err0"
28+ "go.opentelemetry.io/otel"
29+ "go.opentelemetry.io/otel/attribute"
30+ "go.opentelemetry.io/otel/codes"
31+ "go.opentelemetry.io/otel/trace"
2432 "xorm.io/xorm"
2533)
2634
2735var args struct {
2836 hello string
2937 addr string
3038 localdomain string
39+ logLv string
3140}
3241
3342var VERSION = "dev"
3443var f = flag .NewFlagSet ("lcode-hub@" + VERSION , flag .ExitOnError )
44+ var defaultLogLv = "0"
3545
3646func init () {
3747 f .StringVar (& args .addr , "addr" , "127.0.0.1:4349" , "local-hub listen addr" )
3848 f .StringVar (& args .hello , "hello" , "webdav://{{.host}}.lo.shynome.com:4349{{.path}}" , "" )
3949 f .StringVar (& args .localdomain , "localdomain" , ".lo.shynome.com" , "" )
50+ f .StringVar (& args .logLv , "log" , defaultLogLv , "日志输出等级: 0 - 不输出; 1 - Error; 11 - Info; 111 - Debug" )
4051}
4152
53+ var tracer = otel .Tracer ("lcode-hub" )
54+
4255func main () {
4356 f .Parse (os .Args [1 :])
4457
58+ loglv := To1 (strconv .ParseInt (args .logLv , 2 , 64 ))
59+ tp := newTracerProvider (LogLevel (loglv ))
60+ defer tp .Shutdown (context .Background ())
61+
4562 if err := hasRunning (args .addr ); err == nil {
4663 return
4764 }
@@ -52,7 +69,14 @@ func main() {
5269 To (hub .Sync (db ))
5370
5471 l := To1 (net .Listen ("tcp" , args .addr ))
55- fmt .Printf ("lcode-hub is running on %s\n " , args .addr )
72+ defer l .Close ()
73+
74+ _ , span := tracer .Start (
75+ err0 .WithStatus (nil , codes .Ok , "" ),
76+ "lcode-hub is running" ,
77+ trace .WithAttributes (attribute .String ("addr" , args .addr )),
78+ )
79+ defer span .End ()
5680
5781 bash := bash .NewBash ()
5882 bash .VERSION = VERSION
@@ -61,8 +85,13 @@ func main() {
6185 helloTpl := To1 (template .New ("hello" ).Parse (args .hello + "\n " ))
6286 for client := range bash .Connected () {
6387 go func (c * webdav.Client ) {
64- fmt .Println ("client connected" , c .ID )
65- defer fmt .Println ("client disconnected" , c .ID )
88+ _ , span := tracer .Start (
89+ err0 .WithStatus (err0 .KeepEndOutput (nil ), codes .Ok , "" ),
90+ "client connected" ,
91+ trace .WithAttributes (attribute .String ("id" , c .ID )),
92+ )
93+ defer span .End ()
94+
6695 f := format .FindStringSubmatch (c .ID )
6796 if len (f ) != 3 {
6897 return
@@ -87,6 +116,7 @@ func main() {
87116 }
88117 }
89118 hello := string (output .Bytes ())
119+ hello = strings .TrimSuffix (hello , "\n " )
90120 hello = strings .ReplaceAll (hello , "\n " , "\n lo: " )
91121 c .Exec (fmt .Sprintf (">&2 echo lo: %s" , shellescape .Quote (hello )))
92122 if noEditTargets {
@@ -102,21 +132,34 @@ func main() {
102132 hub .LocalDomain = args .localdomain
103133
104134 go http .Serve (net .Listener (bash ), hub )
105- To (bash .Serve (l ))
135+ go bash .Serve (l )
136+
137+ c := make (chan os.Signal )
138+ signal .Notify (c , os .Interrupt )
139+ <- c
140+
106141}
107142
108143func hasRunning (addr string ) (err error ) {
144+ _ , span := tracer .Start (context .Background (), "lcode-hub check" )
145+ defer span .End ()
109146 defer err2 .Handle (& err )
110- client := http.Client {Timeout : 2 * time .Second }
111- resp := To1 (client .Get (fmt .Sprintf ("http://%s/version" , addr )))
112- defer resp .Body .Close ()
113- r := bufio .NewReader (resp .Body )
114- line , _ := To2 (r .ReadLine ())
115- if v := string (line ); v != "lcode-hub" {
116- err = fmt .Errorf ("expect lcode-hub, but got %s" , v )
147+
148+ conn := To1 (net .Dial ("tcp" , addr ))
149+ defer conn .Close ()
150+
151+ conn .SetDeadline (time .Now ().Add (2 * time .Second ))
152+
153+ To1 (io .WriteString (conn , "-1\n " ))
154+ _sid := To1 (io .ReadAll (conn ))
155+ sid := string (_sid )
156+
157+ if ! strings .HasPrefix (sid , "lcode-hub" ) {
158+ err = fmt .Errorf ("expect lcode-hub, but got %s" , sid )
117159 return
118160 }
119- version , _ := To2 (r .ReadLine ())
120- fmt .Printf ("lcode-hub already has running, version: %s. exit.\n " , string (version ))
161+
162+ span .SetStatus (codes .Error , "lcode-hub already has running" )
163+ span .SetAttributes (attribute .String ("version" , sid ))
121164 return
122165}
0 commit comments