# Formies

Formies is a form management tool that allows you to create customizable forms, collect submissions, and view collected data. This project combines a Rust backend and a Svelte frontend, packaged as a single Docker container for easy deployment.

## Features

### 📝 Form Management

- Create forms with customizable fields (text, number, date, etc.).
- View all created forms in a centralized dashboard.

### 🗂️ Submissions

- Submit responses to forms via a user-friendly interface.
- View and manage all form submissions.

### ⚙️ Backend

- Built with Rust using Actix-Web for high performance and scalability.
- Uses SQLite for local data storage with easy migration to PostgreSQL if needed.

### 🎨 Frontend

- Built with SvelteKit for a modern and lightweight user experience.
- Responsive design for use across devices.

### 🚀 Deployment

- Packaged as a single Docker image for seamless deployment.
- Supports CI/CD workflows with Gitea Actions, Drone CI, or GitHub Actions.

## Folder Structure

```
project-root/
├── backend/               # Backend codebase
│   ├── src/
│   │   ├── handlers.rs    # Route handlers for Actix-Web
│   │   ├── models.rs      # Data models (Form, Submission)
│   │   ├── db.rs          # Database initialization
│   │   ├── main.rs        # Main entry point for the backend
│   │   └── ...            # Additional modules
│   ├── Cargo.toml         # Backend dependencies
│   └── Cargo.lock         # Locked dependencies
│
├── frontend/              # Frontend codebase
│   ├── public/            # Built static files (after `npm run build`)
│   ├── src/
│   │   ├── lib/           # Shared utilities (e.g., API integration)
│   │   ├── routes/        # Svelte pages
│   │   │   ├── +page.svelte   # Dashboard
│   │   │   └── form/          # Form-related pages
│   │   └── main.ts        # Frontend entry point
│   ├── package.json       # Frontend dependencies
│   ├── svelte.config.js   # Svelte configuration
│   └── ...                # Additional files
│
├── Dockerfile             # Combined Dockerfile for both frontend and backend
├── docker-compose.yml     # Docker Compose file for deployment
├── .gitea/                # Gitea Actions workflows
│   └── workflows/
│       └── build_and_deploy.yml
├── .drone.yml             # Drone CI configuration
├── README.md              # Documentation (this file)
└── ...                    # Other configuration files
```

## Prerequisites

### Docker

- Install Docker: [Docker Documentation](https://docs.docker.com/)

### Rust (for development)

- Install Rust: [Rustup Installation](https://rustup.rs/)

### Node.js (for frontend development)

- Install Node.js: [Node.js Downloads](https://nodejs.org/)

## Development

### Backend

1. Navigate to the backend/ directory:

   ```sh
   cd backend
   ```

2. Run the backend server:

   ```sh
   cargo run
   ```

   The backend will be available at [http://localhost:8080](http://localhost:8080).

### Frontend

1. Navigate to the frontend/ directory:

   ```sh
   cd frontend
   ```

2. Install dependencies:

   ```sh
   npm install
   ```

3. Start the development server:

   ```sh
   npm run dev
   ```

   The frontend will be available at [http://localhost:5173](http://localhost:5173).

## Deployment

### Build the Docker Image

1. Build the combined Docker image:

   ```sh
   docker build -t your-dockerhub-username/formies-combined .
   ```

2. Run the Docker container:

   ```sh
   docker run -p 8080:8080 your-dockerhub-username/formies-combined
   ```

   Access the application at [http://localhost:8080](http://localhost:8080).

### Using Docker Compose

1. Deploy using `docker-compose.yml`:

   ```sh
   docker-compose up -d
   ```

2. Stop the containers:
   ```sh
   docker-compose down
   ```

## CI/CD Workflow

### Gitea Actions

1. Create a file at `.gitea/workflows/build_and_deploy.yml`:

   ```yaml
   name: Build and Deploy

   on:
     push:
       branches:
         - main

   jobs:
     build:
       runs-on: ubuntu-latest
       steps:
         - name: Clone the repository
           uses: actions/checkout@v3

         - name: Set up Docker
           uses: docker/setup-buildx-action@v2

         - name: Log in to Docker Hub
           run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

         - name: Build and Push Docker Image
           run: |
             docker build -t your-dockerhub-username/formies-combined .
             docker push your-dockerhub-username/formies-combined:latest

         - name: Deploy to Server (optional)
           run: |
             ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
             docker pull your-dockerhub-username/formies-combined:latest
             docker stop formies || true
             docker rm formies || true
             docker run -d --name formies -p 8080:8080 your-dockerhub-username/formies-combined:latest
             EOF
   ```

2. Add secrets in Gitea:
   - `DOCKER_USERNAME`: Your Docker Hub username.
   - `DOCKER_PASSWORD`: Your Docker Hub password.
   - `SERVER_USER`: SSH username for deployment.
   - `SERVER_IP`: IP address of the server.

## API Endpoints

**Base URL:** `http://localhost:8080`

| Method | Endpoint                      | Description                         |
| ------ | ----------------------------- | ----------------------------------- |
| POST   | `/api/forms`                  | Create a new form                   |
| GET    | `/api/forms`                  | Get all forms                       |
| POST   | `/api/forms/{id}/submissions` | Submit data to a form               |
| GET    | `/api/forms/{id}/submissions` | Get submissions for a specific form |

## Future Enhancements

- **Authentication:** Add user-based authentication for managing forms and submissions.
- **Export:** Allow exporting submissions to CSV or Excel.
- **Scaling:** Migrate to PostgreSQL for distributed data handling.
- **Monitoring:** Integrate tools like Prometheus and Grafana for performance monitoring.

## License

This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.