summaryrefslogtreecommitdiff
path: root/src/rhctl/web.go
blob: 2c5094c7865cb4dbf2883598be0e8bf1a7c1beb3 (plain)
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
//
//  rhctl
//
//  Copyright (C) 2009-2016 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of rhctl.
//
//  rhctl is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  rhctl 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 rhctl. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

type WebInterface struct {
	server *http.Server
}

type webHandler struct {
	ctrl *SwitchControl
	H    func(*SwitchControl, http.ResponseWriter, *http.Request)
}

func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	self.H(self.ctrl, w, r)
}

func webSendResponse(w http.ResponseWriter, status int, respdata interface{}) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	encoder := json.NewEncoder(w)
	encoder.Encode(respdata)
}

type webError struct {
	ResponseCode int    `json:"RESPONSE_CODE"`
	ErrorString  string `json:"ERROR_STRING"`
}

func webSendErrorResponse(w http.ResponseWriter, status int, error string) {
	webSendResponse(w, status, webError{ResponseCode: status, ErrorString: error})
}

func webGetStateHandler(ctrl *SwitchControl, w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		webSendErrorResponse(w, http.StatusMethodNotAllowed, "only GET method is allowed")
		return
	}

	resp := make(chan interface{})
	ctrl.Commands <- &Command{Type: CmdState, Response: resp}
	result := <-resp
	switch result.(type) {
	case State:
		webSendResponse(w, http.StatusOK, result.(State))
	case error:
		webSendErrorResponse(w, http.StatusInternalServerError, result.(error).Error())
	default:
		webSendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("invalid response of type %T: %+v", result, result))
	}
}

func (web *WebInterface) Run() {
	rhdl.Printf("Web: handler running...")
	if err := web.server.ListenAndServe(); err != nil {
		rhl.Printf("Web: server returned: %s", err)
	}
}

func WebInit(conf *Config, ctrl *SwitchControl) (web *WebInterface) {
	web = &WebInterface{}

	if conf.Clients.Web.StaticDir != "" {
		rhdl.Printf("Web: serving static files from '%s'", conf.Clients.Web.StaticDir)
		http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(conf.Clients.Web.StaticDir))))
	}
	http.Handle("/socket", webHandler{ctrl, webSocketHandler})

	http.Handle("/api/state", webHandler{ctrl, webGetStateHandler})
	// TODO: add other handler

	web.server = &http.Server{Addr: conf.Clients.Web.Address, ReadTimeout: 2 * time.Hour, WriteTimeout: 2 * time.Hour}
	return
}