-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
76 lines (60 loc) · 1.29 KB
/
simple.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/katallaxie/pkg/debug"
"github.com/katallaxie/pkg/server"
)
type srv struct{}
func (s *srv) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error {
return func() error {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})
time.Sleep(3 * time.Second)
run(func() error {
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-ticker.C:
log.Print(time.Now())
case <-ctx.Done():
ticker.Stop()
return nil
}
}
})
ready()
s := http.Server{Addr: ":3000", Handler: chi.ServerBaseContext(ctx, r)}
if err := s.ListenAndServe(); err != nil {
return err
}
return nil
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s, _ := server.WithContext(ctx)
s.SetLimit(3)
s.Listen(&srv{}, true)
d := debug.New(
debug.WithPprof(),
debug.WithStatusAddr(":8888"),
)
s.Listen(d, false)
log.Printf("starting %s", server.Service.Name())
if err := s.Wait(); errors.Is(err, &server.Error{}) {
fmt.Println(err)
os.Exit(1)
}
}