Skip to content

Commit

Permalink
Add API prefix settings (janhq#2528)
Browse files Browse the repository at this point in the history
Co-authored-by: Van Pham <[email protected]>
  • Loading branch information
GenkaOk and Van-QA authored Apr 4, 2024
1 parent c93283e commit 089e311
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/src/node/api/processors/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class App implements Processor {
isVerboseEnabled: args?.isVerboseEnabled,
schemaPath: join(await appResourcePath(), 'docs', 'openapi', 'jan.yaml'),
baseDir: join(await appResourcePath(), 'docs', 'openapi'),
prefix: args?.prefix,
})
}

Expand Down
3 changes: 2 additions & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ServerConfig {
isVerboseEnabled?: boolean
schemaPath?: string
baseDir?: string
prefix?: string
storageAdataper?: any
}

Expand Down Expand Up @@ -119,7 +120,7 @@ export const startServer = async (configs?: ServerConfig): Promise<boolean> => {
server.addHook('preHandler', configs.storageAdataper)

// Register API routes
await server.register(v1Router, { prefix: '/v1' })
await server.register(v1Router, { prefix: configs?.prefix ?? '/v1' })
// Start listening for requests
await server
.listen({
Expand Down
1 change: 1 addition & 0 deletions web/helpers/atoms/ApiServer.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const hostOptions = ['127.0.0.1', '0.0.0.0']

export const apiServerPortAtom = atomWithStorage('apiServerPort', '1337')
export const apiServerHostAtom = atomWithStorage('apiServerHost', '127.0.0.1')
export const apiServerPrefix = atomWithStorage('apiServerPrefix', '/v1')

export const apiServerCorsEnabledAtom = atomWithStorage(
'apiServerCorsEnabled',
Expand Down
48 changes: 47 additions & 1 deletion web/screens/LocalServer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ import {
apiServerCorsEnabledAtom,
apiServerHostAtom,
apiServerPortAtom,
apiServerPrefix,
apiServerVerboseLogEnabledAtom,
hostOptions,
} from '@/helpers/atoms/ApiServer.atom'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'

const LocalServerScreen = () => {
const [errorRangePort, setErrorRangePort] = useState(false)
const [errorPrefix, setErrorPrefix] = useState(false)
const [serverEnabled, setServerEnabled] = useAtom(serverEnabledAtom)
const showRightSideBar = useAtomValue(showRightSideBarAtom)
const setModalTroubleShooting = useSetAtom(modalTroubleShootingAtom)
Expand All @@ -80,6 +82,7 @@ const LocalServerScreen = () => {
)
const [host, setHost] = useAtom(apiServerHostAtom)
const [port, setPort] = useAtom(apiServerPortAtom)
const [prefix, setPrefix] = useAtom(apiServerPrefix)
const [loadModelError, setLoadModelError] = useAtom(loadModelErrorAtom)

const FIRST_TIME_VISIT_API_SERVER = 'firstTimeVisitAPIServer'
Expand All @@ -95,6 +98,14 @@ const LocalServerScreen = () => {
[setPort]
)

const handleChangePrefix = useCallback(
(value: string) => {
setErrorPrefix(!value.length || !value.startsWith('/'))
setPrefix(value)
},
[setPrefix]
)

useEffect(() => {
if (localStorage.getItem(FIRST_TIME_VISIT_API_SERVER) == null) {
setFirstTimeVisitAPIServer(true)
Expand All @@ -105,12 +116,17 @@ const LocalServerScreen = () => {
handleChangePort(port)
}, [handleChangePort, port])

useEffect(() => {
handleChangePrefix(prefix)
}, [handleChangePrefix, prefix])

const onStartServerClick = async () => {
if (selectedModel == null) return
try {
const isStarted = await window.core?.api?.startServer({
host,
port,
prefix,
isCorsEnabled,
isVerboseEnabled,
})
Expand Down Expand Up @@ -159,7 +175,12 @@ const LocalServerScreen = () => {
<Button
block
themes={serverEnabled ? 'danger' : 'primary'}
disabled={stateModel.loading || errorRangePort || !selectedModel}
disabled={
stateModel.loading ||
errorRangePort ||
errorPrefix ||
!selectedModel
}
onClick={onToggleServer}
>
{serverEnabled ? 'Stop' : 'Start'} Server
Expand Down Expand Up @@ -225,6 +246,31 @@ const LocalServerScreen = () => {
<p className="mt-2 text-xs text-danger">{`The port range should be from 0 to 65536`}</p>
)}
</div>
<div>
<label
id="prefix"
className="mb-2 inline-flex items-start gap-x-2 font-bold text-zinc-500 dark:text-gray-300"
>
API Prefix
</label>
<div className="flex items-center justify-between">
<Input
className={twMerge(
'w-full flex-shrink-0',
errorPrefix && 'border-danger'
)}
type="text"
value={prefix}
onChange={(e) => {
handleChangePrefix(e.target.value)
}}
disabled={serverEnabled}
/>
</div>
{errorPrefix && (
<p className="mt-2 text-xs text-danger">{`Prefix should start with /`}</p>
)}
</div>
<div>
<label
id="cors"
Expand Down

0 comments on commit 089e311

Please sign in to comment.