[Seaside] Serving files with WAComancheAdapter
johan at inceptive.be
Tue Oct 26 17:34:21 UTC 2010
There's no need to create subclasses of WAServerAdaptor for that. You just have to add a 'FileDirectory' entry-point for that.
In the /config application, add a new entry point and select 'File Directory' from the drop-down.
The rest of the configuration is pretty self-explanatory.
For example, if you add the '/css' entrypoint as a file directory that points to '/home/data/project/css', the files from the latter directory will be exposed in ./css url.
On 26 Oct 2010, at 19:16, Tony Fleig wrote:
> I have JQuery CSS files that reference a large number of image files from an images subdirectory. When these files are imported into a WAFileLibrary, the image filenames are changed and of course there are no subdirectories. Changing the original css file to accommodate this is not practical as I expect to receive new ones periodically and then would have to do it all over again.
> I am attracted by the easy deployment of Seaside apps when Apache is not involved and for the few files involved it doesn't seem to me that Apache is warranted, especially during development of the site.
> I could find no information on the right way to do this, so I subclassed WAServerAdaptor as shown below.
> With these modifications, If the URL received from the browser's path begins with /static, then files are served from a specified directory. Otherwise, the request is passed on (eventually) to WADispatcher as normal.
> This works for me, but I have no idea as to whether there is an easier or better way or if there is a feature to allow this that I just did not find.
> I am coming back to Smalltalk after many years and I am definitely a bit rusty, so any criticism of the code will also be greatly appreciated.
> 'From Pharo1.1 of 17 July 2010 [Latest update: #11411] on 26 October 2010 at 10:08:40 am'!
> WAComancheAdaptor subclass: #TFComancheAdaptor
> instanceVariableNames: 'staticDirectory staticPathPart'
> classVariableNames: ''
> poolDictionaries: ''
> category: 'TFStuff'!
> !TFComancheAdaptor methodsFor: 'as yet unclassified' stamp: 'TF 10/26/2010 08:58'!
> ^ FileDirectory default pathName! !
> !TFComancheAdaptor methodsFor: 'as yet unclassified' stamp: 'TF 10/26/2010 08:59'!
> ^ 'static'! !
> !TFComancheAdaptor methodsFor: 'as yet unclassified' stamp: 'TF 10/26/2010 09:55'!
> process: aNativeRequest
> "Serve static files from a specified directory if the URL path begins with a specified name.
> The default staticDirectory is the current directory (nominally the Seaside resources directory)
> and the default static path part is 'static'. Thus, if the client requests
> 'http://localhost:8080/static/css/xyz.css' and the Seaside resources directory is
> '/home/user1/Seaside.app/Contents/Resources', the file returned will be
> Requests whose path does not begin with the static path part are unaffected, i.e. they are
> processed by the WAComancheAdapter as normal."
> | context pathParts fullFilePath method response |
> pathParts := aNativeRequest pathParts.
> pathParts size > 1 ifTrue: [
> (pathParts at: 1) = self staticPathPart ifTrue: [
> method := aNativeRequest method.
> (#(#GET #POST) includes: method) ifFalse: [^nil].
> fullFilePath := self staticDirectory,aNativeRequest url.
> (FileStream isAFileNamed: fullFilePath) ifFalse: [^nil].
> fromStream: (StandardFileStream readOnlyFileNamed: fullFilePath).
> ] .
> ^ self processResponse: (super process: aNativeRequest)
> ! !
> !TFComancheAdaptor methodsFor: 'as yet unclassified' stamp: 'TF 10/26/2010 09:54'!
> processResponse: aResponse
> "Ensures that we get the HttpAdaptor's standard notFound error
> return, rather than the one line of text/plain that is returned by WADispatcher."
> aResponse status = #notFound
> ifTrue: [ ^nil ]
> ifFalse: [^aResponse]
> ! !
> !TFComancheAdaptor methodsFor: 'as yet unclassified' stamp: 'TF 10/26/2010 08:52'!
> staticDirectory isNil ifTrue: [ staticDirectory := self defaultStaticDirectory ].
> ^ staticDirectory! !
> !TFComancheAdaptor methodsFor: 'as yet unclassified' stamp: 'TF 10/26/2010 08:57'!
> staticPathPart isNil ifTrue: [ staticPathPart := self defaultStaticPathPart ].
> ^ staticPathPart! !
> seaside mailing list
> seaside at lists.squeakfoundation.org
More information about the seaside