-
Notifications
You must be signed in to change notification settings - Fork 328
/
web.go
101 lines (88 loc) · 2.72 KB
/
web.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"context"
"fmt"
"net"
"net/http"
"time"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/file"
"github.com/google/gapid/test/robot/monitor"
)
type Config struct {
Port int
StaticRoot file.Path
monitor.Managers
}
type Server struct {
listener listener
Config
o monitor.DataOwner
}
type listener struct {
*net.TCPListener
}
func Create(ctx context.Context, config Config) (*Server, error) {
server := &Server{Config: config}
server.o = monitor.NewDataOwner()
go func() {
if err := monitor.Run(ctx, server.Managers, server.o, nil); err != nil {
log.E(ctx, "Monitoring failed")
}
}()
l, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
return nil, err
}
if config.StaticRoot.IsEmpty() {
log.I(ctx, "Serving internal web content")
http.Handle("/", http.FileServer(static("www")))
} else {
path := config.StaticRoot.System()
log.I(ctx, "Serving web content from %s", path)
http.Handle("/", http.FileServer(http.Dir(path)))
}
http.HandleFunc("/tracks/", server.handleTracks)
http.HandleFunc("/packageChain/", server.handlePackageChain)
http.HandleFunc("/packages/", server.handlePackages)
http.HandleFunc("/artifacts/", server.handleArtifacts)
http.HandleFunc("/subjects/", server.handleSubjects)
http.HandleFunc("/satellites/", server.handleSatellites)
http.HandleFunc("/traces/", server.handleTraces)
http.HandleFunc("/replays/", server.handleReplays)
http.HandleFunc("/reports/", server.handleReports)
http.HandleFunc("/devices/", server.handleDevices)
http.HandleFunc("/workers/", server.handleWorkers)
http.HandleFunc("/entities/", server.handleEntities)
http.HandleFunc("/status/", server.handleStatus)
http.HandleFunc("/gridData/", server.handleGrid)
server.listener = listener{l.(*net.TCPListener)}
return server, nil
}
func (s *Server) Serve(ctx context.Context) error {
return http.Serve(s.listener, nil)
}
func (s *Server) Close() error {
return s.listener.Close()
}
func (l listener) Accept() (net.Conn, error) {
c, err := l.AcceptTCP()
if err == nil {
c.SetKeepAlive(true)
c.SetKeepAlivePeriod(3 * time.Minute)
}
return c, err
}