firmware: Add initial web server
This commit is contained in:
		@@ -10,18 +10,39 @@ let
 | 
				
			|||||||
    systemd = config.systemd.package;
 | 
					    systemd = config.systemd.package;
 | 
				
			||||||
    iwd = config.networking.wireless.iwd.package;
 | 
					    iwd = config.networking.wireless.iwd.package;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  app = pkgs.substituteAll {
 | 
				
			||||||
 | 
					    src = ./app.py;
 | 
				
			||||||
 | 
					    isExecutable = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    python = pkgs.python3.withPackages (ps: with ps; [ pyyaml aiohttp ]);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
in
 | 
					in
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  config = {
 | 
					  config = {
 | 
				
			||||||
    systemd = {
 | 
					    systemd = {
 | 
				
			||||||
      services = {
 | 
					      services = {
 | 
				
			||||||
        qclk-configurer = {
 | 
					        qclk-configurer = {
 | 
				
			||||||
          description = "qclk dynamic configurer";
 | 
					          description = "qCLK dynamic configurer";
 | 
				
			||||||
          after = [ "network.target" ];
 | 
					          after = [ "network.target" ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          serviceConfig = {
 | 
					          serviceConfig = {
 | 
				
			||||||
            Type = "notify-reload";
 | 
					            Type = "notify-reload";
 | 
				
			||||||
            ExecStart = "${configurer} serve";
 | 
					            ExecStart = "${configurer} serve";
 | 
				
			||||||
 | 
					            Restart = "on-failure";
 | 
				
			||||||
 | 
					          };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          wantedBy = [ "multi-user.target" ];
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        qclk-app = {
 | 
				
			||||||
 | 
					          description = "qCLK app";
 | 
				
			||||||
 | 
					          after = [ "qclk-configurer.service" ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          serviceConfig = {
 | 
				
			||||||
 | 
					            Type = "simple";
 | 
				
			||||||
 | 
					            ExecStart = "${app}";
 | 
				
			||||||
 | 
					            Restart = "on-failure";
 | 
				
			||||||
          };
 | 
					          };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          wantedBy = [ "multi-user.target" ];
 | 
					          wantedBy = [ "multi-user.target" ];
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										29
									
								
								firmware/app.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										29
									
								
								firmware/app.py
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					#! @python@/bin/python -B
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from aiohttp import web
 | 
				
			||||||
 | 
					import yaml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CONF_FILE = os.getenv('CONFIG', '/etc/qclk/config.yaml')
 | 
				
			||||||
 | 
					conf_k = web.AppKey('config', dict)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def log(m):
 | 
				
			||||||
 | 
					  print(m, file=sys.stderr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					routes = web.RouteTableDef()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@routes.get('/')
 | 
				
			||||||
 | 
					async def index(req):
 | 
				
			||||||
 | 
					  return web.Response(text='Hello, world!')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def main():
 | 
				
			||||||
 | 
					  app = web.Application()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  with open(CONF_FILE) as f:
 | 
				
			||||||
 | 
					    app[conf_k] = yaml.safe_load(f)
 | 
				
			||||||
 | 
					  app.add_routes(routes)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  web.run_app(app, port=8080)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					  main()
 | 
				
			||||||
@@ -63,7 +63,7 @@ class Configurer:
 | 
				
			|||||||
      self.config = yaml.safe_load(f)
 | 
					      self.config = yaml.safe_load(f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def write_config(self):
 | 
					  def write_config(self):
 | 
				
			||||||
    log(f'Updaing config')
 | 
					    log(f'Updating config')
 | 
				
			||||||
    tmp = os.path.join(self.tmpdir, 'new-config.yaml')
 | 
					    tmp = os.path.join(self.tmpdir, 'new-config.yaml')
 | 
				
			||||||
    with open(tmp, 'w') as f:
 | 
					    with open(tmp, 'w') as f:
 | 
				
			||||||
      yaml.dump(self.config, f)
 | 
					      yaml.dump(self.config, f)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,6 +40,8 @@ in
 | 
				
			|||||||
  perSystem = { lib, libMy, pkgs, ... }:
 | 
					  perSystem = { lib, libMy, pkgs, ... }:
 | 
				
			||||||
  let
 | 
					  let
 | 
				
			||||||
    inherit (lib) concatMapStringsSep;
 | 
					    inherit (lib) concatMapStringsSep;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    devPython = pkgs.python3.withPackages (ps: with ps; [ pyyaml aiohttp ]);
 | 
				
			||||||
  in
 | 
					  in
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    devenv.shells.firmware = libMy.withRootdir {
 | 
					    devenv.shells.firmware = libMy.withRootdir {
 | 
				
			||||||
@@ -47,6 +49,7 @@ in
 | 
				
			|||||||
        nixos-rebuild
 | 
					        nixos-rebuild
 | 
				
			||||||
        nixVersions.latest
 | 
					        nixVersions.latest
 | 
				
			||||||
        wireguard-tools
 | 
					        wireguard-tools
 | 
				
			||||||
 | 
					        devPython
 | 
				
			||||||
      ];
 | 
					      ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      scripts =
 | 
					      scripts =
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,11 @@
 | 
				
			|||||||
    hostName = config.system.name;
 | 
					    hostName = config.system.name;
 | 
				
			||||||
    useDHCP = false;
 | 
					    useDHCP = false;
 | 
				
			||||||
    useNetworkd = true;
 | 
					    useNetworkd = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    firewall = {
 | 
				
			||||||
 | 
					      interfaces.management.allowedTCPPorts = [ 8080 ];
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    wireless.iwd = {
 | 
					    wireless.iwd = {
 | 
				
			||||||
      enable = true;
 | 
					      enable = true;
 | 
				
			||||||
      settings = {
 | 
					      settings = {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user