2020-10-16 19:12:53 +02:00
|
|
|
package web
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
import (
|
2021-06-21 11:21:42 +02:00
|
|
|
"errors"
|
2020-10-16 11:41:56 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-04-16 13:42:37 +02:00
|
|
|
"net/url"
|
2020-10-16 19:12:53 +02:00
|
|
|
"os"
|
2020-10-18 02:46:25 +02:00
|
|
|
"path"
|
2020-10-16 11:41:56 +02:00
|
|
|
"path/filepath"
|
2021-12-17 07:05:05 +01:00
|
|
|
"strings"
|
2021-04-16 13:42:37 +02:00
|
|
|
"text/template"
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-16 11:41:56 +02:00
|
|
|
)
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// RoutedService defines the interface that is needed for any service to
|
|
|
|
// register themself in the web server to provide new endpoints. (see
|
|
|
|
// AddRoutes).
|
2020-10-16 11:41:56 +02:00
|
|
|
type RoutedService interface {
|
|
|
|
RegisterRoutes(*mux.Router)
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// Server is the structure responsible for managing our http web server.
|
|
|
|
type Server struct {
|
2020-11-09 13:19:03 +01:00
|
|
|
http.Server
|
|
|
|
|
2022-06-03 12:10:46 +02:00
|
|
|
baseURL string
|
|
|
|
rootPath string
|
|
|
|
basePrefix string
|
|
|
|
port int
|
|
|
|
ssl bool
|
|
|
|
logger *mlog.Logger
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// NewServer creates a new instance of the webserver.
|
2021-05-29 08:23:10 +02:00
|
|
|
func NewServer(rootPath string, serverRoot string, port int, ssl, localOnly bool, logger *mlog.Logger) *Server {
|
2020-10-16 11:41:56 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
2022-06-03 12:10:46 +02:00
|
|
|
basePrefix := os.Getenv("FOCALBOARD_HTTP_SERVER_BASEPATH")
|
|
|
|
if basePrefix != "" {
|
|
|
|
r = r.PathPrefix(basePrefix).Subrouter()
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:29:13 +01:00
|
|
|
var addr string
|
|
|
|
if localOnly {
|
|
|
|
addr = fmt.Sprintf(`localhost:%d`, port)
|
|
|
|
} else {
|
|
|
|
addr = fmt.Sprintf(`:%d`, port)
|
|
|
|
}
|
|
|
|
|
2021-04-16 13:42:37 +02:00
|
|
|
baseURL := ""
|
|
|
|
url, err := url.Parse(serverRoot)
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Error("Invalid ServerRoot setting", mlog.Err(err))
|
2021-04-16 13:42:37 +02:00
|
|
|
}
|
|
|
|
baseURL = url.Path
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
ws := &Server{
|
2020-11-09 13:19:03 +01:00
|
|
|
Server: http.Server{
|
2021-01-27 00:29:13 +01:00
|
|
|
Addr: addr,
|
2020-11-09 13:19:03 +01:00
|
|
|
Handler: r,
|
|
|
|
},
|
2022-06-03 12:10:46 +02:00
|
|
|
baseURL: baseURL,
|
|
|
|
rootPath: rootPath,
|
|
|
|
port: port,
|
|
|
|
ssl: ssl,
|
|
|
|
logger: logger,
|
|
|
|
basePrefix: basePrefix,
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
2020-10-16 16:21:42 +02:00
|
|
|
|
|
|
|
return ws
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
func (ws *Server) Router() *mux.Router {
|
|
|
|
return ws.Server.Handler.(*mux.Router)
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// AddRoutes allows services to register themself in the webserver router and provide new endpoints.
|
|
|
|
func (ws *Server) AddRoutes(rs RoutedService) {
|
2020-11-09 13:19:03 +01:00
|
|
|
rs.RegisterRoutes(ws.Router())
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
func (ws *Server) registerRoutes() {
|
2022-06-03 12:10:46 +02:00
|
|
|
ws.Router().PathPrefix("/static").Handler(http.StripPrefix(ws.basePrefix+"/static/", http.FileServer(http.Dir(filepath.Join(ws.rootPath, "static")))))
|
2020-11-09 13:19:03 +01:00
|
|
|
ws.Router().PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-10-18 02:46:25 +02:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2021-04-16 13:42:37 +02:00
|
|
|
indexTemplate, err := template.New("index").ParseFiles(path.Join(ws.rootPath, "index.html"))
|
|
|
|
if err != nil {
|
2021-12-17 07:05:05 +01:00
|
|
|
ws.logger.Log(errorOrWarn(), "Unable to serve the index.html file", mlog.Err(err))
|
2021-06-21 11:21:42 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2021-04-16 13:42:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = indexTemplate.ExecuteTemplate(w, "index.html", map[string]string{"BaseURL": ws.baseURL})
|
|
|
|
if err != nil {
|
2021-12-17 07:05:05 +01:00
|
|
|
ws.logger.Log(errorOrWarn(), "Unable to serve the index.html file", mlog.Err(err))
|
2021-06-21 11:21:42 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2021-04-16 13:42:37 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-18 02:46:25 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-17 07:05:05 +01:00
|
|
|
// Start runs the web server and start listening for connections.
|
2021-03-01 20:36:36 +01:00
|
|
|
func (ws *Server) Start() {
|
2020-10-18 02:46:25 +02:00
|
|
|
ws.registerRoutes()
|
2021-05-24 19:06:11 +02:00
|
|
|
if ws.port == -1 {
|
2021-11-03 15:24:31 +01:00
|
|
|
ws.logger.Debug("server not bind to any port")
|
2021-05-24 19:06:11 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
isSSL := ws.ssl && fileExists("./cert/cert.pem") && fileExists("./cert/key.pem")
|
2020-10-16 11:41:56 +02:00
|
|
|
if isSSL {
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Info("https server started", mlog.Int("port", ws.port))
|
2021-01-18 21:27:13 +01:00
|
|
|
go func() {
|
|
|
|
if err := ws.ListenAndServeTLS("./cert/cert.pem", "./cert/key.pem"); err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Fatal("ListenAndServeTLS", mlog.Err(err))
|
2021-01-18 21:27:13 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Info("http server started", mlog.Int("port", ws.port))
|
2021-01-18 21:27:13 +01:00
|
|
|
go func() {
|
2021-06-21 11:21:42 +02:00
|
|
|
if err := ws.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Fatal("ListenAndServeTLS", mlog.Err(err))
|
2021-01-18 21:27:13 +01:00
|
|
|
}
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Info("http server stopped")
|
2021-01-18 21:27:13 +01:00
|
|
|
}()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
func (ws *Server) Shutdown() error {
|
|
|
|
return ws.Close()
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// fileExists returns true if a file exists at the path.
|
2020-10-16 19:12:53 +02:00
|
|
|
func fileExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
return err == nil
|
|
|
|
}
|
2021-12-17 07:05:05 +01:00
|
|
|
|
|
|
|
// errorOrWarn returns a `warn` level if this server instance is running unit tests, otherwise `error`.
|
|
|
|
func errorOrWarn() mlog.Level {
|
2022-05-05 21:49:34 +02:00
|
|
|
unitTesting := strings.ToLower(strings.TrimSpace(os.Getenv("FOCALBOARD_UNIT_TESTING")))
|
2021-12-17 07:05:05 +01:00
|
|
|
if unitTesting == "1" || unitTesting == "y" || unitTesting == "t" {
|
|
|
|
return mlog.LvlWarn
|
|
|
|
}
|
|
|
|
return mlog.LvlError
|
|
|
|
}
|