@@ -6,6 +6,7 @@ import { HTTPException } from "hono/http-exception";
66import { basicAuth } from "hono/basic-auth" ;
77import { parseUsers } from "./parse-users.js" ;
88import { html } from "hono/html" ;
9+ import { getDomainUrl } from "./get-domain-url.js" ;
910const baseURL = process . env . MOODLE_BASE_URL ;
1011if ( ! baseURL ) {
1112 throw new Error ( "MOODLE_BASE_URL is not set" ) ;
@@ -40,6 +41,13 @@ const moodleAuth = async (c, next) => {
4041const app = new Hono ( ) ;
4142app . use ( cors ( ) ) ;
4243app . use ( logger ( ) ) ;
44+ app . use ( async ( c , next ) => {
45+ const domainUrl = getDomainUrl ( c . req . raw , {
46+ defaultHost : "localhost:3000" ,
47+ } ) ;
48+ c . set ( "domainUrl" , domainUrl ) ;
49+ await next ( ) ;
50+ } ) ;
4351// Apply middleware to all /api routes
4452app . use ( "/api/*" , moodleAuth ) ;
4553app . post ( "/api/usecases/:function" , async ( c ) => {
@@ -127,22 +135,33 @@ app.get("/meta", async (c) => {
127135// swagger 2.0 docs
128136app . get ( "/docs/swagger/:id" , ( c ) => {
129137 const id = c . req . param ( "id" ) ;
138+ const domainUrl = c . get ( "domainUrl" ) ;
130139 const swaggerDocs = swagger2 . filter ( Boolean ) ;
131140 const doc = swaggerDocs [ Number ( id ) ] ;
132141 if ( ! doc ) {
133142 return c . json ( { error : true , message : "Invalid id" } , { status : 400 } ) ;
134143 }
144+ // we need to update the basePath in the swagger doc
145+ doc . host = domainUrl ;
135146 return c . json ( doc , { status : 200 } ) ;
136147} ) ;
137148app . get ( "/docs/openapi_3_1/:id{.+\\.json}" , ( c ) => {
138149 const id = c . req . param ( "id" ) ;
150+ const domainUrl = c . get ( "domainUrl" ) ;
139151 // remove the .json from the id
140152 const idWithoutJson = id . replace ( ".json" , "" ) ;
141153 const openapi31Docs = [ openapi31 , usecaseOpenapi ] . filter ( Boolean ) ;
142154 const doc = openapi31Docs [ Number ( idWithoutJson ) ] ;
143155 if ( ! doc ) {
144156 return c . json ( { error : true , message : "Invalid id" } , { status : 400 } ) ;
145157 }
158+ // we need to update the servers in the openapi doc
159+ doc . servers = [
160+ {
161+ url : `${ domainUrl } /api` ,
162+ description : "Moodle webservice API" ,
163+ } ,
164+ ] ;
146165 return c . json ( doc , { status : 200 } ) ;
147166} ) ;
148167app . get ( "/docs/openapi_3_1/:id" , ( c ) => {
0 commit comments