diff --git a/main.go b/main.go new file mode 100644 index 0000000..6ecc707 --- /dev/null +++ b/main.go @@ -0,0 +1,40 @@ +package main + +import "log" + +type Blog struct { + Id string `json:"id"` + Content string `json:"content"` + Date string `json:"created_at"` + Author Account `json:"account"` +} + +type Config struct { + Listen string `json:"listen"` + CertPath string `json:"cert_path"` + KeyPath string `json:"key_path"` + BaseURL string `json:"base_url"` + Title string `json:"title"` + HomeMessage string `json:"home_message"` +} + +type Account struct { + Id string `json:"id"` + Name string `json:"display_name"` + Url string `json:"url"` +} + +type Thread struct { + Ancestors []Blog `json:"ancestors"` + Descendants []Blog `json:"descendants"` +} + +func main() { + config := getConfig() + + listener := listen(config.Listen, config.CertPath, config.KeyPath) + log.Println("Server successfully started") + log.Println("Server is listening at " + config.Listen) + + serve(listener, config.BaseURL, config.Title, config.HomeMessage) +} diff --git a/mastoUtil.go b/mastoUtil.go new file mode 100644 index 0000000..a59d795 --- /dev/null +++ b/mastoUtil.go @@ -0,0 +1,132 @@ +package main + +import ( + "net/http" + "log" + "encoding/json" + "io/ioutil" + "fmt" +) + +func getBlog(baseURL, account string) []Blog { + if baseURL == "" || account == "" { + log.Println("baseURL or account is empty") + return nil + } + + resp, err := http.Get(baseURL + "/api/v1/accounts/" + account + "/statuses?exclude_reblogs=true&exlude_replies=true") + if err != nil { + log.Println("Mastodon API request: %s", err) + return nil + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + log.Println("Mastodon API response: %s", resp.Status) + return nil + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("Mastodon response body: %s", err) + } + + var blogs []Blog + json.Unmarshal(body, &blogs) + + return blogs +} + +func getAccount(baseURL, accountId string) (Account, error) { + if baseURL == "" || accountId == "" { + log.Println("baseURL or account is empty") + return Account{}, fmt.Errorf("baseURL or account is empty") + } + + resp, err := http.Get(baseURL + "/api/v1/accounts/" + accountId) + if err != nil { + log.Println("Mastodon API request: %s", err) + return Account{}, fmt.Errorf("API request failed") + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + log.Println("Mastodon API response: %s", resp.Status) + return Account{}, fmt.Errorf("API response is not 200") + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("Mastodon response body: %s", err) + return Account{}, fmt.Errorf("Failed to read response") + } + + var account Account + json.Unmarshal(body, &account) + + return account, nil +} + +func getToot(baseURL, tootId string) (Blog, error) { + if baseURL == "" || tootId == "" { + log.Println("baseURL or tootID is empty") + return Blog{}, fmt.Errorf("baseURL or tootID is empty") + } + + resp, err := http.Get(baseURL + "/api/v1/statuses/" + tootId) + if err != nil { + log.Println("Mastodon API request: %s", err) + return Blog{}, fmt.Errorf("API request failed") + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + log.Println("Mastodon API response: %s", resp.Status) + return Blog{}, fmt.Errorf("API response is not 200") + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("Mastodon response body: %s", err) + return Blog{}, fmt.Errorf("Failed to read response") + } + + var toot Blog + json.Unmarshal(body, &toot) + + return toot, nil +} + +func getThread(baseURL, tootId string) (Thread, error) { + if baseURL == "" || tootId == "" { + log.Println("baseURL or tootID is empty") + return Thread{}, fmt.Errorf("baseURL or tootID is empty") + } + + resp, err := http.Get(baseURL + "/api/v1/statuses/" + tootId + "/context") + if err != nil { + log.Println("Mastodon API request: %s", err) + return Thread{}, fmt.Errorf("API request failed") + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + log.Println("Mastodon API response: %s", resp.Status) + return Thread{}, fmt.Errorf("API response is not 200") + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("Mastodon response body: %s", err) + return Thread{}, fmt.Errorf("Failed to read response") + } + + var thread Thread + json.Unmarshal(body, &thread) + + return thread, nil +} diff --git a/server.go b/server.go index e79a195..0c9bf91 100644 --- a/server.go +++ b/server.go @@ -5,84 +5,12 @@ import ( "net/url" "log" "net" - "net/http" "bufio" - "os" "strconv" "fmt" - "encoding/json" - "io/ioutil" "strings" - "html" - "regexp" ) -type Blog struct { - Id string `json:"id"` - Content string `json:"content"` - Date string `json:"created_at"` - Author Account `json:"account"` -} - -type Config struct { - Listen string `json:"listen"` - CertPath string `json:"cert_path"` - KeyPath string `json:"key_path"` - BaseURL string `json:"base_url"` - Title string `json:"title"` - HomeMessage string `json:"home_message"` -} - -type Account struct { - Id string `json:"id"` - Name string `json:"display_name"` - Url string `json:"url"` -} - -type Thread struct { - Ancestors []Blog `json:"ancestors"` - Descendants []Blog `json:"descendants"` -} - -func main() { - config := getConfig() - - listener := listen(config.Listen, config.CertPath, config.KeyPath) - log.Println("Server successfully started") - log.Println("Server is listening at " + config.Listen) - - serve(listener, config.BaseURL, config.Title, config.HomeMessage) -} - -func getConfig() Config { - configPath := os.Getenv("MASTOGEM_CONFIG_PATH") - if configPath == "" { - log.Println("MASTOGEM_CONFIG_PATH was not set, using default settings") - - config := Config{ - Listen: "127.0.0.1:1965", - CertPath: "cert.pem", - KeyPath: "key.rsa", - BaseURL: "https://mamot.fr", - Title: "MastoGem", - HomeMessage: "Welcome on MastoGem, this is a Mastodon proxy for Gemini. You can view the last 20 toots of a Mastodon account by providing its ID.", - } - - return config - } - - configFile, err := ioutil.ReadFile(configPath) - if err != nil { - log.Fatalln("config file: %s", err) - } - - var config Config - - json.Unmarshal(configFile, &config) - - return config -} - func listen(address, certFile, keyFile string) net.Listener { cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { @@ -307,152 +235,3 @@ func printThread(conn *tls.Conn, baseURL, tootID string) { } } } - -func removeHTMLTags(content string) string { - text := strings.ReplaceAll(content, "
", "") - text = strings.ReplaceAll(text, "
", "\n\n") - text = strings.ReplaceAll(text, "", "") + text = strings.ReplaceAll(text, "
", "\n\n") + text = strings.ReplaceAll(text, "