Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
71 lines
2.4 KiB
Go
71 lines
2.4 KiB
Go
package v1
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const instagramAuthorizationScope = "instagram_business_basic,instagram_business_manage_messages"
|
|
|
|
const tiktokAuthorizationScope = "user.info.basic,user.info.username,user.info.stats,user.info.profile,user.account.type,user.insights,message.list.read,message.list.send,message.list.manage"
|
|
|
|
func authorizationReturnTo(c *gin.Context) string {
|
|
if value := strings.TrimSpace(c.Query("return_to")); value != "" {
|
|
return value
|
|
}
|
|
var payload struct {
|
|
ReturnTo string `json:"return_to" form:"return_to"`
|
|
}
|
|
_ = c.ShouldBind(&payload)
|
|
return strings.TrimSpace(payload.ReturnTo)
|
|
}
|
|
|
|
func buildInstagramChatwootAuthorizationURL(accountID uint, returnTo string) (string, error) {
|
|
clientID := strings.TrimSpace(os.Getenv("INSTAGRAM_APP_ID"))
|
|
clientSecret := strings.TrimSpace(os.Getenv("INSTAGRAM_APP_SECRET"))
|
|
if clientID == "" || clientSecret == "" {
|
|
return "", fmt.Errorf("Instagram OAuth is not configured")
|
|
}
|
|
|
|
state, err := signedChatwootOAuthStateWithReturnTo(accountID, clientSecret, returnTo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
frontendURL := strings.TrimRight(envOrDefaultV1("FRONTEND_URL", "http://localhost:3000"), "/")
|
|
params := url.Values{}
|
|
params.Set("client_id", clientID)
|
|
params.Set("redirect_uri", frontendURL+"/instagram/callback")
|
|
params.Set("scope", instagramAuthorizationScope)
|
|
params.Set("enable_fb_login", "0")
|
|
params.Set("force_authentication", "1")
|
|
params.Set("response_type", "code")
|
|
params.Set("state", state)
|
|
return "https://api.instagram.com/oauth/authorize?" + params.Encode(), nil
|
|
}
|
|
|
|
func buildTikTokChatwootAuthorizationURL(accountID uint, returnTo string) (string, error) {
|
|
clientID := strings.TrimSpace(os.Getenv("TIKTOK_APP_ID"))
|
|
clientSecret := strings.TrimSpace(os.Getenv("TIKTOK_APP_SECRET"))
|
|
if clientID == "" || clientSecret == "" {
|
|
return "", fmt.Errorf("TikTok OAuth is not configured")
|
|
}
|
|
|
|
state, err := signedChatwootOAuthStateWithReturnTo(accountID, clientSecret, returnTo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
frontendURL := strings.TrimRight(envOrDefaultV1("FRONTEND_URL", "http://localhost:3000"), "/")
|
|
params := url.Values{}
|
|
params.Set("client_id", clientID)
|
|
params.Set("client_key", clientID)
|
|
params.Set("redirect_uri", frontendURL+"/tiktok/callback")
|
|
params.Set("response_type", "code")
|
|
params.Set("scope", tiktokAuthorizationScope)
|
|
params.Set("state", state)
|
|
return "https://www.tiktok.com/v2/auth/authorize?" + params.Encode(), nil
|
|
}
|