make it universal

This commit is contained in:
2026-02-21 19:33:19 +03:00
parent 408ae6a76b
commit 3090b18ff9
10 changed files with 109 additions and 212 deletions

87
main.go
View File

@@ -1,22 +1,15 @@
package main
import (
"encoding/json"
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"raindrop-glance/raindrop"
)
func sendJSON(w http.ResponseWriter, data any, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, err.Error(), 500)
}
}
"golang.org/x/oauth2"
)
func main() {
authConfigPath := os.Getenv("AUTH_CONFIG")
@@ -24,72 +17,47 @@ func main() {
log.Fatalf("AUTH_CONFIG env is missing")
}
if _, err := os.Stat(authConfigPath); os.IsNotExist(err) {
os.Create(authConfigPath)
os.WriteFile(authConfigPath, []byte("{}"), 0644)
}
data, err := os.ReadFile(authConfigPath)
token, err := readConfig(authConfigPath)
if err != nil {
log.Fatalf("failed to read auth config file: %v\n", err)
log.Fatalf("failed to read config file: %v\n", err)
}
config := new(raindrop.TokensResponse)
if err := json.Unmarshal(data, config); err != nil {
log.Fatalf("failed to decode auth config file: %v\n", err)
}
rdClient, err := raindrop.NewClient(raindrop.ClientConfig{
ClientId: os.Getenv("CLIENT_ID"),
oauthConfig := oauth2.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
RedirectURI: os.Getenv("REDIRECT_URI"),
})
if err != nil {
log.Fatalf("failed to create raindrop client: %v\n", err)
}
if config.AccessToken != "" {
rdClient.SetApiToken(config.TokenType, config.AccessToken)
Endpoint: oauth2.Endpoint{
AuthURL: os.Getenv("AUTH_URL"),
TokenURL: os.Getenv("TOKEN_URL"),
DeviceAuthURL: os.Getenv("DEVICE_AUTH_URL"),
AuthStyle: oauth2.AuthStyleAutoDetect,
},
RedirectURL: os.Getenv("REDIRECT_URI"),
}
mux := http.NewServeMux()
mux.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) {
rdClient.OauthRedirect(w, r)
mux.HandleFunc("GET /oauth", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, oauthConfig.AuthCodeURL(""), http.StatusTemporaryRedirect)
})
mux.HandleFunc("GET /oauth/redirect", func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
if code == "" {
http.Error(w, "invalid request", 400)
return
}
resp, err := rdClient.ExchangeOauthCode(code)
mux.HandleFunc("GET /oauth/callback", func(w http.ResponseWriter, r *http.Request) {
token, err = oauthConfig.Exchange(context.Background(), r.URL.Query().Get("code"))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
config = resp
jsonData, err := json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, fmt.Sprintf("failed to encode config file: %v", err), 500)
if err := writeConfig(authConfigPath, token); err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := os.WriteFile(authConfigPath, jsonData, 0644); err != nil {
http.Error(w, fmt.Sprintf("failed to save config file: %v", err), 500)
return
}
rdClient.SetApiToken(resp.TokenType, resp.AccessToken)
w.WriteHeader(200)
fmt.Fprintf(w, "ok")
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
baseURL := "https://api.raindrop.io/rest/v1"
baseURL := os.Getenv("API_URL")
req, err := http.NewRequest(r.Method, baseURL+r.URL.Path, r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("failed to create request: %v", err), 500)
@@ -97,8 +65,8 @@ func main() {
}
req.URL.RawQuery = r.URL.RawQuery
if config.AccessToken != "" {
authHeader := fmt.Sprintf("%s %s", config.TokenType, config.AccessToken)
if token.AccessToken != "" {
authHeader := fmt.Sprintf("%s %s", token.TokenType, token.AccessToken)
req.Header.Add("Authorization", authHeader)
}
@@ -118,15 +86,6 @@ func main() {
if _, err := io.Copy(w, resp.Body); err != nil {
http.Error(w, err.Error(), 500)
}
// data, err, status := rdClient.MakeApiRequest(r.Method, r.URL.Path, r.Body, r.URL.Query())
// if err != nil {
// http.Error(w, err.Error(), status)
// return
// }
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(status)
// w.Write(data)
})
log.Println("starting http server")