forked from heroiclabs/nakama
/
ops_accepter.go
110 lines (93 loc) · 3.32 KB
/
ops_accepter.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
102
103
104
105
106
107
108
109
110
// Copyright 2017 The Nakama Authors
//
// 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 server
import (
"encoding/json"
"fmt"
"net/http"
"nakama/build/generated/dashboard"
"os"
"runtime"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
// opsService is responsible for serving the dashboard and all of its required resources
type opsService struct {
logger *zap.Logger
version string
config Config
statsService StatsService
mux *mux.Router
dashboardFilesystem http.FileSystem
}
// NewOpsService creates a new opsService
func NewOpsService(logger *zap.Logger, multiLogger *zap.Logger, version string, config Config, statsService StatsService) *opsService {
service := &opsService{
logger: logger,
version: version,
config: config,
statsService: statsService,
mux: mux.NewRouter(),
dashboardFilesystem: &assetfs.AssetFS{
Asset: dashboard.Asset,
AssetDir: dashboard.AssetDir,
AssetInfo: dashboard.AssetInfo,
},
}
service.mux.HandleFunc("/v0/cluster/stats", service.statusHandler).Methods("GET")
service.mux.HandleFunc("/v0/config", service.configHandler).Methods("GET")
service.mux.HandleFunc("/v0/info", service.infoHandler).Methods("GET")
service.mux.PathPrefix("/").Handler(http.FileServer(service.dashboardFilesystem)).Methods("GET") //needs to be last
go func() {
bindAddr := fmt.Sprintf(":%d", config.GetOpsPort())
handlerWithCORS := handlers.CORS(handlers.AllowedOrigins([]string{"*"}))(service.mux)
err := http.ListenAndServe(bindAddr, handlerWithCORS)
if err != nil {
multiLogger.Fatal("Ops listener failed", zap.Error(err))
}
}()
multiLogger.Info("Ops", zap.Int("port", config.GetOpsPort()))
hostname, err := os.Hostname()
if err != nil {
hostname = "127.0.0.1"
}
multiLogger.Info("Dashboard", zap.String("url", fmt.Sprintf("http://%s:%d", hostname, config.GetOpsPort())))
return service
}
func (s *opsService) Stop() {}
func (s *opsService) statusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
stats := s.statsService.GetStats()
statsJSON, _ := json.Marshal(stats)
w.Write(statsJSON)
}
func (s *opsService) configHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
config, _ := json.Marshal(s.config)
w.Write(config)
}
func (s *opsService) infoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
info := map[string]interface{}{
"version": s.version,
"go": runtime.Version(),
"arch": runtime.GOARCH,
"os": runtime.GOOS,
"cpus": runtime.NumCPU(),
}
infoBytes, _ := json.Marshal(info)
w.Write(infoBytes)
}