Skip to content
This repository was archived by the owner on Oct 22, 2023. It is now read-only.

Commit 9c5b9a3

Browse files
committed
feat: complete robot listen and database check
1 parent 3cf596c commit 9c5b9a3

18 files changed

+899
-55
lines changed

README.md

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,48 @@ Best Unofficial Midjourney API
2525
- [ ] Send image message, return image link, for image generation function
2626
- [ ] **Multiple Midjourney accounts**
2727

28-
2928
## Start 开始
3029

31-
### Docker 容器
30+
### Use Our Service 使用我们的服务
31+
32+
<details><summary>正在开发中,当前不可用</summary>
33+
<p>
34+
35+
```shell
36+
curl -X 'POST' \
37+
'https://midjourney-api.sku.moe/v1/imagine' \
38+
-H 'accept: application/json' \
39+
-H 'Content-Type: application/json' \
40+
-H 'user_token: <你的 User Token>' \
41+
-d '{
42+
"prompt": "a beautiful girl"
43+
}'
44+
```
45+
46+
</p>
47+
</details>
48+
49+
### Self-deployment 自部署
50+
51+
| Environment Variable | Description |
52+
|----------------------|----------------------------------------------------------------|
53+
| `SECRET` | A secret key |
54+
| `USER_TOKEN` | Discord User Token which account that subscribes to Midjourney |
55+
| `BOT_TOKEN` | Bot token |
56+
| `GUILD_ID` | Guild ID |
57+
| `CHANNEL_ID` | Channel ID |
58+
| `DATABASE_URL` | Optional |
59+
60+
| 环境变量 | 说明 |
61+
|----------------|---------------------------|
62+
| `SECRET` | 私人密钥 |
63+
| `USER_TOKEN` | 订阅了Midjourney的Discord用户令牌 |
64+
| `BOT_TOKEN` | 机器人 Token |
65+
| `GUILD_ID` | 公会ID |
66+
| `CHANNEL_ID` | 频道ID |
67+
| `DATABASE_URL` | 可选 |
68+
69+
#### Docker 容器
3270

3371
Pull 拉取
3472

@@ -39,27 +77,43 @@ docker pull aprilnea/midjourney-api:latest
3977
Run 运行
4078

4179
```docker
42-
docker run -it -e USER_TOKEN="" \
80+
docker run -it \
81+
-e SECRET="" \
82+
-e USER_TOKEN="" \
4383
-e BOT_TOKEN="" \
4484
-e GUILD_ID="" \
4585
-e CHANNEL_ID="" \
4686
-e DATABASE_URL="" \ aprilnea/midjourney-api:latest
4787
```
4888

