Package postgres.sql and link template file using go:embed

This commit is contained in:
denverquane
2023-10-29 10:23:06 -07:00
parent c904761772
commit 64de25915f
5 changed files with 29 additions and 20 deletions

View File

@ -38,7 +38,7 @@ FROM alpine:3.13.0 AS final
# * App directory to allow mounting volumes
RUN addgroup -g 1000 bot && \
adduser -HD -u 1000 -G bot bot && \
mkdir -p /app/logs /app/locales /app/storage /app/templates && \
mkdir -p /app/logs /app/locales && \
chown -R bot:bot /app
USER bot
WORKDIR /app
@ -46,8 +46,6 @@ WORKDIR /app
# Import the compiled executable and locales.
COPY --from=builder /app /app
COPY ./locales/ /app/locales
COPY ./templates/ /app/templates
COPY ./storage/postgres.sql /app/storage/postgres.sql
# Port used for AMU API
EXPOSE 5000

View File

@ -1,6 +1,7 @@
package bot
import (
_ "embed"
"github.com/automuteus/automuteus/v8/bot/command"
"github.com/automuteus/automuteus/v8/docs"
"github.com/automuteus/automuteus/v8/pkg/discord"
@ -8,14 +9,17 @@ import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"html/template"
"net/http"
"os"
"strings"
)
//go:embed templates/link.tmpl
var linkTemplateFileContents string
func (bot *Bot) StartAPIServer(port string) {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
docs.SwaggerInfo.BasePath = "/"
docs.SwaggerInfo.Title = "AutoMuteUs"
@ -119,9 +123,24 @@ func handleGetOpenAmongUsCapture(bot *Bot) func(c *gin.Context) {
return
}
hyperlink, _, _ := formCaptureURL(bot.url, connectCode)
c.HTML(http.StatusOK, "link.tmpl", gin.H{
t, err := template.New("template").Parse(linkTemplateFileContents)
if err != nil {
c.JSON(http.StatusInternalServerError, HttpError{
StatusCode: http.StatusInternalServerError,
Error: err.Error(),
})
return
}
err = t.Execute(c.Writer, map[string]string{
"URL": hyperlink,
})
if err != nil {
c.JSON(http.StatusInternalServerError, HttpError{
StatusCode: http.StatusInternalServerError,
Error: err.Error(),
})
return
}
}
}

View File

@ -1,6 +1,7 @@
package main
import (
_ "embed"
"errors"
"fmt"
"github.com/automuteus/automuteus/v8/bot/command"
@ -32,6 +33,9 @@ var (
date = "unknown"
)
//go:embed storage/postgres.sql
var postgresFileContents string
const (
DefaultURL = "http://localhost:8123"
DefaultMaxRequests5Sec int64 = 7
@ -160,7 +164,7 @@ func discordMainWrapper() error {
if !isOfficial {
go func() {
err := psql.LoadAndExecFromFile("./storage/postgres.sql")
err := psql.ExecFromString(postgresFileContents)
if err != nil {
log.Println("Exiting with fatal error when attempting to execute postgres.sql:")
log.Fatal(err)

View File

@ -10,9 +10,7 @@ import (
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/top-gg/go-dbl"
"io"
"log"
"os"
"strconv"
"time"
)
@ -52,18 +50,8 @@ func (psqlInterface *PsqlInterface) Init(addr string) error {
return nil
}
func (psqlInterface *PsqlInterface) LoadAndExecFromFile(filepath string) error {
f, err := os.Open(filepath)
if err != nil {
return err
}
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
return err
}
tag, err := psqlInterface.Pool.Exec(context.Background(), string(bytes))
func (psqlInterface *PsqlInterface) ExecFromString(postgresFileContents string) error {
tag, err := psqlInterface.Pool.Exec(context.Background(), postgresFileContents)
if err != nil {
return err
}