mirror of
				https://github.com/gristlabs/grist-core.git
				synced 2025-06-13 20:53:59 +00:00 
			
		
		
		
	config: new API endpoint
This adds PATCH and GET endpoints to handle `config.json`.
This commit is contained in:
		
							parent
							
								
									d57c3f068d
								
							
						
					
					
						commit
						2d85ed1bfe
					
				
							
								
								
									
										35
									
								
								app/server/lib/ConfigBackendAPI.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								app/server/lib/ConfigBackendAPI.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
import * as express from 'express';
 | 
			
		||||
import {expressWrap} from 'app/server/lib/expressWrap';
 | 
			
		||||
 | 
			
		||||
import {getGlobalConfig} from 'app/server/lib/globalConfig';
 | 
			
		||||
 | 
			
		||||
import log from "app/server/lib/log";
 | 
			
		||||
 | 
			
		||||
export class ConfigBackendAPI {
 | 
			
		||||
  public addEndpoints(app: express.Express, requireInstallAdmin: express.RequestHandler) {
 | 
			
		||||
    app.get('/api/config/:key', requireInstallAdmin, expressWrap((req, resp) => {
 | 
			
		||||
      log.debug('config: requesting configuration', req.params);
 | 
			
		||||
 | 
			
		||||
      // Only one key is valid for now
 | 
			
		||||
      if (req.params.key === 'edition') {
 | 
			
		||||
        resp.send({value: getGlobalConfig().edition.get()});
 | 
			
		||||
      } else {
 | 
			
		||||
        resp.status(404).send({ error: 'Configuration key not found.' });
 | 
			
		||||
      }
 | 
			
		||||
    }));
 | 
			
		||||
 | 
			
		||||
    app.patch('/api/config', requireInstallAdmin, expressWrap(async (req, resp) => {
 | 
			
		||||
      const config = req.body.config;
 | 
			
		||||
      log.debug('config: received new configuration item', config);
 | 
			
		||||
 | 
			
		||||
      // Only one key is valid for now
 | 
			
		||||
      if(config.edition !== undefined) {
 | 
			
		||||
        await getGlobalConfig().edition.set(config.edition);
 | 
			
		||||
 | 
			
		||||
        resp.send({ msg: 'ok' });
 | 
			
		||||
      } else {
 | 
			
		||||
        resp.status(400).send({ error: 'Invalid configuration key' });
 | 
			
		||||
      }
 | 
			
		||||
    }));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -87,7 +87,8 @@ import {AddressInfo} from 'net';
 | 
			
		||||
import fetch from 'node-fetch';
 | 
			
		||||
import * as path from 'path';
 | 
			
		||||
import * as serveStatic from "serve-static";
 | 
			
		||||
import {IGristCoreConfig} from "./configCore";
 | 
			
		||||
import {ConfigBackendAPI} from "app/server/lib/ConfigBackendAPI";
 | 
			
		||||
import {IGristCoreConfig} from "app/server/lib/configCore";
 | 
			
		||||
 | 
			
		||||
// Health checks are a little noisy in the logs, so we don't show them all.
 | 
			
		||||
// We show the first N health checks:
 | 
			
		||||
@ -1948,6 +1949,14 @@ export class FlexServer implements GristServer {
 | 
			
		||||
    }));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public addConfigEndpoints() {
 | 
			
		||||
    // Need to be an admin to change the Grist config
 | 
			
		||||
    const requireInstallAdmin = this.getInstallAdmin().getMiddlewareRequireAdmin();
 | 
			
		||||
 | 
			
		||||
    const configBackendAPI = new ConfigBackendAPI();
 | 
			
		||||
    configBackendAPI.addEndpoints(this.app, requireInstallAdmin);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Get the HTML template sent for document pages.
 | 
			
		||||
  public async getDocTemplate(): Promise<DocTemplate> {
 | 
			
		||||
    const page = await fse.readFile(path.join(getAppPathTo(this.appRoot, 'static'),
 | 
			
		||||
 | 
			
		||||
@ -165,6 +165,7 @@ export async function main(port: number, serverTypes: ServerType[],
 | 
			
		||||
      server.addLogEndpoint();
 | 
			
		||||
      server.addGoogleAuthEndpoint();
 | 
			
		||||
      server.addInstallEndpoints();
 | 
			
		||||
      server.addConfigEndpoints();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (includeDocs) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user