-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
156 lines (120 loc) · 4.07 KB
/
Copy pathmain.c
File metadata and controls
156 lines (120 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <http/http.h>
#include <http/route.h>
#include <stdlib.h>
#include <stdio.h>
conn_ctx_t *conn_pool = NULL;
int conn_pool_next = 0;
pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER;
static inline const char *
req_get_param(http_req_t *req, const char *key, int *val_len) {
int klen = strlen(key);
for (int i = 0; i < req->param_count; i++) {
if (req->params[i].key_len == klen &&
memcmp(req->params[i].key, key, klen) == 0) {
if (val_len) *val_len = req->params[i].val_len;
return req->params[i].val;
}
}
return NULL;
}
static inline float read_cpu_temp(void) {
FILE *f = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if (!f) return -1.0f;
int millideg;
fscanf(f, "%d", &millideg);
fclose(f);
return millideg / 1000.0f;
}
#define TEMP_BUF_SIZE 64
static float temp_buf[TEMP_BUF_SIZE];
static int temp_idx = 0;
static int temp_count = 0;
static pthread_mutex_t temp_lock = PTHREAD_MUTEX_INITIALIZER;
static inline void record_temp(float t) {
pthread_mutex_lock(&temp_lock);
temp_buf[temp_idx] = t;
temp_idx = (temp_idx + 1) % TEMP_BUF_SIZE;
if (temp_count < TEMP_BUF_SIZE) temp_count++;
pthread_mutex_unlock(&temp_lock);
}
static inline float avg_temp(void) {
float sum = 0.0f;
pthread_mutex_lock(&temp_lock);
for (int i = 0; i < temp_count; i++)
sum += temp_buf[i];
pthread_mutex_unlock(&temp_lock);
return temp_count ? sum / temp_count : 0.0f;
}
void cpu_temp_handler(http_req_t *req, http_resp_t *res) {
static __thread char buf[512];
float cur = read_cpu_temp();
if (cur < 0) {
res->status = 500;
res->body_ptr = "temp read error";
res->body_len = 15;
return;
}
record_temp(cur);
int avg_len = 0, last_len = 0;
const char *avg_q = req_get_param(req, "avg", &avg_len);
const char *last_q = req_get_param(req, "last", &last_len);
int off = 0;
off += snprintf(buf + off, sizeof(buf) - off, "{");
int need_comma = 0;
if (avg_q) {
off += snprintf(buf + off, sizeof(buf) - off,
"\"avg\":%.2f", avg_temp());
need_comma = 1;
}
if (last_q) {
int n = 0;
for (int i = 0; i < last_len; i++)
if (last_q[i] >= '0' && last_q[i] <= '9')
n = n * 10 + (last_q[i] - '0');
if (n > TEMP_BUF_SIZE) n = TEMP_BUF_SIZE;
if (need_comma)
off += snprintf(buf + off, sizeof(buf) - off, ",");
off += snprintf(buf + off, sizeof(buf) - off, "\"last\":[");
pthread_mutex_lock(&temp_lock);
for (int i = 0; i < n && i < temp_count; i++) {
int idx = (temp_idx - 1 - i + TEMP_BUF_SIZE) % TEMP_BUF_SIZE;
off += snprintf(buf + off, sizeof(buf) - off,
"%.2f%s",
temp_buf[idx],
(i + 1 < n && i + 1 < temp_count) ? "," : "");
}
pthread_mutex_unlock(&temp_lock);
off += snprintf(buf + off, sizeof(buf) - off, "]");
need_comma = 1;
}
if (!avg_q && !last_q) {
off += snprintf(buf + off, sizeof(buf) - off,
"\"temp\":%.2f", cur);
}
off += snprintf(buf + off, sizeof(buf) - off, "}");
res->status = 200;
res->is_static = 1;
res->body_ptr = buf;
res->body_len = off;
}
int main(){
http *App = CreateServer();
add_route("GET","/echo", handle_echo);
add_route("GET","/ping", handle_ping);
add_route("GET", "/api/v1/cpu/temp", cpu_temp_handler);
// add_route("GET","/hi", hi_router);
if (!App) {
fprintf(stderr, "Failed to create server\n");
return 1;
}
printf("✅ Server running! Press Ctrl+C to stop.\n\n");
fflush(stdout);
// Keep main thread alive
for(;;) sleep(1);
return HTTP_STATS;
}
/*
: 1765966243:0;gcc -O3 -I include -march=native -flto -pthread -D_GNU_SOURCE main.c ./src/*.c -o http
: 1765966245:0;./http
grim -o "$(hyprctl monitors | awk '/Monitor/{mon=$2} /focused: yes/{print mon}')" screenshot.png
*/