Exemple #1
0
        protected virtual Yield PutConfig(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            XDoc config = request.ToDocument();

            if (config.Name != "config")
            {
                throw new DreamBadRequestException("bad document type");
            }
            if (IsStarted)
            {
                throw new DreamBadRequestException("service must be stopped first");
            }
            _timerFactory = TaskTimerFactory.Create(this);

            // configure service container
            var lifetimeScope = _env.CreateServiceLifetimeScope(this, (c, b) => PreInitializeLifetimeScope(c, b, config));

            // call container-less start (which contains shared start logic)
            yield return(Coroutine.Invoke(Start, request.ToDocument(), new Result()));

            // call start with container for sub-classes that want to resolve instances at service start
            yield return(Coroutine.Invoke(Start, config, lifetimeScope, new Result()));

            response.Return(DreamMessage.Ok(new XDoc("service-info")
                                            .Start("private-key")
                                            .Add(DreamCookie.NewSetCookie("service-key", PrivateAccessKey, Self.Uri).AsSetCookieDocument)
                                            .End()
                                            .Start("internal-key")
                                            .Add(DreamCookie.NewSetCookie("service-key", InternalAccessKey, Self.Uri).AsSetCookieDocument)
                                            .End()
                                            ));
        }
Exemple #2
0
        protected virtual Yield PutConfig(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            XDoc config = request.ToDocument();

            if (config.Name != "config")
            {
                throw new DreamBadRequestException("bad document type");
            }
            if (IsStarted)
            {
                throw new DreamBadRequestException("service must be stopped first");
            }
            _timerFactory = TaskTimerFactory.Create(this);

            // configure service container
            var components       = config["components"];
            var servicecontainer = _env.CreateServiceContainer(this);
            var builder          = new ContainerBuilder();

            builder.Register(_timerFactory).ExternallyOwned();
            if (!components.IsEmpty)
            {
                _log.Debug("registering service level module");
                builder.RegisterModule(new XDocAutofacContainerConfigurator(components, DreamContainerScope.Service));
            }
            builder.Build(servicecontainer);

            // call container-less start (which contains shared start logic)
            yield return(Coroutine.Invoke(Start, request.ToDocument(), new Result()));

            // call start with container for sub-classes that want to resolve instances at service start
            yield return(Coroutine.Invoke(Start, config, servicecontainer, new Result()));

            response.Return(DreamMessage.Ok(new XDoc("service-info")
                                            .Start("private-key")
                                            .Add(DreamCookie.NewSetCookie("service-key", PrivateAccessKey, Self.Uri).AsSetCookieDocument)
                                            .End()
                                            .Start("internal-key")
                                            .Add(DreamCookie.NewSetCookie("service-key", InternalAccessKey, Self.Uri).AsSetCookieDocument)
                                            .End()
                                            ));
        }
