Initialize Tinder API Wrapper with server configuration and Docker setup

This commit is contained in:
2025-03-20 22:19:48 +01:00
commit e99b56e434
17 changed files with 1459 additions and 0 deletions

25
cmd/server/Dockerfile Normal file
View File

@@ -0,0 +1,25 @@
FROM golang:1.18-alpine AS builder
WORKDIR /app
# Copy go mod files
COPY go.mod ./
COPY go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN go build -o /tinder-proxy ./cmd/server
# Use a smaller image for the final container
FROM alpine:latest
WORKDIR /
COPY --from=builder /tinder-proxy /tinder-proxy
EXPOSE 8080
ENTRYPOINT ["/tinder-proxy"]

View File

@@ -0,0 +1,15 @@
package config
// Config holds the server configuration
type Config struct {
ListenAddr string
TargetAPI string
}
// New creates a new Config with default values
func New() *Config {
return &Config{
ListenAddr: ":8080",
TargetAPI: "https://tinder.cloonar.com",
}
}

139
cmd/server/handlers/auth.go Normal file
View File

@@ -0,0 +1,139 @@
package handlers
import (
"fmt"
"net/http"
tinder "tinder-api-wrapper"
)
// HandleSendPhoneAuth handles the phone authentication request
func HandleSendPhoneAuth(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var authReq struct {
PhoneNumber string `json:"phone_number"`
}
if err := readJSON(r, &authReq); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp, err := client.SendPhoneLogin(authReq.PhoneNumber)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandleValidateOTP handles the OTP validation request
func HandleValidateOTP(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var authReq struct {
PhoneNumber string `json:"phone_number"`
OTP string `json:"otp"`
}
if err := readJSON(r, &authReq); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp, err := client.LoginWithOTP(authReq.PhoneNumber, authReq.OTP)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandleFacebookAuth handles Facebook authentication
func HandleFacebookAuth(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var authReq struct {
FacebookID string `json:"facebook_id"`
FacebookToken string `json:"facebook_token"`
}
if err := readJSON(r, &authReq); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp, err := client.LoginWithFacebook(authReq.FacebookID, authReq.FacebookToken)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandleRefreshAuth handles authentication token refresh
func HandleRefreshAuth(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var authReq struct {
RefreshToken string `json:"refresh_token"`
}
if err := readJSON(r, &authReq); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp, err := client.RefreshAuth(authReq.RefreshToken)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandleGetProfile handles profile retrieval
func HandleGetProfile(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
authToken := r.Header.Get("X-Auth-Token")
if authToken == "" {
http.Error(w, "Missing authentication token", http.StatusUnauthorized)
return
}
clientWithAuth := client.WithAuthToken(authToken)
resp, err := clientWithAuth.GetProfile()
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}

View File

@@ -0,0 +1,76 @@
package handlers
import (
"fmt"
"net/http"
"strings"
tinder "tinder-api-wrapper"
)
// HandleLike handles the /like/{userId} endpoint
func HandleLike(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
path := strings.TrimPrefix(r.URL.Path, "/like/")
if path == "" {
http.Error(w, "User ID is required", http.StatusBadRequest)
return
}
resp, err := client.LikeUser(path)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandlePass handles the /pass/{userId} endpoint
func HandlePass(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
path := strings.TrimPrefix(r.URL.Path, "/pass/")
if path == "" {
http.Error(w, "User ID is required", http.StatusBadRequest)
return
}
resp, err := client.PassUser(path)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}
// HandleGetRecs handles the /v2/recs/core endpoint
func HandleGetRecs(client *tinder.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
locale := r.URL.Query().Get("locale")
resp, err := client.GetRecommendations(locale)
if err != nil {
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, resp)
}
}

View File

@@ -0,0 +1,37 @@
package handlers
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// readJSON reads and decodes JSON from request body
func readJSON(r *http.Request, v interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("failed to read request body: %v", err)
}
if err := json.Unmarshal(body, v); err != nil {
return fmt.Errorf("invalid request format: %v", err)
}
return nil
}
// writeJSON writes JSON response with proper headers
func writeJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)
}
// LogMiddleware logs all HTTP requests
func LogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}

35
cmd/server/main.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"flag"
"fmt"
"log"
"net/http"
tinder "tinder-api-wrapper"
"tinder-api-wrapper/cmd/server/config"
"tinder-api-wrapper/cmd/server/router"
)
func main() {
// Initialize configuration
cfg := config.New()
// Parse command line flags
flag.StringVar(&cfg.ListenAddr, "listen", cfg.ListenAddr, "Address to listen on (e.g., :8080)")
flag.StringVar(&cfg.TargetAPI, "target", cfg.TargetAPI, "Target Tinder API endpoint")
flag.Parse()
// Create Tinder client
client, err := tinder.NewClient(cfg.TargetAPI)
if err != nil {
log.Fatalf("Failed to create Tinder client: %v", err)
}
// Setup router
handler := router.Setup(client)
// Start server
fmt.Printf("Starting Tinder API proxy server on %s -> %s\n", cfg.ListenAddr, cfg.TargetAPI)
log.Fatal(http.ListenAndServe(cfg.ListenAddr, handler))
}

View File

@@ -0,0 +1,28 @@
package router
import (
"net/http"
tinder "tinder-api-wrapper"
"tinder-api-wrapper/cmd/server/handlers"
)
// Setup creates and configures the HTTP router
func Setup(client *tinder.Client) http.Handler {
mux := http.NewServeMux()
// Core endpoints
mux.HandleFunc("/like/", handlers.HandleLike(client))
mux.HandleFunc("/pass/", handlers.HandlePass(client))
mux.HandleFunc("/v2/recs/core", handlers.HandleGetRecs(client))
// Auth endpoints
mux.HandleFunc("/v2/auth/sms/send", handlers.HandleSendPhoneAuth(client))
mux.HandleFunc("/v2/auth/sms/validate", handlers.HandleValidateOTP(client))
mux.HandleFunc("/v2/auth/facebook", handlers.HandleFacebookAuth(client))
mux.HandleFunc("/v2/auth/login/refresh", handlers.HandleRefreshAuth(client))
mux.HandleFunc("/v2/profile", handlers.HandleGetProfile(client))
// Wrap with logging middleware
return handlers.LogMiddleware(mux)
}