-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
50 lines (41 loc) · 997 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package clipty
import (
"context"
"errors"
"github.com/sorenisanerd/gotty/server"
)
// RunServer runs a cli web server
func RunServer(ctx context.Context, mainFunc MainFunc) error {
opt := server.Options{}
return RunServerWithOption(ctx, &opt, mainFunc)
}
func RunServerWithOption(ctx context.Context, opt *server.Options, mainFunc MainFunc) error {
return RunServerWithPersistParams(ctx, opt, mainFunc, nil)
}
func RunServerWithPersistParams(ctx context.Context, opt *server.Options, mainFunc MainFunc, persistParams map[string][]string) error {
if opt.Address == "" {
opt.Address = "0.0.0.0"
}
if opt.Port == "" {
opt.Port = "8080"
}
if err := opt.Validate(); err != nil {
return err
}
factory, err := newFactory(persistParams, mainFunc)
if err != nil {
return err
}
srv, err := server.New(factory, opt)
if err != nil {
return err
}
err = srv.Run(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
return err
}
return nil
}