Exemple #3
0
        //--- Constructors ---
        public S3Storage(XDoc configuration, ILog log)
        {
            _timerFactory = TaskTimerFactory.Create(this);
            _log          = log;
            _publicKey    = configuration["publickey"].AsText;
            _privateKey   = configuration["privatekey"].AsText;
            _bucket       = configuration["bucket"].AsText;
            _prefix       = configuration["prefix"].AsText;
            if (string.IsNullOrEmpty(_publicKey))
            {
                throw new ArgumentException("Invalid Amazon S3 publickey");
            }
            if (string.IsNullOrEmpty(_privateKey))
            {
                throw new ArgumentException("Invalid Amazon S3 privatekey");
            }
            if (string.IsNullOrEmpty(_bucket))
            {
                throw new ArgumentException("Invalid Amazon S3 bucket");
            }
            if (string.IsNullOrEmpty(_prefix))
            {
                throw new ArgumentException("Invalid Amazon S3 prefix");
            }
            _tempDirectory = Path.Combine(Path.GetTempPath(), "s3_cache_" + XUri.EncodeSegment(_prefix));
            if (Directory.Exists(_tempDirectory))
            {
                Directory.Delete(_tempDirectory, true);
            }
            Directory.CreateDirectory(_tempDirectory);
            _allowRedirects  = configuration["allowredirects"].AsBool ?? false;
            _redirectTimeout = TimeSpan.FromSeconds(configuration["redirecttimeout"].AsInt ?? 60);
            _cacheTtl        = (configuration["cachetimeout"].AsInt ?? 60 * 60).Seconds();

            // initialize S3 plug
            _s3 = Plug.New("http://s3.amazonaws.com", TimeSpan.FromSeconds(configuration["timeout"].AsDouble ?? DEFAUTL_S3_TIMEOUT)).WithPreHandler(S3AuthenticationHeader).At(_bucket);
        }
        //--- Constructor ---
        internal DekiInstance(DekiWikiService deki, string id, XDoc instanceConfig, ILicenseController licenseController)
        {
            if (deki == null)
            {
                throw new ArgumentNullException("deki");
            }
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            this.Id = id;

            // Note (arnec): this is now the the third place we define a wikiid based logger repository, however constructors interdependencies
            // currently neccessitates this duplication, and fortunately it is mostly an aesthetic issue.
            _loggerRepository = new ContextLoggerRepository("[" + Id + "] ");
            _log = _loggerRepository.Get(GetType());
            this.TimerFactory = TaskTimerFactory.Create(this);
            this.Cache        = new DreamCache(TimerFactory);
            var cacheFactory     = new InMemoryKeyValueCacheFactory(TimerFactory);
            var searchSerializer = new SearchSerializer();

            cacheFactory.SetSerializer <SearchResult>(searchSerializer);
            cacheFactory.SetSerializer <SearchResultDetail>(searchSerializer);
            this.SearchCache   = cacheFactory.Create();
            this.Config        = instanceConfig;
            _licenseController = licenseController;
            this.Log           = LogManager.GetLogger(deki.GetType().Name + "_" + id);
            _deki   = deki;
            _status = DekiInstanceStatus.CREATED;
            _apiKey = Config[ConfigBL.SECURITY_APIKEY].AsText;
            foreach (XDoc hostDoc in Config["host"])
            {
                string host = hostDoc.Contents;
                if (!StringUtil.EqualsInvariantIgnoreCase(host, "*"))
                {
                    string port   = hostDoc["@port"].AsText;
                    string scheme = hostDoc["@https"].AsBool.GetValueOrDefault() ? "https://" : "http://";
                    string uri    = scheme + host + (string.IsNullOrEmpty(port) ? "" : ":" + port);
                    _canonicalUri = new XUri(uri);
                    _log.DebugFormat("divined canonical use from hosts as {0}", _canonicalUri);
                    break;
                }
            }
            if (_canonicalUri == null)
            {
                // Note (arnec): this is a best guess fallback. It will only work in these scenarios:
                // a) The host was set up with a uri.public that has ends in @api and with the @api points to the site uri, or
                // b) The api lives on the same machine as the site, so that deriving uri.public for the host from the machine
                // IP happens to point to the same machine
                // Either way it relies on the hard-coded assumption that the api is accessible via {site}/@api
                _canonicalUri = DreamContext.Current.ServerUri;
                if (_canonicalUri.LastSegment.EqualsInvariantIgnoreCase("@api"))
                {
                    _canonicalUri = _canonicalUri.WithoutLastSegment();
                }
                _log.DebugFormat("using server uri as canonical uri: {0}", _canonicalUri);
            }
            else
            {
                // Note (arnec): Propagating a much hard-coded assumption, i.e. that the Api for any Deki instance can be accessed
                // at the instances' canonical uri plus @api

                // register the api uri with the dream host so that requests originating from within Dream are guaranteed to be locally routed
                _deki.Env.At("status", "aliases").Post(new XDoc("aliases").Elem("uri.alias", _canonicalUri.At("@api")), new Result <DreamMessage>());
            }
        }