surrealpatch/web/web.go

114 lines
2.3 KiB
Go
Raw Normal View History

2016-04-03 21:48:57 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//,
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"github.com/abcum/fibre"
"github.com/abcum/fibre/mw"
2017-02-19 19:36:29 +00:00
2016-04-03 21:48:57 +00:00
"github.com/abcum/surreal/cnf"
2016-04-10 15:59:01 +00:00
"github.com/abcum/surreal/log"
2016-04-03 21:48:57 +00:00
)
// Setup sets up the server for remote connections
func Setup(opts *cnf.Options) (err error) {
2019-06-16 07:05:59 +00:00
log.WithPrefix("web").Infof("Starting web server on %s", opts.Conn)
2016-04-03 21:48:57 +00:00
s := fibre.Server()
2016-04-03 21:48:57 +00:00
routes(s)
2016-06-18 13:42:06 +00:00
s.SetName("web")
s.SetIdleTimeout("5s")
2020-03-28 09:59:51 +00:00
s.SetReadTimeout("30s")
2019-11-21 08:55:14 +00:00
s.SetWriteTimeout("60s")
2016-04-03 21:48:57 +00:00
s.SetHTTPErrorHandler(errors)
2016-04-10 15:59:01 +00:00
s.Logger().SetLogger(log.Instance())
2016-04-03 21:48:57 +00:00
// Setup middleware
2017-02-19 19:36:29 +00:00
s.Use(mw.Uniq()) // Add uniq id
2016-04-03 21:48:57 +00:00
s.Use(mw.Fail()) // Catch panics
2017-02-19 19:36:29 +00:00
s.Use(mw.Logs()) // Log requests
2016-04-03 21:48:57 +00:00
2017-11-16 20:46:54 +00:00
// Add cors headers
s.Use(mw.Cors(&mw.CorsOpts{
AllowedOrigin: "*",
2017-11-16 20:46:54 +00:00
AllowedMethods: []string{
"GET",
"PUT",
"POST",
"PATCH",
"DELETE",
"TRACE",
"OPTIONS",
},
AllowedHeaders: []string{
"Accept",
"Authorization",
"Content-Type",
"Origin",
"NS",
"DB",
"ID",
2017-11-16 20:46:54 +00:00
},
AccessControlMaxAge: 1800,
2017-11-16 20:46:54 +00:00
}))
2016-04-03 21:48:57 +00:00
// Check body size
s.Use(mw.Size(&mw.SizeOpts{
AllowedLength: 1 << 20, // 1mb
2016-04-03 21:48:57 +00:00
}))
// Setup authentication
s.Use(auth())
// Setup live queries
s.Use(live())
2017-11-23 13:38:00 +00:00
// Redirect non-https
s.Use(mw.Secure(&mw.SecureOpts{
2017-11-23 14:34:32 +00:00
RedirectHTTP: len(opts.Cert.Crt) != 0 || len(opts.Cert.Key) != 0,
2017-11-23 13:38:00 +00:00
}))
// Log successful start
2019-06-16 07:05:59 +00:00
log.WithPrefix("web").Infof("Started web server on %s", opts.Conn)
2016-04-03 21:48:57 +00:00
// Run the server
2016-06-18 13:42:06 +00:00
if len(opts.Cert.Crt) == 0 || len(opts.Cert.Key) == 0 {
2019-06-16 07:05:59 +00:00
s.Run(opts.Conn)
2016-04-13 14:40:19 +00:00
}
2016-06-18 13:42:06 +00:00
if len(opts.Cert.Crt) != 0 && len(opts.Cert.Key) != 0 {
2019-06-16 07:05:59 +00:00
s.Run(opts.Conn, opts.Cert.Crt, opts.Cert.Key)
2016-04-13 14:40:19 +00:00
}
2016-04-03 21:48:57 +00:00
return nil
}
// Exit tears down the server gracefully
2019-11-20 13:20:27 +00:00
func Exit(opts *cnf.Options) (err error) {
log.WithPrefix("web").Infof("Shutting down web server on %s", opts.Conn)
return
2016-04-03 21:48:57 +00:00
}