89+
```shell
90+
curl -X 'POST' \
91+
'https://127.0.0.1:8080/v1/imagine' \
92+
-H 'accept: application/json' \
93+
-H 'Content-Type: application/json' \
94+
-H 'Authentication: Bearer {SECRET}' \
95+
-d '{
96+
"prompt": "a beautiful girl"
97+
}'
98+
```
99+
49100
更多请查看[文档](https://midjourney.sku.moe/midjourney-api/prepare)
50101

51-
For more please check[documentation](https://midjourney.sku.moe/midjourney-api/prepare)
102+
For more please check [documentation](https://midjourney.sku.moe/midjourney-api/prepare)
52103

53104
## Thanks 鸣谢
54105

55106
- [yokonsan/midjourney-api](https://github.com/yokonsan/midjourney-api)
56107

57-
## Donate 捐赠
108+
## License
58109

59-
感谢您的激励,能让该项目持续发展。
110+
[MIT](./LICENSE)
60111

61-
Thank you for inspiring and being able to keep the program going.
112+
----
62113

63-
[GitHub Sponsor](https://github.com/sponsors/AprilNEA) | [爱发电](https://afdian.net/a/aprilnea)
114+
**Midjourney-Web** © [AprilNEA](https://github.com/AprilNEA), Released under the [MIT](./LICENSE) License.
64115

65-
<img src="https://hits-app.vercel.app/hits?url=https%3A%2F%2Fgithub.com%2FAprilNEA%2FMidjourney-API" />
116+
> Help me if you
117+
> want: [GitHub Sponsor](https://github.com/sponsors/AprilNEA) | [爱发电](https://afdian.net/a/aprilnea)
118+
>
119+
> Telegram Channel [@AprilsBlog](https://t.me/AprilsBlog) · Twitter [@AprilNEA](https://twitter.com/AprilNEA)

alembic.ini

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = alembic
6+
7+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
8+
# Uncomment the line below if you want the files to be prepended with date and time
9+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
10+
# for all available tokens
11+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
12+
13+
# sys.path path, will be prepended to sys.path if present.
14+
# defaults to the current working directory.
15+
prepend_sys_path = .
16+
17+
# timezone to use when rendering the date within the migration file
18+
# as well as the filename.
19+
# If specified, requires the python-dateutil library that can be
20+
# installed by adding `alembic[tz]` to the pip requirements
21+
# string value is passed to dateutil.tz.gettz()
22+
# leave blank for localtime
23+
# timezone =
24+
25+
# max length of characters to apply to the
26+
# "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to alembic/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
53+
54+
# set to 'true' to search source files recursively
55+
# in each "version_locations" directory
56+
# new in Alembic version 1.10
57+
# recursive_version_locations = false
58+
59+
# the output encoding used when revision files
60+
# are written from script.py.mako
61+
# output_encoding = utf-8
62+
63+
sqlalchemy.url = driver://user:pass@localhost/dbname
64+
65+
66+
[post_write_hooks]
67+
# post_write_hooks defines scripts or Python functions that are run
68+
# on newly generated revision scripts. See the documentation for further
69+
# detail and examples
70+
71+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
72+
hooks = black
73+
black.type = console_scripts
74+
black.entrypoint = black
75+
black.options = -l 79 REVISION_SCRIPT_FILENAME
76+
77+
# Logging configuration
78+
[loggers]
79+
keys = root,sqlalchemy,alembic
80+
81+
[handlers]
82+
keys = console
83+
84+
[formatters]
85+
keys = generic
86+
87+
[logger_root]
88+
level = WARN
89+
handlers = console
90+
qualname =
91+
92+
[logger_sqlalchemy]
93+
level = WARN
94+
handlers =
95+
qualname = sqlalchemy.engine
96+
97+
[logger_alembic]
98+
level = INFO
99+
handlers =
100+
qualname = alembic
101+
102+
[handler_console]
103+
class = StreamHandler
104+
args = (sys.stderr,)
105+
level = NOTSET
106+
formatter = generic
107+
108+
[formatter_generic]
109+
format = %(levelname)-5.5s [%(name)s] %(message)s
110+
datefmt = %H:%M:%S

alembic/__init__.py

Whitespace-only changes.

alembic/env.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from config import Config
2+
from logging.config import fileConfig
3+
4+
from sqlalchemy import engine_from_config
5+
from sqlalchemy import pool
6+
7+
from alembic import context
8+
9+
# this is the Alembic Config object, which provides
10+
# access to the values within the .ini file in use.
11+
config = context.config
12+
13+
# Interpret the config file for Python logging.
14+
# This line sets up loggers basically.
15+
if config.config_file_name is not None:
16+
fileConfig(config.config_file_name)
17+
18+
# add your model's MetaData object here
19+
# for 'autogenerate' support
20+
from app.database import Base
21+
22+
target_metadata = Base.metadata
23+
24+
25+
# other values from the config, defined by the needs of env.py,
26+
# can be acquired:
27+
# my_important_option = config.get_main_option("my_important_option")
28+
# ... etc.
29+
30+
31+
def run_migrations_offline() -> None:
32+
"""Run migrations in 'offline' mode.
33+
34+
This configures the context with just a URL
35+
and not an Engine, though an Engine is acceptable
36+
here as well. By skipping the Engine creation
37+
we don't even need a DBAPI to be available.
38+
39+
Calls to context.execute() here emit the given string to the
40+
script output.
41+
42+
"""
43+
# url = config.get_main_option("sqlalchemy.url")
44+
url = Config.database_url
45+
46+
context.configure(
47+
url=url,
48+
target_metadata=target_metadata,
49+
literal_binds=True,
50+
dialect_opts={"paramstyle": "named"},
51+
)
52+
53+
with context.begin_transaction():
54+
context.run_migrations()
55+
56+
57+
def run_migrations_online() -> None:
58+
"""Run migrations in 'online' mode.
59+
60+
In this scenario we need to create an Engine
61+
and associate a connection with the context.
62+
63+
"""
64+
alembic_config = config.get_section(config.config_ini_section)
65+
alembic_config["sqlalchemy.url"] = Config.database_url.replace(
66+
"postgresql+asyncpg://", "postgresql+psycopg2://"
67+
)
68+
69+
connectable = engine_from_config(
70+
alembic_config,
71+
prefix="sqlalchemy.",
72+
poolclass=pool.NullPool,
73+
)
74+
75+
with connectable.connect() as connection:
76+
context.configure(connection=connection, target_metadata=target_metadata)
77+
78+
with context.begin_transaction():
79+
context.run_migrations()
80+
81+
82+
if context.is_offline_mode():
83+
run_migrations_offline()
84+
else:
85+
run_migrations_online()

alembic/script.py.mako

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade() -> None:
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade() -> None:
24+
${downgrades if downgrades else "pass"}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""init projects
2+
3+
Revision ID: 1eca776ad538
4+
Revises:
5+
Create Date: 2023-06-26 18:08:20.907129
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "1eca776ad538"
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade() -> None:
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
pass
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade() -> None:
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
pass
28+
# ### end Alembic commands ###

app/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from app import schema
1313
from app.routes import v1_router
1414
from app.database import db_startup
15+
from app.middleware import SecretBearer
1516

1617
app = FastAPI(title="Midjourney API", description="", version="0.1.0")
1718
app.add_middleware(
@@ -93,4 +94,5 @@ async def all_exception_handler(request: Request, exc: Exception):
9394
v1_router,
9495
prefix="/v1",
9596
tags=["v1"],
97+
dependencies=[Depends(SecretBearer())],
9698
)

0 commit comments

Comments
 (0)