A Golang HTTP gateway with OAuth authentication and Casbin authorization middleware, built with Beego web framework and a React frontend.
โโโโโโโโโโโโโโโโโโโ
โ Client โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP Gateway (Port 9000) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ OAuth Middleware โ โ
โ โ (JWT Authentication) โ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโโ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Casbin Middleware โ โ
โ โ (Authorization) โ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโโ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ API Controllers โ โ
โ โ + Static Files โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
git clone https://github.com/casbin/mcp-gateway.git cd mcp-gateway
go mod download
go build -o mcp-gateway .
./mcp-gateway
The backend will start on port 9000.
cd web
npm install
npm run dev
npm run build
The built files will be in web/dist and automatically served by the backend on port 9000.
Two demo accounts are available for testing:
alice / password123 (has admin role with full access)bob / password456 (has user role with limited access)GET /health - Health check endpointPOST /login - Login endpointGET /api/users - List all users (admin only)GET /api/users/:id - Get specific user (admin only)POST /api/users - Create new user (admin only)GET /api/profile - Get current user profilePUT /api/profile - Update current user profileAll protected endpoints require a JWT token in the Authorization header:
Authorization: Bearer <token>
Example login request:
curl -X POST http://localhost:9000/login \ -H "Content-Type: application/json" \ -d '{"username":"alice","password":"password123"}'
Example authenticated request:
curl http://localhost:9000/api/users \ -H "Authorization: Bearer <your-token>"
Edit conf/app.conf to configure OAuth settings:
[dev] oauth.secret = your-secret-key-dev [prod] oauth.secret = ${OAUTH_SECRET||your-secret-key}
Edit conf/policy.csv to modify access control rules:
p, admin, /api/*, GET p, admin, /api/*, POST p, user, /api/profile, GET g, alice, admin g, bob, user
The format is: p, role, resource, action for permissions and g, user, role for role assignments.
go test ./... -v
go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
cd web
npm run dev
This will start the development server on port 8001 with hot module replacement.
go build -ldflags="-s -w" -o mcp-gateway .
cd web npm run build cd ..
./mcp-gateway
The application will serve both backend APIs and frontend static files on port 9000.
You can containerize the application:
FROM golang:1.21-alpine AS backend-builder WORKDIR /app COPY go.* ./ RUN go mod download COPY . . RUN go build -ldflags="-s -w" -o mcp-gateway . FROM node:20-alpine AS frontend-builder WORKDIR /app COPY web/package*.json ./ RUN npm ci COPY web/ ./ RUN npm run build FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=backend-builder /app/mcp-gateway . COPY --from=frontend-builder /app/dist ./web/dist COPY conf ./conf EXPOSE 9000 CMD ["./mcp-gateway"]
Apache-2.0 License
Contributions are welcome! Please feel free to submit a Pull Request.