Skip to content

About

New backend written in go with gRPC as an API interface

Resources

Stars

18 stars

Watchers

3 watching

Forks

Repository files navigation

🎓 Campus-Backend

The API behind the TUM Campus Apps. One Go binary serves gRPC and REST, and also scrapes the campus for data.

CI Lint License: GPL v3 API

Try the API · Proto definition · Quick start · Contributing


Every time a student at the Technical University of Munich opens the TUM Campus App to check today's menu, rate a dish, find out how crowded the Mensa is, or see what TU Film is showing this week, the request ends up here.

The API is public. If you want to build something for campus, you can use it too. Please use it responsibly.

✨ What it does

Feature Where the data comes from
🍝 Canteens and dishes: menus, opening info, ratings with tags eat-api + ratings from app users
👥 Live canteen head count: how full is the Mensa right now? Estimated from LRZ Wi-Fi access point stats
📰 News and alerts from TUM sources Scraped on a schedule by cron jobs
🎬 Movies from TU Film, with posters and metadata tu-film.de + OMDb
🏛️ Student clubs directory Scraped from the student council site
💬 Feedback with image uploads (gRPC streaming), sent out by email App users
📱 Update notes and device registration App users

🏗️ How it works

 Campus apps ──gRPC──┐
                     ▼
               ┌── :50051 (cmux) ──┐
               │                   │
         application/grpc        HTTP/1
               │                   ├── /v1/*      → grpc-gateway ─┐
               ▼                   ├── /swagger/  → Swagger UI    │
          gRPC server ◀────────────┼───────────────────────────────┘
               │                   ├── /metrics   → Prometheus
               ▼                   ├── /files/    → /Storage
         CampusServer              └── /health
               │
               ▼
             MySQL ◀── CronService (scrapers, polls `crontab` every 30 s)

In production, REST is served at api.tum.app and gRPC at api-grpc.tum.app:443.

  • One proto, two protocols. campus_backend.proto is the single source of truth. gRPC stubs, the REST gateway and the OpenAPI spec are all generated from it.
  • One port. cmux looks at each connection and sends it to gRPC or HTTP.
  • Built-in cron. Scrapers and downloaders run inside the same binary, scheduled from the crontab table.
  • Observability. Prometheus metrics, Sentry error reporting and structured logs via logrus.

🚀 Quick start

You need Docker. That's it.

git clone https://github.com/TUM-Dev/Campus-Backend.git
cd Campus-Backend
docker compose -f docker-compose.local.yml up --watch

This starts MySQL and the backend at localhost:50051. With --watch, the server is rebuilt and restarted every time you save a file in server/. Changes to tests, test data and Markdown files are ignored. Use up -d instead if you just want it running in the background.

Now try it:

# REST
curl localhost:50051/v1/canteen/allCanteens
curl localhost:50051/v1/news/sources

# gRPC (server reflection is enabled)
grpcurl -plaintext localhost:50051 list
grpcurl -plaintext localhost:50051 api.Campus/ListCanteens
URL What you get
localhost:50051/v1/… REST API
localhost:50051/swagger/ Swagger UI (see note)
localhost:50051/metrics Prometheus metrics
localhost:50051/health Health check
localhost:3306 MySQL (data persists in the mysql-data volume)

Running without Docker

cd server
DB_DSN='root:secret_root_password@tcp(localhost:3306)/campus_db?charset=utf8mb4&parseTime=True&loc=Local' go run .

🛠️ Development

All Go commands run from server/.

go build ./...
go test ./...            # spins up a MySQL testcontainer, so Docker is required
bash api/generate.bash   # regenerate gRPC, gateway and Swagger code from the proto

Tests that need a database start one MySQL container per test binary via testcontainers and give each test its own database with the production schema. To reuse a MySQL server that's already running:

TEST_MYSQL_DSN='root:secret_root_password@tcp(localhost:3306)/' go test ./...

VS Code users get debug configs for free in .vscode/launch.json.

🗺️ Repo map

server/
├── main.go                       # entry point: cmux, gRPC, gateway, HTTP routes
├── api/tumdev/                   # campus_backend.proto + generated code
├── backend/                      # one file per domain: cafeteria.go, news.go, movie.go, …
│   ├── cron/                     # scheduled scrapers and downloaders
│   └── migration/                # schema migrations (legacy Go + Atlas-generated sql/)
├── model/                        # GORM models, one per table
└── utils/                        # DB setup, test containers, telemetry, error mapping
client/                           # example gRPC clients (separate Go module)

Recipes

➕ Add a new endpoint
  1. Add an rpc with a google.api.http option, plus its request and reply messages, to campus_backend.proto.
  2. Run bash api/generate.bash and commit the generated files.
  3. Implement the method on *CampusServer in server/backend/<domain>.go. Start it with s.checkDevice(ctx) and return status.Error(codes.X, …) errors.

⚠️ Released apps depend on this API. Never renumber or reuse proto field numbers. A proto change on main automatically opens a PR in Campus-Flutter.

🗄️ Change the database schema

Migrations are generated with Atlas from the GORM models and applied by the server on startup.

  1. Edit the model in server/model/. If it's a new model, add it to migration.Models().
  2. In server/, run atlas migrate diff --env gorm <short_name> (install Atlas; Docker is required). It writes backend/migration/sql/<version>_<short_name>.sql and updates atlas.sum.
  3. Review the generated SQL and commit it together with atlas.sum.

For data changes, or for views and triggers (which Atlas' free edition doesn't manage), edit the generated file or create one with atlas migrate new --env gorm <short_name>, then run atlas migrate hash --env gorm.

The desired schema is the models plus legacy.sql, which declares production tables without a model. The Go migrations in backend/migration/ are frozen, including the post-baseline registry; a regression test rejects new Go migration IDs. Keep existing IDs for compatibility, but never duplicate an Atlas schema change in Go. CI fails, and comments the missing SQL on your PR, if the models change without a migration.

⏰ Add a cron job
  1. Add a type constant in backend/cron/cronjobs.go and wire it into the query and the switch.
  2. Add the value to the type enum in model/crontab.go.
  3. Generate the migration with atlas migrate diff --env gorm <short_name>, append the INSERT for the crontab row, then run atlas migrate hash --env gorm.

⚙️ Configuration

Variable Required Purpose
DB_DSN ✅ MySQL DSN. Docker Compose builds it from DB_NAME, DB_USER_NAME, DB_USER_PASSWORD and DB_PORT in .env.
ENVIRONMENT dev or prod. Changes telemetry behavior.
SENTRY_DSN Error reporting via Sentry
OMDB_API_KEY Movie metadata from OMDb
SMTP_URL, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD, SMTP_FROM Sending feedback emails

The committed .env contains only local dev defaults. Put real secrets in .env.local, which is gitignored.

🤝 Contributing

Contributions are very welcome, whether that's a bug fix, a new scraper or a whole new feature for the apps.

  1. Install pre-commit so formatting, goimports, proto regeneration and go mod tidy run automatically:
    pip install pre-commit && pre-commit install
    pre-commit run -a   # optional: check every file once
  2. Make your change, add tests, and open a PR. CI runs the tests, golangci-lint and the migration checks.

Working with an AI coding agent? AGENTS.md has the full conventions.

🪤 Gotchas

  • /swagger/ locally has no spec until you copy server/api/tumdev/campus_backend.swagger.json to server/swagger/swagger.json. The Dockerfile does this for you.
  • docker-compose.yml is the production deployment. Use docker-compose.local.yml for local work.
  • Some legacy columns have odd names (lastRun, barrierFree_moreInfo). Production relies on them, so leave them as they are.

📜 License

GPL-3.0. Built with ❤️ by TUM-Dev for students at TUM.

About

New backend written in go with gRPC as an API interface

Resources

Stars

18 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages