Skip to content

Commit

Permalink
support route base
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomkk-qfeng committed Jul 4, 2023
1 parent 15694f8 commit 3f1759d
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ cfg.json
package-lock.json
pnpm-lock.yaml
tsconfig.tsbuildinfo
front-standalone/dist/*
75 changes: 75 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Open-OAuth2Playground
Open-OAuth2Playground: 简单而好用的开源 OAuth2-Playground

![](https://github.com/ECNU/Open-OAuth2Playground/blob/main/demo.jpg?raw=true)

- [Open-OAuth2Playground](#open-oauth2playground)
- [安装运行](#安装运行)
- [二进制直接运行](#二进制直接运行)
- [systemctl 托管](#systemctl-托管)
- [编译打包](#编译打包)
- [后端编译](#后端编译)
- [前端编译](#前端编译)
- [统一打包](#统一打包)
- [配置](#配置)
- [后端配置](#后端配置)
- [前端配置](#前端配置)
- [API](#api)


## 安装运行

### 二进制直接运行
[release](https://github.com/ECNU/Open-OAuth2Playground/releases) 中下载最新的 [release] 包,解压后直接运行即可。

```
tar -zxvf Open-OAuth2Playground-0.1.0-linux-amd64.tar.gz
cd Open-OAuth2Playground/
./control start
```
访问你服务器的 80 端口即可使用。


### systemctl 托管
假定部署在 `/opt/Open-OAuth2Playground` 目录下,如果部署在其他目录修改 `playground.service` 中的 `WorkingDirectory``ExecStart` 两个字段即可。
```
cp playground.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable playground
systemctl start playground
```

### 编译打包
获取项目源码
```
git clone https://github.com/ECNU/Open-OAuth2Playground.git
```
#### 后端编译
```
cd Open-OAuth2Playground/
go mod tidy
go build
```
#### 前端编译
```
cd front-standalone/
pnpm install
pnpm build
```
#### 统一打包
```
cd ..
chmod +x control
./control pack
```

### 配置
#### 后端配置
修改 `cfg.json`

#### 前端配置
修改 `.env.production`

注意前端的 `route_base` 需要和后端相匹配。
### API
todo
108 changes: 108 additions & 0 deletions control
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash

WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE

mkdir -p var

app=Open-OAuth2Playground
conf=cfg.json
pidfile=var/app.pid
logfile=var/app.log

function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
fi
return 0
}

function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
fi

if ! [ -f $conf ];then
echo "Config file $conf doesn't exist, creating one."
cp cfg.example.json $conf
fi
nohup ./$app -c $conf &> $logfile &
echo $! > $pidfile
echo "$app started..., pid=$!"
}

function stop() {
pid=`cat $pidfile`
kill $pid
echo "$app stoped..."
}

function restart() {
stop
sleep 1
start
}

function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo started
else
echo stoped
fi
}

function tailf() {
tail -f $logfile
}

function build() {
go build
if [ $? -ne 0 ]; then
exit $?
fi
./$app -v
}

function pack() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v`
file_list="front-standalone control cfg.json cfg.json.example $app"
echo "...tar $app-$version.tar.gz <= $file_list"
tar zcf $app-$version.tar.gz gitversion $file_list
}


function help() {
echo "$0 build|pack|start|stop|restart|status|tail"
}

if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
elif [ "$1" == "build" ];then
build
elif [ "$1" == "pack" ];then
pack
else
help
fi
9 changes: 7 additions & 2 deletions controller/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import (

func Routes(r *gin.Engine) {

r.GET("/playground/v1/config", getConfig)
r.Static(g.Config().Http.RouteBase+"css/", "front-standalone/dist/css/")
r.Static(g.Config().Http.RouteBase+"js/", "front-standalone/dist/js/")
r.StaticFile(g.Config().Http.RouteBase, "front-standalone/dist/index.html")
r.StaticFile(g.Config().Http.RouteBase+"favicon.ico", "front-standalone/dist/favicon.ico")

playground := r.Group("/playground/v1")
r.GET(g.Config().Http.RouteBase+"v1/config", getConfig)

playground := r.Group(g.Config().Http.RouteBase + "v1")
playground.Use(IPLimitCheck)
playground.Use(NoCache())
playground.POST("/oauth2/client_credentials", clientCredentials)
Expand Down
Binary file added demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions front-standalone/.env.development
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Router path
VUE_APP_ROUTER_BASE=/playground/
VUE_APP_ROUTER_BASE=/
# Api Config
VUE_APP_API_PROTO=http
VUE_APP_API_HOST=localhost
VUE_APP_API_PORT=
VUE_APP_API_ROUTE=/playground/v1
VUE_APP_API_VERSION=v1
9 changes: 4 additions & 5 deletions front-standalone/.env.production
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Router path
VUE_APP_ROUTER_BASE=/playground/
# Api Config
VUE_APP_API_PROTO=http
VUE_APP_API_HOST=localhost
VUE_APP_ROUTER_BASE=/
VUE_APP_API_PROTO=
VUE_APP_API_HOST=
VUE_APP_API_PORT=
VUE_APP_API_ROUTE=/playground/v1
VUE_APP_API_VERSION=v1
15 changes: 0 additions & 15 deletions front-standalone/README.en.md

This file was deleted.

15 changes: 0 additions & 15 deletions front-standalone/README.md

This file was deleted.

4 changes: 2 additions & 2 deletions front-standalone/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Layout from '/@/views/Layout.vue';

const routes = [
{
path: '/',
path: process.env.VUE_APP_ROUTER_BASE,
name: 'Layout',
component: Layout,
meta: {title: 'OO2P'}
}
];

const router = createRouter({
history: createWebHistory("/playground/"),
history: createWebHistory("/"),
routes: routes
});

Expand Down
5 changes: 3 additions & 2 deletions front-standalone/src/utils/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class HttpService{
const proto = process.env.VUE_APP_API_PROTO;
const host = process.env.VUE_APP_API_HOST;
const port = process.env.VUE_APP_API_PORT?.length>0?":".concat(process.env.VUE_APP_API_PORT):"";
const route = process.env.VUE_APP_API_ROUTE;
const route = process.env.VUE_APP_ROUTER_BASE+process.env.VUE_APP_API_VERSION;
const baseURL = host ? proto+"://"+host+port+route : route;
this.http = axios.create({
baseURL: proto+"://"+host+port+route,
baseURL: baseURL,
timeout: 60000,
});

Expand Down
1 change: 1 addition & 0 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type EndpointConfig struct {
HttpConfig Http 配置
*/
type HttpConfig struct {
RouteBase string `json:"route_base"`
Listen string `json:"listen"`
CORS []string `json:"cors"`
TrustProxy []string `json:"trust_proxy"`
Expand Down
21 changes: 21 additions & 0 deletions playground.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[Unit]
Description=playground
After=network-online.target
Wants=network-online.target

[Service]
# modify when deploy in prod env
User=root
Group=root

Type=simple
ExecStart=/opt/Open-OAuth2Playground/Open-OAuth2Playground
WorkingDirectory=/opt/Open-OAuth2Playground

Restart=always
RestartSec=1
StartLimitInterval=0

[Install]
WantedBy=multi-user.target

0 comments on commit 3f1759d

Please sign in to comment.