Skip to content

Commit 0e71c83

Browse files
committed
issue #26 rename serv to router and start build Server class
1 parent 1d8aa81 commit 0e71c83

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

serv.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type node struct {
5050
fn Handler
5151
}
5252

53-
type serv struct {
53+
type router struct {
5454
methods map[string]*node
5555
redirects map[string]string
5656
needUid bool
@@ -68,15 +68,15 @@ type serv struct {
6868
}
6969

7070
var (
71-
rt *serv
72-
server *http.Server
71+
rt *router
72+
routerer *http.Server
7373

74-
ErrServerAlreadyStarted error = errors.New("server already started")
74+
ErrServerAlreadyStarted error = errors.New("routerer already started")
7575
)
7676

7777
func init() {
7878

79-
rt = &serv{
79+
rt = &router{
8080
methods: make(map[string]*node),
8181
redirects: make(map[string]string),
8282
notFoundFunc: func(c *Context) { c.StandardError(404) },
@@ -88,7 +88,7 @@ func init() {
8888
}
8989
}
9090

91-
func (sr *serv) optionsOrNotFound(c *Context) {
91+
func (sr *router) optionsOrNotFound(c *Context) {
9292
if sr.optionsFunc != nil && c.Method() == "OPTIONS" {
9393
sr.optionsFunc(c)
9494
} else {
@@ -230,7 +230,7 @@ func path2list(path string) []string {
230230
return list
231231
}
232232

233-
func (r *serv) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
233+
func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
234234

235235
if handler, has := r.fileHandlers[req.URL.Path]; has {
236236
handler.ServeHTTP(rw, req)
@@ -383,9 +383,9 @@ func makeUid(rw http.ResponseWriter, req *http.Request) {
383383

384384
func Start(addr string) error {
385385

386-
if server == nil {
387-
server = &http.Server{Addr: addr, Handler: rt}
388-
if err := server.ListenAndServe(); err != http.ErrServerClosed {
386+
if routerer == nil {
387+
routerer = &http.Server{Addr: addr, Handler: rt}
388+
if err := routerer.ListenAndServe(); err != http.ErrServerClosed {
389389
return err
390390
}
391391
return nil
@@ -394,15 +394,15 @@ func Start(addr string) error {
394394
}
395395

396396
func Shutdown() {
397-
if server != nil {
397+
if routerer != nil {
398398
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
399399
defer cancel()
400-
if err := server.Shutdown(ctx); err != nil {
400+
if err := routerer.Shutdown(ctx); err != nil {
401401
if rt != nil && rt.errorHandler != nil {
402402
rt.errorHandler(err)
403403
}
404404
}
405-
server = nil
405+
routerer = nil
406406
}
407407
}
408408

server.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package serv
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"time"
7+
)
8+
9+
type Server struct {
10+
router *router
11+
server *http.Server
12+
}
13+
14+
func New() *Server {
15+
16+
s := &Server{}
17+
18+
s.router = &router{
19+
methods: make(map[string]*node),
20+
redirects: make(map[string]string),
21+
notFoundFunc: func(c *Context) { c.StandardError(404) },
22+
badRequestFunc: func(c *Context) { c.StandardError(400) },
23+
internalErrorFunc: func(c *Context) { c.StandardError(500) },
24+
staticHandlers: make(map[string]http.Handler),
25+
fileHandlers: make(map[string]http.Handler),
26+
authCheck: func(login string, passwd string) bool { return false },
27+
}
28+
29+
return s
30+
}
31+
32+
func (s *Server) Start(addr string) error {
33+
34+
if s.server == nil {
35+
s.server = &http.Server{Addr: addr, Handler: s.router}
36+
if err := s.server.ListenAndServe(); err != http.ErrServerClosed {
37+
return err
38+
}
39+
return nil
40+
}
41+
return ErrServerAlreadyStarted
42+
}
43+
44+
func (s *Server) Shutdown() {
45+
if s.server != nil {
46+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
47+
defer cancel()
48+
if err := s.server.Shutdown(ctx); err != nil {
49+
if rt != nil && s.router.errorHandler != nil {
50+
s.router.errorHandler(err)
51+
}
52+
}
53+
s.server = nil
54+
}
55+
}

0 commit comments

Comments
 (0)