The Revel server engine gives the developer the freedom to implement the server howeverthey would like to choose. By default the Go HTTP engine is used, but you can also usethe fasthttp engine or the newrelic engine. Or configure an engine to meet your needs.For example you can design an engine to listen on multiple IP addresses and serve outyour website that way.

Registration

To register revel.RegisterServerEngine(name string, loader func()revel.ServerEngine) to registeryour server engine.

App.conf

  • server.engine Defaults to go. Specify the engine you wish to use.If you are using a module server engine you must declare the module in the modules section.

    Server Engines

To implement your own custom server engine by implementing the revel.ServerEngine.

  1. typeServerEngineinterface{// Initialize the server (non blocking)Init(init*EngineInit)// Starts the server. This will block until server is stoppedStart()// Fires a new event to the serverEvent(eventint,argsinterface{})// Returns the engine instance for specific callsEngine()interface{}// Returns the engine NameName()string// Returns any statsStats()map[string]interface{}}typeEngineInitstruct{Address,NetworkstringPortintCallbackfunc(ServerContext)}

Interface methods

  • Init() Called when the server engine is created, it is called passing in a revel.EngineInit object which contains the basic data needed to initialize the engine and the Engine.Callback which is how the server conveys the request/response traffic to revel. This is called before any revel.StartupHooks are called
  • Start() Called as the final step. This call is not expected to return unless the engine has shutdown
  • Event() The event interface will receive events from the revel.InitEventHandler
  • Name() is the name of the engine which will match the app.conf parameter
  • Stats() is called to retrieve any statistics the engine may want to provide
    Seerevel.GOHttpServer for an example

Server ContextThe server context is the communication bridge between Revel and the revel.ServerEngine

  1. ServerContextinterface{GetRequest()ServerRequestGetResponse()ServerResponse}// Callback ServerRequest typeServerRequestinterface{GetRaw()interface{}Get(theTypeint)(interface{},error)Set(theTypeint,theValueinterface{})bool}// Callback ServerResponse typeServerResponseinterface{ServerRequest}// Callback WebSocket typeServerWebSocketinterface{ServerResponseMessageSendJson(vinterface{})errorMessageReceiveJson(vinterface{})error}// Expected response for HTTP_SERVER_HEADER type (if implemented)ServerHeaderinterface{SetCookie(cookiestring)GetCookie(keystring)(valueServerCookie,errerror)Set(keystring,valuestring)Add(keystring,valuestring)Del(keystring)Get(keystring)(value[]string)SetStatus(statusCodeint)}// Expected response for FROM_HTTP_COOKIE type (if implemented)ServerCookieinterface{GetValue()string}// Expected response for HTTP_MULTIPART_FORMServerMultipartForminterface{GetFile()map[string][]*multipart.FileHeaderGetValue()url.ValuesRemoveAll()error}StreamWriterinterface{WriteStream(namestring,contentlenint64,modtimetime.Time,readerio.Reader)error}

As you can see the majority of communications are done through a Set/Get with a key.The key determines what Revel is expecting for the the data.

  1. const(/* Minimum Engine Type Values */_=iotaENGINE_RESPONSE_STATUSENGINE_WRITERENGINE_PARAMETERSENGINE_PATHENGINE_REQUESTENGINE_RESPONSE)const(/* HTTP Engine Type Values Starts at 1000 */HTTP_QUERY=ENGINE_PARAMETERSHTTP_PATH=ENGINE_PATHHTTP_BODY=iota+1000HTTP_FORM=iota+1000HTTP_MULTIPART_FORM=iota+1000HTTP_METHOD=iota+1000HTTP_REQUEST_URI=iota+1000HTTP_REMOTE_ADDR=iota+1000HTTP_HOST=iota+1000HTTP_SERVER_HEADER=iota+1000HTTP_STREAM_WRITER=iota+1000HTTP_WRITER=ENGINE_WRITER)

The minimum requirements for Revel to operate are the ENGINE constants, the HTTPconstants provide an enriched experience for the

GoDoc Reference
GitHub Labels

原文: https://revel.github.io/manual/server-engine.html