8000 更新 db.go by luyanci · Pull Request #3 · luyanci/remake_bot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

更新 db.go #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,9 @@ RUN go mod tidy -v;CGO_ENABLED=0 go build -o /bot

FROM alpine:latest
WORKDIR /src
RUN apk update && \
apk add --no-cache postgresql postgresql-client && \
apk add --no-cache build-base && \
rm -rf /var/cache/apk/*

ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=114514
ENV POSTGRES_DB=postgres

RUN mkdir -p /var/lib/postgresql/data
RUN chown -R postgres:postgres /var/lib/postgresql/data

RUN su - postgres -c "initdb -D /var/lib/postgresql/data"

COPY start_postgres.sh /start_postgres.sh
RUN chmod +x /start_postgres.sh

COPY --from=build /bot bot
COPY --from=build /src/countries.json countries.json
COPY --from=build /src/user_list.json user_list.json

EXPOSE 5432
CMD ["/start_postgres.sh", "/src/bot" ]
CMD [ "/src/bot" ]
2 changes: 1 addition & 1 deletion bot/module.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bot

import (
"github.com/purofle/remake_bot/database"
"github.com/luyanci/remake_bot/database"
"go.uber.org/fx"
)

Expand Down
23 changes: 22 additions & 1 deletion database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ import (
"context"
"database/sql"
_ "github.com/lib/pq"
"os"
"go.uber.org/fx"
"go.uber.org/zap"
)

var Module = fx.Provide(NewDatabase)

func NewDatabase(lc fx.Lifecycle, logger *zap.Logger) (*sql.DB, error) {
connStr := "postgresql://postgres:114514@localhost:5432/postgres?sslmode=disable"
user := os.Getenv("POSTGRES_USER")
password := os.Getenv("POSTGRES_PASSWORD")
host := os.Getenv("POSTGRES_HOST")
port := os.Getenv("POSTGRES_PORT")
dbname := os.Getenv("POSTGRES_DB")
if user == "" {
user = "postgres"
}
if password == "" {
password = "114514"
}
if host == "" {
host = "localhost"
}
if port == "" {
port = "5432"
}
if dbname == "" {
dbname = "postgres"
}
connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", user, password, host, port, dbname)

db, err := sql.Open("postgres", connStr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/purofle/remake_bot
module github.com/luyanci/remake_bot

go 1.21

Expand Down
11 changes: 0 additions & 11 deletions start_postgres.sh

This file was deleted.

0