Browse Source

Refactor.

txlyre 10 months ago
parent
commit
11f93c9919
7 changed files with 64 additions and 22 deletions
  1. 4 0
      .gitignore
  2. 13 10
      app.py
  3. 28 0
      counter.c/Recipe
  4. 2 7
      counter.c/counter.c
  5. 3 1
      counter.c/counter.h
  6. 10 4
      counter.c/main.c
  7. 4 0
      requirements.txt

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+__pycache__
+config.hjson
+counter
+counter.c/*.o

+ 13 - 10
app.py

@@ -1,8 +1,8 @@
+import struct
 import asyncio
 from typing import Annotated
 
 import redis.asyncio as aioredis
-from aiofiles.tempfile import NamedTemporaryFile
 from fastapi import FastAPI, Path
 from fastapi.responses import Response
 
@@ -40,17 +40,20 @@ async def get_counter(
 
   await redis.set(name, str(number))
 
-  async with NamedTemporaryFile('rb') as f:
-    proc = await asyncio.create_subprocess_shell(
-      f'{config.CounterPath} {number} {f.name}'
-    )
+  proc = await asyncio.create_subprocess_exec(
+    config.CounterPath,
+    stdin=asyncio.subprocess.PIPE,
+    stdout=asyncio.subprocess.PIPE
+  )
 
-    await proc.communicate()
+  stdout, _ = await proc.communicate(
+    input=struct.pack('L', number)
+  )
 
-    return Response(
-      content=await f.read(),
-      media_type='image/png'
-    )
+  return Response(
+    content=stdout,
+    media_type='image/png'
+  )
 
 @app.on_event('startup')
 async def startup_event():

+ 28 - 0
counter.c/Recipe

@@ -0,0 +1,28 @@
+<recipe>
+  <script src="C">
+    define("OUTPUT", "../counter");
+    define("CC", "clang");
+  </script>
+
+  <rule name="debug">
+    flags("-ggdb");
+  </rule>
+
+  <rule name="release">
+    flags("-Ofast");
+  </rule>
+
+  <rule name="rebuild" requires="clear" then="build"/>
+
+  <rule name="build" then="link">
+    sources(glob("*.c"));
+    flags("-w");
+    pkg_config("libpng");
+  </rule>
+
+  <rule name="clear">
+    `rm -rf *.o $_OUT`;
+  </rule>
+
+  <rule name="entry" requires="release,build"/>
+</recipe>

+ 2 - 7
counter.c/counter.c

@@ -1,5 +1,6 @@
 #include <math.h>
 #include <stdlib.h>
+#include <string.h>
 #include <png.h>
 #include "counter.h"
 #include "powers_table.h"
@@ -16,7 +17,7 @@ static int nth_digit(unsigned long number, int n) {
   return (number / powers_table[n][0]) % 10;
 }
 
-void counter_create(unsigned long number, char *filename) {
+void counter_create(unsigned long number, FILE *fd) {
   png_structp png;
   png_infop info;
 
@@ -27,8 +28,6 @@ void counter_create(unsigned long number, char *filename) {
 
   png_byte **rows;
 
-  FILE *fd;
-
   png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   info = png_create_info_struct(png);
 
@@ -47,8 +46,6 @@ void counter_create(unsigned long number, char *filename) {
       memcpy(rows[y] + (i * DIGIT_WIDTH * 4), digits_data[digits[i]][y], DIGIT_WIDTH * 4);
   }
 
-  fd = fopen(filename, "wb");
-
   png_init_io(png, fd);
   png_set_IHDR(
     png,
@@ -66,8 +63,6 @@ void counter_create(unsigned long number, char *filename) {
   png_write_end(png, info);
   png_destroy_write_struct(&png, &info);
 
-  fclose(fd);
-
   for (int y = 0; y < DIGIT_HEIGHT; y++)
     free(rows[y]);
 

+ 3 - 1
counter.c/counter.h

@@ -1,7 +1,9 @@
 #ifndef _COUNTER_H
 #define _COUNTER_H
 
+#include <stdlib.h>
+
 void counter_release(void);
-void counter_create(unsigned long number, char *filename);
+void counter_create(unsigned long, FILE *);
 
 #endif

+ 10 - 4
counter.c/main.c

@@ -1,12 +1,18 @@
 #include <stdlib.h>
-
 #include "counter.h"
 
+void read_ulong(uint_t *r) {
+  *r = 0;
+
+  for (unsigned long i = 0; i < sizeof(unsigned long); i++)
+    *r |= ((unsigned long)fgetc(stdin)) << (i*8);
+}
+
 int main(int argc, char **argv) {
-  if (argc != 3)
-    return 1;
+  unsigned long number;
 
-  counter_create(strtoul(argv[1], NULL, 10), argv[2]);
+  read_ulong(&number);
+  counter_create(number, stdout);
 
   return 0;
 }

+ 4 - 0
requirements.txt

@@ -0,0 +1,4 @@
+redis[hiredis]
+fastapi
+uvicorn[standard]
+hjson