Skip to content

Commit 2440cd1

Browse files
committed
Refine code
1 parent 8bb4100 commit 2440cd1

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

gmoryx/sys_http.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
package gmoryx
2424

2525
import (
26-
"net/http"
27-
"net"
2826
"fmt"
27+
"net"
28+
"net/http"
2929
)
3030

3131
type HttpResponseWriter interface {
@@ -41,18 +41,19 @@ type HttpHandler interface {
4141
}
4242

4343
func HttpHandle(pattern string, handler HttpHandler) {
44-
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request){
45-
handler.ServeHTTP(w, &HttpRequest{r:r,})
44+
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
45+
handler.ServeHTTP(w, &HttpRequest{r: r})
4646
})
4747
}
4848

4949
var httpError error
5050
var httpListener net.Listener
51+
5152
func HttpListenAndServe(addr string, handler HttpHandler) (err error) {
5253
var h http.Handler
5354
if handler != nil {
54-
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
55-
handler.ServeHTTP(w, &HttpRequest{r:r,})
55+
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56+
handler.ServeHTTP(w, &HttpRequest{r: r})
5657
})
5758
}
5859

@@ -67,8 +68,8 @@ func HttpListenAndServe(addr string, handler HttpHandler) (err error) {
6768
return
6869
}
6970

