-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
134 lines (112 loc) · 4.37 KB
/
server.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
// 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
package server
import (
"fmt"
"net/http"
"strings"
"github.com/codegangsta/negroni"
"github.com/documize/community/core/api/plugins"
"github.com/documize/community/core/database"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/store"
"github.com/documize/community/server/routing"
"github.com/documize/community/server/web"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
var testHost string // used during automated testing
// Start router to handle all HTTP traffic.
func Start(rt *env.Runtime, s *store.Store, ready chan struct{}) {
rt.Log.Info(fmt.Sprintf("Product: %s version %s (build %s)", rt.Product.Title, rt.Product.Version, rt.Product.Revision))
// decide which mode to serve up
switch rt.Flags.SiteMode {
case env.SiteModeOffline:
rt.Log.Info("Serving OFFLINE web server")
case env.SiteModeSetup:
rt.Log.Info("Serving SETUP web server")
dbHandler := database.Handler{Runtime: rt, Store: s}
routing.Add(rt, routing.RoutePrefixPrivate, "setup", []string{"POST", "OPTIONS"}, nil, dbHandler.Setup)
case env.SiteModeBadDB:
rt.Log.Info("Serving BAD DATABASE web server")
default:
err := plugins.Setup(s)
if err != nil {
rt.Log.Error("plugin setup failed", err)
}
rt.Log.Info("Web Server: starting up")
}
// define middleware
cm := middleware{Runtime: rt, Store: s}
// define API endpoints
routing.RegisterEndpoints(rt, s)
// wire up API endpoints
router := mux.NewRouter()
// "/api/public/..."
router.PathPrefix(routing.RoutePrefixPublic).Handler(negroni.New(
negroni.HandlerFunc(cm.cors),
negroni.Wrap(routing.BuildRoutes(rt, routing.RoutePrefixPublic)),
))
// "/api/..."
router.PathPrefix(routing.RoutePrefixPrivate).Handler(negroni.New(
negroni.HandlerFunc(cm.Authorize),
negroni.Wrap(routing.BuildRoutes(rt, routing.RoutePrefixPrivate)),
))
// "/..."
router.PathPrefix(routing.RoutePrefixRoot).Handler(negroni.New(
negroni.HandlerFunc(cm.cors),
negroni.Wrap(routing.BuildRoutes(rt, routing.RoutePrefixRoot)),
))
// Look out for reverse proxy headers.
router.Use(handlers.ProxyHeaders)
n := negroni.New()
n.Use(negroni.NewStatic(web.StaticAssetsFileSystem()))
n.Use(negroni.HandlerFunc(cm.cors))
n.UseHandler(router)
// tell caller we are ready to serve HTTP
ready <- struct{}{}
// 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)
} 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)
go func() {
err := http.ListenAndServe(":"+rt.Flags.ForceHTTPPort2SSL, http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Connection", "close")
var host = strings.Replace(req.Host, rt.Flags.ForceHTTPPort2SSL, rt.Flags.HTTPPort, 1) + req.RequestURI
http.Redirect(w, req, "https://"+host, http.StatusMovedPermanently)
}))
if err != nil {
rt.Log.Error("ListenAndServe on "+rt.Flags.ForceHTTPPort2SSL, err)
}
}()
}
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)
server := &http.Server{Addr: ":" + rt.Flags.HTTPPort, Handler: n /*, TLSConfig: myTLSConfig*/}
server.SetKeepAlivesEnabled(true)
if err := server.ListenAndServeTLS(rt.Flags.SSLCertFile, rt.Flags.SSLKeyFile); err != nil {
rt.Log.Error("ListenAndServeTLS on "+rt.Flags.HTTPPort, err)
}
}
}