Converted project to Rust
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "rest-cobol"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_yaml = "0.9"
|
||||
serde_json = "1.0"
|
||||
tower-http = { version = "0.5", features = ["fs"] }
|
||||
libloading = "0.8"
|
||||
@@ -1,5 +0,0 @@
|
||||
module rest-cobol
|
||||
|
||||
go 1.26.4
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1
|
||||
@@ -1,4 +0,0 @@
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -1,74 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ApiConfig struct {
|
||||
Version string `yaml:"version"`
|
||||
Project string `yaml:"project"`
|
||||
Endpoints []Endpoint `yaml:"endpoints"`
|
||||
}
|
||||
|
||||
type Endpoint struct {
|
||||
Path string `yaml:"path"`
|
||||
Method string `yaml:"method"`
|
||||
CobolSource string `yaml:"cobol_source"`
|
||||
Procedure string `yaml:"procedure"`
|
||||
Arguments []Argument `yaml:"arguments"`
|
||||
ResponseCopybook string `yaml:"response_copybook"`
|
||||
}
|
||||
|
||||
type Argument struct {
|
||||
Name string `yaml:"name"`
|
||||
Target string `yaml:"target"`
|
||||
Type string `yaml:"type"`
|
||||
Source string `yaml:"source"`
|
||||
}
|
||||
|
||||
func readConf(filename string) (*ApiConfig, error) {
|
||||
buf, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &ApiConfig{}
|
||||
err = yaml.Unmarshal(buf, c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("in file %q: %w", filename, err)
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func projectHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
api, err := readConf("api.yaml")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
response := map[string]string{"project": api.Project}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/project", projectHandler)
|
||||
|
||||
port := ":8080"
|
||||
fmt.Printf("Server starting on port %s...\n", port)
|
||||
if err := http.ListenAndServe(port, nil); err != nil {
|
||||
log.Fatalf("Server failed to start: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
use axum::{
|
||||
routing::get,
|
||||
Json, Router,
|
||||
http::StatusCode,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct ApiConfig {
|
||||
version: String,
|
||||
project: String,
|
||||
endpoints: Vec<Endpoint>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Endpoint {
|
||||
path: String,
|
||||
method: String,
|
||||
cobol_source: String,
|
||||
procedure: String,
|
||||
arguments: Vec<Argument>,
|
||||
response_copybook: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Argument {
|
||||
name: String,
|
||||
target: String,
|
||||
#[serde(rename = "type")]
|
||||
arg_type: String,
|
||||
source: String,
|
||||
}
|
||||
|
||||
#[repr(C, packed)]
|
||||
struct Student {
|
||||
st_id: [u8; 5],
|
||||
first_name: [u8; 20],
|
||||
last_name: [u8; 20],
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
eprintln!("Starting RestCOBOL Rust server...");
|
||||
let app = Router::new()
|
||||
.route("/project", get(project_handler));
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
|
||||
eprintln!("Binding to {}...", addr);
|
||||
let listener = match tokio::net::TcpListener::bind(addr).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to bind to {}: {}", addr, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
eprintln!("Server starting on port 8080...");
|
||||
if let Err(e) = axum::serve(listener, app).await {
|
||||
eprintln!("Server error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async fn project_handler() -> Result<Json<serde_json::Value>, (StatusCode, String)> {
|
||||
let config = read_conf("api.yaml").map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"project": config.project
|
||||
})))
|
||||
}
|
||||
|
||||
fn read_conf(filename: &str) -> Result<ApiConfig, Box<dyn std::error::Error>> {
|
||||
let mut file = File::open(filename)?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
let config: ApiConfig = serde_yaml::from_str(&contents)?;
|
||||
Ok(config)
|
||||
}
|
||||
Reference in New Issue
Block a user