1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 21:29:42 +02:00
documize/server/server.go

138 lines
4.3 KiB
Go
Raw Normal View History

2016-07-21 17:57:38 +01:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
2017-07-21 18:14:19 +01:00
package server
2016-07-21 17:57:38 +01:00
import (
2021-03-16 13:58:27 -04:00
"crypto/tls"
2016-07-21 17:57:38 +01:00
"fmt"
"net/http"
"strings"
"github.com/codegangsta/negroni"
"github.com/documize/community/core/api/plugins"
"github.com/documize/community/core/database"
2017-07-21 14:53:32 +01:00
"github.com/documize/community/core/env"
"github.com/documize/community/domain/store"
2017-07-21 18:14:19 +01:00
"github.com/documize/community/server/routing"
"github.com/documize/community/server/web"
"github.com/gorilla/handlers"
2016-07-21 17:57:38 +01:00
"github.com/gorilla/mux"
)
var testHost string // used during automated testing
2017-07-21 18:14:19 +01:00
// Start router to handle all HTTP traffic.
func Start(rt *env.Runtime, s *store.Store, ready chan struct{}) {
2017-07-21 18:21:34 +01:00
// decide which mode to serve up
switch rt.Flags.SiteMode {
case env.SiteModeOffline:
2017-07-21 18:14:19 +01:00
rt.Log.Info("Serving OFFLINE web server")
case env.SiteModeSetup:
2017-07-21 18:14:19 +01:00
rt.Log.Info("Serving SETUP web server")
2017-08-29 17:55:41 +01:00
dbHandler := database.Handler{Runtime: rt, Store: s}
routing.Add(rt, routing.RoutePrefixPrivate, "setup", []string{"POST", "OPTIONS"}, nil, dbHandler.Setup)
case env.SiteModeBadDB:
2017-07-21 18:14:19 +01:00
rt.Log.Info("Serving BAD DATABASE web server")
2016-07-21 17:57:38 +01:00
default:
err := plugins.Setup(s)
if err != nil {
2017-08-29 17:55:41 +01:00
rt.Log.Error("plugin setup failed", err)
}
rt.Log.Info("Web Server: starting up")
2016-07-21 17:57:38 +01:00
}
// define middleware
2017-07-26 10:50:26 +01:00
cm := middleware{Runtime: rt, Store: s}
2017-07-21 18:21:34 +01:00
// define API endpoints
2017-07-26 10:50:26 +01:00
routing.RegisterEndpoints(rt, s)
2017-07-21 18:21:34 +01:00
// wire up API endpoints
2016-07-21 17:57:38 +01:00
router := mux.NewRouter()
// "/api/public/..."
2017-07-21 18:14:19 +01:00
router.PathPrefix(routing.RoutePrefixPublic).Handler(negroni.New(
negroni.HandlerFunc(cm.cors),
2017-07-21 18:14:19 +01:00
negroni.Wrap(routing.BuildRoutes(rt, routing.RoutePrefixPublic)),
2016-07-21 17:57:38 +01:00
))
// "/api/..."
2017-07-21 18:14:19 +01:00
router.PathPrefix(routing.RoutePrefixPrivate).Handler(negroni.New(
2017-08-02 15:26:31 +01:00
negroni.HandlerFunc(cm.Authorize),
2017-07-21 18:14:19 +01:00
negroni.Wrap(routing.BuildRoutes(rt, routing.RoutePrefixPrivate)),
2016-07-21 17:57:38 +01:00
))
// "/..."
2017-07-21 18:14:19 +01:00
router.PathPrefix(routing.RoutePrefixRoot).Handler(negroni.New(
negroni.HandlerFunc(cm.cors),
2017-07-21 18:14:19 +01:00
negroni.Wrap(routing.BuildRoutes(rt, routing.RoutePrefixRoot)),
2016-07-21 17:57:38 +01:00
))
// Look out for reverse proxy headers.
router.Use(handlers.ProxyHeaders)
2016-07-21 17:57:38 +01:00
n := negroni.New()
n.Use(negroni.NewStatic(web.StaticAssetsFileSystem()))
n.Use(negroni.HandlerFunc(cm.cors))
2016-07-21 17:57:38 +01:00
n.UseHandler(router)
// tell caller we are ready to serve HTTP
ready <- struct{}{}
2017-07-21 18:21:34 +01:00
// start server
if !rt.Flags.SSLEnabled() {
rt.Log.Info("Web Server: binding non-SSL server on " + rt.Flags.HTTPPort)
if rt.Flags.SiteMode == env.SiteModeSetup {
rt.Log.Info("***")
rt.Log.Info(fmt.Sprintf("*** Go to http://localhost:%s/setup in your web browser and complete setup wizard ***", rt.Flags.HTTPPort))
rt.Log.Info("***")
}
n.Run(testHost + ":" + rt.Flags.HTTPPort)
2016-07-21 17:57:38 +01:00
} else {
if rt.Flags.ForceHTTPPort2SSL != "" {
rt.Log.Info("Web Server: binding non-SSL server on " + rt.Flags.ForceHTTPPort2SSL + " and redirecting to SSL server on " + rt.Flags.HTTPPort)
2016-07-21 17:57:38 +01:00
go func() {
err := http.ListenAndServe(":"+rt.Flags.ForceHTTPPort2SSL, http.HandlerFunc(
2016-07-21 17:57:38 +01:00
func(w http.ResponseWriter, req *http.Request) {
2017-05-19 11:36:28 +01:00
w.Header().Set("Connection", "close")
var host = strings.Replace(req.Host, rt.Flags.ForceHTTPPort2SSL, rt.Flags.HTTPPort, 1) + req.RequestURI
2016-07-21 17:57:38 +01:00
http.Redirect(w, req, "https://"+host, http.StatusMovedPermanently)
}))
if err != nil {
rt.Log.Error("ListenAndServe on "+rt.Flags.ForceHTTPPort2SSL, err)
2016-07-21 17:57:38 +01:00
}
}()
}
if rt.Flags.SiteMode == env.SiteModeSetup {
rt.Log.Info("***")
rt.Log.Info(fmt.Sprintf("*** Go to https://localhost:%s/setup in your web browser and complete setup wizard ***", rt.Flags.HTTPPort))
rt.Log.Info("***")
}
rt.Log.Info("Web Server: starting SSL server on " + rt.Flags.HTTPPort + " with " + rt.Flags.SSLCertFile + " " + rt.Flags.SSLKeyFile)
2016-07-21 17:57:38 +01:00
2021-03-16 13:58:27 -04:00
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
}
server := &http.Server{Addr: ":" + rt.Flags.HTTPPort, Handler: n, TLSConfig: cfg}
2016-07-21 17:57:38 +01:00
server.SetKeepAlivesEnabled(true)
2017-07-21 14:53:32 +01:00
if err := server.ListenAndServeTLS(rt.Flags.SSLCertFile, rt.Flags.SSLKeyFile); err != nil {
rt.Log.Error("ListenAndServeTLS on "+rt.Flags.HTTPPort, err)
2016-07-21 17:57:38 +01:00
}
}
}