2024-07-08 19:43:55 +02:00
#!/usr/bin/env node
2024-07-14 21:16:59 +02:00
const SpaccDotWebServer = require ( '../SpaccDotWeb.Server.js' ) ;
2024-02-24 20:26:34 +01:00
const server = SpaccDotWebServer . setup ( {
2024-02-23 01:38:08 +01:00
appName : 'Example' ,
// staticPrefix: '/static/',
2024-07-14 21:16:59 +02:00
// staticRoot: '', // not (yet) implemented
2024-02-23 01:38:08 +01:00
// staticFiles: [],
2024-07-14 21:16:59 +02:00
linkStyles : [ 'index.css' ] ,
// linkRuntimeScripts: [], // not (yet) implemented
linkClientScripts : [ 'particles.js' ] ,
2024-07-15 00:47:19 +02:00
// pageTitler: (title, opts={}, context) => `...`,
// appPager: (content, title, opts={}, context) => `...`,
// htmlPager: (content, title, opts={}, context) => `...`,
2024-02-23 01:38:08 +01:00
} ) ;
2024-07-14 21:16:59 +02:00
if ( SpaccDotWebServer . envIsNode && [ 'dump' , 'html' , 'writeStaticHtml' ] . includes ( process . argv [ 2 ] ) ) {
const fileName = server . writeStaticHtml ( Number ( process . argv [ 3 ] || 0 ) ) ;
2024-07-08 19:43:55 +02:00
console . log ( ` Dumped Static HTML to ' ${ fileName } '! ` ) ;
2024-02-23 01:38:08 +01:00
} else {
2024-07-08 19:43:55 +02:00
const serverData = server . initServer ( {
2024-02-23 01:38:08 +01:00
// defaultResponse: { code: 500, headers: {} },
// endpointsFalltrough: false,
// port: 3000,
2024-07-13 22:15:53 +02:00
address : '0.0.0.0' ,
2024-02-23 01:38:08 +01:00
// maxBodyUploadSize: null,
2024-07-12 18:43:09 +02:00
// handleHttpHead: true,
2024-02-23 01:38:08 +01:00
// appElement: 'div#app',
// transitionElement: 'div#transition',
2024-07-13 22:15:53 +02:00
// metaCookie: 'spaccdotweb-meta',
// cookieInUrl: 'spaccdotweb-cookie', // not (yet) implemented
2024-07-08 19:43:55 +02:00
// endpoints are defined by a discriminator and an action
2024-02-23 01:38:08 +01:00
endpoints : [
2024-07-08 19:43:55 +02:00
// a discriminator can be a simple boolean function
[ ( ctx ) => {
const now = ( new Date ) ;
return ( [ 'GET' , 'POST' ] . includes ( ctx . request . method ) && now . getHours ( ) === 0 && now . getMinutes ( ) === 0 ) ;
} , ( ctx ) => ctx . renderPage ( ` <p>We're sorry but, to avoid disturbing the spirits, Testing is not available at 00:00. Please retry in just a minute.</p> ` , 'Error' ) ] ,
// or, a discriminator can be a specially-constructed filter string
[ 'GET|POST /main/' , async ( ctx ) => {
//[ (ctx) => (['GET', 'POST'].includes(ctx.request.method) && ctx.urlSections[0] === 'main'), (ctx) => {
2024-02-23 01:38:08 +01:00
if ( ctx . request . method === 'POST' ) {
if ( ctx . bodyParameters ? . add ) {
ctx . setCookie ( ` count= ${ parseInt ( ctx . getCookie ( 'count' ) || 0 ) + 1 } ` ) ;
} else if ( ctx . bodyParameters ? . reset ) {
ctx . setCookie ( ` count= ` ) ;
2024-07-08 19:43:55 +02:00
}
// a short sleep so that we can test client transitions
2024-07-13 22:15:53 +02:00
await ( new Promise ( resolve => setTimeout ( resolve , 1500 ) ) ) ;
2024-07-08 19:43:55 +02:00
}
2024-02-23 01:38:08 +01:00
const content = `
< h2 > Test < / h 2 >
< p > This page was rendered at $ { Date ( ) } . < / p >
< p > These were your cookies at time of request : < / p >
2024-02-24 20:26:34 +01:00
< pre > $ { ctx . getCookie ( ) || '[None]' } < / p r e >
2024-02-23 01:38:08 +01:00
< form method = "POST" >
< input type = "submit" name = "add" value = "Add 1 to cookie" / >
< input type = "submit" name = "reset" value = "Reset cookies" / >
< / f o r m >
2024-07-08 19:43:55 +02:00
< p > Context data for this request : < / p >
< pre > $ { JSON . stringify ( {
request : {
method : ctx . request . method ,
} ,
urlSections : ctx . urlSections ,
urlParameters : ctx . urlParameters ,
bodyParameters : ctx . bodyParameters ,
} , null , 2 ) } < / p r e >
2024-02-23 01:38:08 +01:00
` ;
2024-07-08 19:43:55 +02:00
// the main content of a page can be rendered with the main template using:
2024-02-24 20:26:34 +01:00
ctx . renderPage ( content , 'Test' ) ;
2024-02-23 01:38:08 +01:00
} ] ,
2024-07-08 19:43:55 +02:00
// redirects are easy
[ 'GET' , ( ctx ) => {
ctx . redirectTo ( '/main/' ) ;
// alternatively: return { code: 302, headers: { location: '/main/' } };
} ] ,
2024-02-23 01:38:08 +01:00
] ,
} ) ;
2024-07-08 19:43:55 +02:00
if ( SpaccDotWebServer . envIsNode ) {
console . log ( ` Running Server on < ${ serverData . address } : ${ serverData . port } >... ` ) ;
}
2024-02-23 01:38:08 +01:00
} ;