1 /** 2 * Copyright © Yurai Web Framework 2021 3 * License: MIT (https://github.com/YuraiWeb/yurai/blob/main/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module yurai.external.iserver; 7 import yurai.core.settings; 8 import yurai.services; 9 10 public interface IServer 11 { 12 @property string name(); 13 void setup(string[] ipAddresses, ushort port, bool debugMode, string staticFilesFolder); 14 void run(); 15 16 IServer registerPreService(IPreMiddleware middleware); 17 IServer registerContentService(IContentMiddleware middleware); 18 IServer registerPostService(IPostMiddleware middleware); 19 20 @property IPreMiddleware[] preServices(); 21 @property IContentMiddleware[] contentServices(); 22 @property IPostMiddleware[] postServices(); 23 @property IMailService mailService(); 24 25 IServer registerMailService(IMailService mailService); 26 } 27 28 private IServer[string] _servers; 29 30 public: 31 void registerServer(IServer server) 32 { 33 _servers[server.name] = server; 34 } 35 36 IServer setupServer(string name, string[] ipAddresses, ushort port, string staticFilesFolder) 37 { 38 auto server = _servers.get(name, null); 39 40 if (!server) 41 { 42 return null; 43 } 44 45 server.setup(ipAddresses, port, Yurai_IsDebugging, staticFilesFolder); 46 47 return server; 48 } 49 50 private string[] _registeredViews; 51 52 void registerView(string file, string name, string route = null, string contentType = null, string additionalPreContent = null) 53 { 54 import std.file : readText; 55 import std.string : format; 56 57 _registeredViews ~= "@[name: %s]\r\n%s%s%s".format(name, route ? "@[route: %s]\r\n".format(route) : "", contentType ? "@[content-type: %s]\r\n".format(contentType) : "", (additionalPreContent ? additionalPreContent : "") ~ readText(file)); 58 } 59 60 bool registerServers() 61 { 62 static if (!Yurai_IsPreBuilding) 63 { 64 static if (Yurai_UseVibed) 65 { 66 import yurai.external.servers.vibed; 67 VibedServer.register(); 68 } 69 } 70 else 71 { 72 import yurai.prebuilding : preBuild; 73 74 preBuild(_registeredViews); 75 } 76 77 return !Yurai_IsPreBuilding; 78 }