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

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)
})
}