-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
65 lines (60 loc) · 1.63 KB
/
transport.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
package transport
import (
"context"
"log"
"net/http"
"time"
"github.com/vodolaz095/asset_storage/internal/service"
)
type WebServer struct {
Srv *http.Server
Authentication *service.Authentication
Assets *service.Assets
Logger *log.Logger
}
func (s *WebServer) ListenHTTP(ctx context.Context, addr string) (err error) {
handler := http.NewServeMux()
handler.HandleFunc("/api/auth", s.login)
handler.HandleFunc("/api/upload-asset/", s.upload)
handler.HandleFunc("/api/asset/", s.get)
s.Srv = &http.Server{
Addr: addr,
Handler: handler,
}
go func() {
<-ctx.Done()
s.Logger.Printf("Stopping http server...")
cwto, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err = s.Srv.Shutdown(cwto)
if err != nil {
s.Logger.Printf("Error terminating HTTP server: %s", err)
} else {
s.Logger.Printf("HTTP server stopped...")
}
}()
return s.Srv.ListenAndServe()
}
func (s *WebServer) ListenHTTPS(ctx context.Context, addr string, pathToCert, pathToKey string) (err error) {
handler := http.NewServeMux()
handler.HandleFunc("/api/auth", s.login)
handler.HandleFunc("/api/upload-asset/", s.upload)
handler.HandleFunc("/api/asset/", s.get)
s.Srv = &http.Server{
Addr: addr,
Handler: handler,
}
go func() {
<-ctx.Done()
s.Logger.Printf("Stopping https server...")
cwto, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err = s.Srv.Shutdown(cwto)
if err != nil {
s.Logger.Printf("Error terminating HTTPS server: %s", err)
} else {
s.Logger.Printf("HTTPS server stopped...")
}
}()
return s.Srv.ListenAndServeTLS(pathToCert, pathToKey)
}