Redoing program to center around JSONRPC instead of a more REST-like API

This commit is contained in:
Ben 2025-07-28 08:10:06 -07:00
parent ed4bbeb608
commit be8936a665
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
15 changed files with 209 additions and 170 deletions

69
web/listen.go Normal file
View file

@ -0,0 +1,69 @@
package web
/* This file handles listening to HTTP requests */
import (
//"signal-cli-http/auth"
"signal-cli-http/subprocess"
"fmt"
"io"
"log"
"net/http"
)
func StartWebserver(port int) {
http.HandleFunc("/", getRoot)
err := http.ListenAndServe(":"+fmt.Sprint(port), nil)
fmt.Println(err)
}
func getRoot(w http.ResponseWriter, r *http.Request) {
// Check that Authentication header exists
authArr, ok := r.Header["Authentication"]
if (!ok) || (len(authArr) == 0) {
w.WriteHeader(400);
w.Write([]byte("Authentication header missing\n"))
return;
}
bearer := authArr[0];
// Check that the request is allowed for the path
/*if !conf.GlobalConfig.ValidateBearerKey(bearer, r.URL.Path) {
w.WriteHeader(403);
w.Write([]byte("Bearer key not whitelisted for this path\n"))
return;
}*/
// Read request body
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500);
w.Write([]byte("Error reading body\n"))
return;
}
// Call subprocess
status, bodyContent, err := subprocess.Run(r.URL.Path, body)
// Error
if err != nil {
w.WriteHeader(500);
w.Write([]byte("Internal server error: " + err.Error() + "\n"));
return
}
// Respond to client with status
if status == 0 {
w.WriteHeader(200);
w.Write(bodyContent);
} else {
w.WriteHeader(400);
w.Write([]byte("Program exited with status " + fmt.Sprint(status)));
}
// Log the request
log.Default().Print("HTTP Request: ", bearer, " " , r.URL.Path, " ", status)
}

17
web/readme.md Normal file
View file

@ -0,0 +1,17 @@
# Web - Signal-CLI HTTP
This module handles the HTTP requests. The path used for requests genuinely does not matter. However, every request must have a JSON object body, and an `Authorization: bearer <token>` header. The response will also always be a JSON.
Possible response codes:
* 400 Bad Request: Bad JSON, missing Authorization header, etc.
* 401 Unauthorized: bearer token not allowed for presented request.
* 500 Internal Server Error: Self explanitory.
* 501 Not Implemented: Will be returned for message receiving.
* 200 OK: Request was forwarded to JSONRPC subprocess and that process returned something, including if the "error" key is present in the returned JSON.
Any non-200 response code will come with a JSON with the following format:
```json
{"error":"Error message string"}
```