func main() { var vars Vars // Create logger. vars.log = log.NewLogger() vars.log.Info("initializing...") // Connect to the database. db, err := database.Connect(conf.C.DbType, conf.C.DbConn) if err != nil { vars.log.WithFields(logrus.Fields{ "err": err, "db_type": conf.C.DbType, "db_conn": conf.C.DbConn, }).Error("Could not connect to database") return } vars.db = db // Create datastore. vars.ds = database.NewDatastore(db) // Create router and add middleware. mux := router.New() mux.Use(webhelpers.Recoverer) mux.Use(middleware.Options) mux.Use(ContextMiddleware(&vars)) mux.Use(middleware.SetHeaders) // We wrap the Request ID middleware and our logger 'outside' the mux, so // all requests (including ones that aren't matched by the router) get // logged. var handler http.Handler = mux handler = webhelpers.LogrusLogger(vars.log, handler) handler = webhelpers.RequestID(handler) // Start serving vars.log.Infof("starting server on: %s", conf.C.HostString()) graceful.Run(conf.C.HostString(), 10*time.Second, handler) vars.log.Info("server finished") }
func main() { var vars Vars // Create logger. vars.log = log.NewLogger() vars.log.WithFields(logrus.Fields{ "project_name": conf.ProjectName, "version": conf.Version, "revision": conf.Revision, }).Info("initializing...") // Connect to the database. db, err := database.Connect(conf.C.DbType, conf.C.DbConn) if err != nil { vars.log.WithFields(logrus.Fields{ "err": err, "db_type": conf.C.DbType, "db_conn": conf.C.DbConn, }).Error("Could not connect to database") return } vars.db = db // Create datastore. vars.ds = database.NewDatastore(db) // Create API router and add middleware. apiMux := router.API() apiMux.Use(middleware.Options) apiMux.Use(middleware.JSON) // Create web router and add middleware. webMux := router.Web() webMux.Use(webhelpers.Recoverer) webMux.Use(ContextMiddleware(&vars)) webMux.Use(middleware.SetHeaders) // "Mount" the API mux under the web mux, on the "/api" prefix. webMux.Handle("/api/*", apiMux) // Serve all static assets. serveAssetAt := func(asset, path string) { info, _ := static.AssetInfo(asset) modTime := info.ModTime() data := static.MustAsset(asset) webMux.Get(path, func(w http.ResponseWriter, r *http.Request) { vars.log.Debugf("serving asset: %s", asset) http.ServeContent(w, r, asset, modTime, bytes.NewReader(data)) }) } for _, asset := range static.AssetNames() { vars.log.Debugf("adding route for asset: %s", asset) serveAssetAt(asset, "/static/"+asset) } // Special case a bunch of assets that should be served from the root. for _, asset := range []string{ "clientaccesspolicy.xml", "crossdomain.xml", "favicon.ico", "humans.txt", "robots.txt", } { // Note: only serve if we have this asset. if _, err := static.Asset(asset); err == nil { vars.log.Debugf("adding special route for asset: %s", asset) serveAssetAt(asset, "/"+asset) } } // Serve the index page if we have one. for _, asset := range []string{"index.html", "index.htm"} { // Note: only serve if we have this asset, and only serve the first // option. if _, err := static.Asset(asset); err == nil { vars.log.Debugf("adding index route for asset: %s", asset) serveAssetAt(asset, "/") break } } // We wrap the Request ID middleware and our logger 'outside' the mux, so // all requests (including ones that aren't matched by the router) get // logged. var handler http.Handler = webMux handler = webhelpers.LogrusLogger(vars.log, handler) handler = webhelpers.RequestID(handler) // Start serving vars.log.Infof("starting server on: %s", conf.C.HostString()) graceful.Run(conf.C.HostString(), 10*time.Second, handler) vars.log.Info("server finished") }