This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
/
web.go
138 lines (122 loc) · 5.67 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (C) 2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package web
import (
"fmt"
"github.com/CanonicalLtd/device-config/config"
"github.com/CanonicalLtd/device-config/service"
"github.com/CanonicalLtd/device-config/service/network"
"github.com/CanonicalLtd/device-config/service/snapd"
"github.com/CanonicalLtd/device-config/service/system"
"github.com/CanonicalLtd/device-config/service/transfer"
"github.com/gorilla/mux"
"log"
"net"
"net/http"
"strconv"
"syscall"
)
// Web implements the web service
type Web struct {
Settings *config.Settings
Auth service.AuthService
NetSrv network.Service
Snapd snapd.Client
TimeSrv service.TimeService
SystemSrv system.Service
TransferSrv transfer.Service
}
// NewWebService starts a new web service
func NewWebService(settings *config.Settings, auth service.AuthService, network network.Service, snapd snapd.Client, t service.TimeService, sys system.Service, xfer transfer.Service) *Web {
return &Web{
Settings: settings,
Auth: auth,
NetSrv: network,
Snapd: snapd,
TimeSrv: t,
SystemSrv: sys,
TransferSrv: xfer,
}
}
const (
soBINDTODEVICE = 0x19
)
// Start the web service
func (srv Web) Start() error {
if len(srv.Settings.NetworkInterfaceDevice) > 0 {
return srv.StartOnInterface(srv.Settings.NetworkInterfaceDevice)
}
listenOn := fmt.Sprintf("%s:%s", srv.Settings.NetworkInterfaceIP, srv.Settings.Port)
fmt.Printf("Starting service on port %s\n", listenOn)
return http.ListenAndServe(listenOn, srv.Router())
}
// StartOnInterface starts the web service on a specific network interface
func (srv Web) StartOnInterface(iface string) error {
fmt.Printf("Starting service on port %s:%s (%s)\n", srv.Settings.NetworkInterfaceIP, srv.Settings.Port, iface)
// Create a TCP listener
port, _ := strconv.Atoi(srv.Settings.Port)
listenOn := net.TCPAddr{
IP: net.ParseIP(srv.Settings.NetworkInterfaceIP),
Port: port,
}
tcpListener, err := net.ListenTCP("tcp", &listenOn)
if err != nil {
log.Fatal("net.ListenTCP()", err)
}
// Bind to a specific interface (SO_BINDTODEVICE is linux only)
f, _ := tcpListener.File()
syscall.SetsockoptString(int(f.Fd()), syscall.SOL_SOCKET, soBINDTODEVICE, iface)
// Service the routes
return http.Serve(tcpListener, srv.Router())
}
// Router returns the application router
func (srv Web) Router() *mux.Router {
// Start the web service router
router := mux.NewRouter()
router.Handle("/v1/config", Middleware(http.HandlerFunc(srv.AppConfig))).Methods("GET")
router.Handle("/v1/login", Middleware(http.HandlerFunc(srv.Login))).Methods("POST")
router.Handle("/v1/factory-reset", Middleware(http.HandlerFunc(srv.FactoryReset))).Methods("POST")
router.Handle("/v1/network", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Network))).Methods("GET")
router.Handle("/v1/network", srv.MiddlewareWithAuth(http.HandlerFunc(srv.NetworkInterface))).Methods("POST")
router.Handle("/v1/network/apply", srv.MiddlewareWithAuth(http.HandlerFunc(srv.NetworkApply))).Methods("POST")
router.Handle("/v1/proxy", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Proxy))).Methods("GET")
router.Handle("/v1/proxy", srv.MiddlewareWithAuth(http.HandlerFunc(srv.ProxyUpdate))).Methods("POST")
router.Handle("/v1/time", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Time))).Methods("GET")
router.Handle("/v1/time", srv.MiddlewareWithAuth(http.HandlerFunc(srv.TimeConfig))).Methods("POST")
router.Handle("/v1/services", srv.MiddlewareWithAuth(http.HandlerFunc(srv.AppServices))).Methods("GET")
router.Handle("/v1/system", srv.MiddlewareWithAuth(http.HandlerFunc(srv.SystemResources))).Methods("GET")
router.Handle("/v1/snaps", srv.MiddlewareWithAuth(http.HandlerFunc(srv.SnapList))).Methods("GET")
router.Handle("/v1/snaps/{snap}", srv.MiddlewareWithAuth(http.HandlerFunc(srv.SnapSet))).Methods("PUT")
router.Handle("/v1/transfer/export", srv.MiddlewareWithAuth(http.HandlerFunc(srv.TransferExport))).Methods("GET")
router.Handle("/v1/transfer/import", srv.MiddlewareWithAuth(http.HandlerFunc(srv.TransferImport))).Methods("POST")
router.Handle("/logout", Middleware(http.HandlerFunc(srv.Logout))).Methods("GET")
// Serve the static path
//p := path.Join(srv.Settings.DocRoot, "/static/")
fs := http.StripPrefix("/static/", http.FileServer(http.Dir(srv.Settings.DocRoot)))
router.PathPrefix("/static/").Handler(fs)
// Default path is the index page
router.Handle("/", Middleware(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/login", Middleware(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/network", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/proxy", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/time", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/services", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/snaps", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Index))).Methods("GET")
router.Handle("/settings", srv.MiddlewareWithAuth(http.HandlerFunc(srv.Index))).Methods("GET")
router.NotFoundHandler = Middleware(http.HandlerFunc(srv.Index))
return router
}