70-
go func(){
71-
defer func(){
71+
go func() {
72+
defer func() {
7273
if r := recover(); r != nil {
7374
httpError = fmt.Errorf("Recover from %v", r)
7475
}

https/example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func ExampleLetsencryptManagerHttpAndHttps() {
4343

4444
var err error
4545
var m https.Manager
46-
if m,err = https.NewLetsencryptManager("", []string{"winlin.cn"}, "letsencrypt.cache"); err != nil {
46+
if m, err = https.NewLetsencryptManager("", []string{"winlin.cn"}, "letsencrypt.cache"); err != nil {
4747
fmt.Println("https failed, err is", err)
4848
return
4949
}
@@ -71,7 +71,7 @@ func ExampleLetsencryptManagerHttps() {
7171

7272
var err error
7373
var m https.Manager
74-
if m,err = https.NewLetsencryptManager("", []string{"winlin.cn"}, "letsencrypt.cache"); err != nil {
74+
if m, err = https.NewLetsencryptManager("", []string{"winlin.cn"}, "letsencrypt.cache"); err != nil {
7575
fmt.Println("https failed, err is", err)
7676
return
7777
}
@@ -102,7 +102,7 @@ func ExampleSelfSignHttps() {
102102
// openssl req -new -x509 -key server.key -out server.crt -days 365
103103
var err error
104104
var m https.Manager
105-
if m,err = https.NewSelfSignManager("server.crt", "server.key"); err != nil {
105+
if m, err = https.NewSelfSignManager("server.crt", "server.key"); err != nil {
106106
fmt.Println("https failed, err is", err)
107107
return
108108
}
@@ -137,7 +137,7 @@ func ExampleSelfSignHttpAndHttps() {
137137
// openssl req -new -x509 -key server.key -out server.crt -days 365
138138
var err error
139139
var m https.Manager
140-
if m,err = https.NewSelfSignManager("server.crt", "server.key"); err != nil {
140+
if m, err = https.NewSelfSignManager("server.crt", "server.key"); err != nil {
141141
fmt.Println("https failed, err is", err)
142142
return
143143
}

https/https.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ package https
2323

2424
import (
2525
"crypto/tls"
26-
"runtime"
27-
"strings"
28-
"strconv"
2926
"fmt"
3027
"github.com/ossrs/go-oryx-lib/https/letsencrypt"
28+
"runtime"
29+
"strconv"
30+
"strings"
3131
)
3232

3333
// Requires golang 1.6+, because there's bug in http.Server
@@ -36,9 +36,9 @@ func checkRuntime() (err error) {
3636
version := strings.Trim(runtime.Version(), "go")
3737
if versions := strings.Split(version, "."); len(versions) < 1 {
3838
return fmt.Errorf("invalid version=%v", version)
39-
} else if major,err := strconv.Atoi(versions[0]); err != nil {
39+
} else if major, err := strconv.Atoi(versions[0]); err != nil {
4040
return fmt.Errorf("invalid version=%v, err=%v", version, err)
41-
} else if minor,err := strconv.Atoi(versions[1]); err != nil {
41+
} else if minor, err := strconv.Atoi(versions[1]); err != nil {
4242
return fmt.Errorf("invalid version=%v, err=%v", version, err)
4343
} else if major == 1 && minor < 6 {
4444
return fmt.Errorf("requires golang 1.6+, version=%v(%v.%v)", version, major, minor)
@@ -54,7 +54,7 @@ type Manager interface {
5454

5555
// The cert is sign by ourself.
5656
type selfSignManager struct {
57-
cert *tls.Certificate
57+
cert *tls.Certificate
5858
certFile string
5959
keyFile string
6060
}
@@ -63,17 +63,17 @@ func NewSelfSignManager(certFile, keyFile string) (m Manager, err error) {
6363
if err = checkRuntime(); err != nil {
6464
return
6565
}
66-
return &selfSignManager{certFile: certFile, keyFile: keyFile},nil
66+
return &selfSignManager{certFile: certFile, keyFile: keyFile}, nil
6767
}
6868

6969
func (v *selfSignManager) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
7070
if v.cert != nil {
71-
return v.cert,nil
71+
return v.cert, nil
7272
}
7373

7474
cert, err := tls.LoadX509KeyPair(v.certFile, v.keyFile)
7575
if err != nil {
76-
return nil,err
76+
return nil, err
7777
}
7878

7979
// cache the cert.
@@ -116,7 +116,7 @@ func NewLetsencryptManager(email string, hosts []string, cacheFile string) (m Ma
116116
}
117117
}
118118

119-
return v,nil
119+
return v, nil
120120
}
121121

122122
func (v *letsencryptManager) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {

options/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func ExampleOptions() {
3333
// Parse config file from argv:
3434
// ./binary -c file
3535
// ./binary -c conf/oryx.json
36-
configFile := oo.ParseArgv("conf/console.json", "1.0", "oryx")
36+
configFile := oo.ParseArgv("conf/console.json", "1.0", "GoOryx/1.0")
3737

3838
var f *os.File
3939
if f, err = os.Open(configFile); err != nil {

options/options.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import (
3434
)
3535

3636
// parse the argv with config, version and signature.
37-
// @param rcf the recomment config file path.
37+
// @param rcf The recomment config file path.
38+
// @param version The vesion of application, such as 1.2.3
39+
// @param signature The signature of application, such as SRS/1.2.3
3840
func ParseArgv(rcf, version, signature string) (confFile string) {
3941
// the args format:
4042
// -c conf/ory.json
@@ -44,15 +46,15 @@ func ParseArgv(rcf, version, signature string) (confFile string) {
4446
// --conf=conf/oryx.json
4547
if true {
4648
dv := ""
47-
ua := "the config file"
49+
ua := "The config file"
4850
flag.StringVar(&confFile, "c", dv, ua)
4951
flag.StringVar(&confFile, "conf", dv, ua)
5052
}
5153

5254
var showVersion bool
5355
if true {
5456
dv := false
55-
ua := "print version"
57+
ua := "Print version"
5658
flag.BoolVar(&showVersion, "v", dv, ua)
5759
flag.BoolVar(&showVersion, "V", dv, ua)
5860
flag.BoolVar(&showVersion, "version", dv, ua)
@@ -69,10 +71,10 @@ func ParseArgv(rcf, version, signature string) (confFile string) {
6971
flag.Usage = func() {
7072
fmt.Println(signature)
7173
fmt.Println(fmt.Sprintf("Usage: %v [-c|--conf <filename>] [-?|-h|--help] [-v|-V|--version] [-g|--signature]", os.Args[0]))
72-
fmt.Println(fmt.Sprintf(" -c, --conf filename : the config file path"))
73-
fmt.Println(fmt.Sprintf(" -?, -h, --help : show this help and exit"))
74-
fmt.Println(fmt.Sprintf(" -v, -V, --version : print version and exit"))
75-
fmt.Println(fmt.Sprintf(" -g, --signature : print signature and exit"))
74+
fmt.Println(fmt.Sprintf(" -c, --conf filename : The config file path"))
75+
fmt.Println(fmt.Sprintf(" -?, -h, --help : Show this help and exit"))
76+
fmt.Println(fmt.Sprintf(" -v, -V, --version : Print version and exit"))
77+
fmt.Println(fmt.Sprintf(" -g, --signature : Print signature and exit"))
7678
fmt.Println(fmt.Sprintf("For example:"))
7779
fmt.Println(fmt.Sprintf(" %v -c %v", os.Args[0], rcf))
7880
}

0 commit comments

Comments
 (0)