focalboard/server/web/webserver.go

138 lines
3.3 KiB
Go
Raw Normal View History

package web
2020-10-16 11:41:56 +02:00
import (
"errors"
2020-10-16 11:41:56 +02:00
"fmt"
"net/http"
"net/url"
"os"
"path"
2020-10-16 11:41:56 +02:00
"path/filepath"
"text/template"
2020-10-16 11:41:56 +02:00
"github.com/gorilla/mux"
"github.com/mattermost/focalboard/server/services/mlog"
2020-10-16 11:41:56 +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)
}
// Server is the structure responsible for managing our http web server.
type Server struct {
http.Server
baseURL string
rootPath string
port int
ssl bool
logger *mlog.Logger
2020-10-16 11:41:56 +02:00
}
// NewServer creates a new instance of the webserver.
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()
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)
}
baseURL := ""
url, err := url.Parse(serverRoot)
if err != nil {
logger.Error("Invalid ServerRoot setting", mlog.Err(err))
}
baseURL = url.Path
ws := &Server{
Server: http.Server{
2021-01-27 00:29:13 +01:00
Addr: addr,
Handler: r,
},
baseURL: baseURL,
2020-10-16 16:21:42 +02:00
rootPath: rootPath,
port: port,
ssl: ssl,
logger: logger,
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
}
func (ws *Server) Router() *mux.Router {
return ws.Server.Handler.(*mux.Router)
}
// AddRoutes allows services to register themself in the webserver router and provide new endpoints.
func (ws *Server) AddRoutes(rs RoutedService) {
rs.RegisterRoutes(ws.Router())
2020-10-16 11:41:56 +02:00
}
func (ws *Server) registerRoutes() {
ws.Router().PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(ws.rootPath, "static")))))
ws.Router().PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
indexTemplate, err := template.New("index").ParseFiles(path.Join(ws.rootPath, "index.html"))
if err != nil {
ws.logger.Error("Unable to serve the index.html file", mlog.Err(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
err = indexTemplate.ExecuteTemplate(w, "index.html", map[string]string{"BaseURL": ws.baseURL})
if err != nil {
ws.logger.Error("Unable to serve the index.html file", mlog.Err(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
})
}
// Start runs the web server and start listening for charsetnnections.
2021-03-01 20:36:36 +01:00
func (ws *Server) Start() {
ws.registerRoutes()
if ws.port == -1 {
ws.logger.Error("server not bind to any port")
return
}
2020-10-16 11:41:56 +02:00
isSSL := ws.ssl && fileExists("./cert/cert.pem") && fileExists("./cert/key.pem")
2020-10-16 11:41:56 +02:00
if isSSL {
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 {
ws.logger.Fatal("ListenAndServeTLS", mlog.Err(err))
2021-01-18 21:27:13 +01:00
}
}()
return
2020-10-16 11:41:56 +02:00
}
ws.logger.Info("http server started", mlog.Int("port", ws.port))
2021-01-18 21:27:13 +01:00
go func() {
if err := ws.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
ws.logger.Fatal("ListenAndServeTLS", mlog.Err(err))
2021-01-18 21:27:13 +01:00
}
ws.logger.Info("http server stopped")
2021-01-18 21:27:13 +01:00
}()
2020-10-16 11:41:56 +02:00
}
func (ws *Server) Shutdown() error {
return ws.Close()
}
// fileExists returns true if a file exists at the path.
func fileExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return err == nil
}