Begin split into multiple files

This commit is contained in:
Romain de Laage 2021-03-02 20:31:38 +01:00
parent 66f6156ceb
commit 8f74708a44
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
4 changed files with 238 additions and 221 deletions

40
main.go Normal file
View File

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

132
mastoUtil.go Normal file
View File

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

221
server.go
View File

@ -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, "<p>", "")
text = strings.ReplaceAll(text, "</p>", "\n\n")
text = strings.ReplaceAll(text, "<br />", "\n")
text = strings.ReplaceAll(text, "<br>", "\n")
text = strings.ReplaceAll(text, "</a>", "")
text = strings.ReplaceAll(text, "</span>", "")
regexString := "<a( [^>]*)?>"
regex, err := regexp.Compile(regexString)
if err != nil {
log.Println("regex: %s", err)
return ""
}
text = regex.ReplaceAllLiteralString(text, "")
regexString = "<span( [^>]*)?>"
regex, err = regexp.Compile(regexString)
if err != nil {
log.Println("regex: %s", err)
return ""
}
text = regex.ReplaceAllLiteralString(text, "")
text = html.UnescapeString(text)
return text
}
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
}

66
util.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"html"
"regexp"
"strings"
"os"
"io/ioutil"
"encoding/json"
"log"
)
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: "certs/cert.pem",
KeyPath: "certs/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 removeHTMLTags(content string) string {
text := strings.ReplaceAll(content, "<p>", "")
text = strings.ReplaceAll(text, "</p>", "\n\n")
text = strings.ReplaceAll(text, "<br />", "\n")
text = strings.ReplaceAll(text, "<br>", "\n")
text = strings.ReplaceAll(text, "</a>", "")
text = strings.ReplaceAll(text, "</span>", "")
regexString := "<a( [^>]*)?>"
regex, err := regexp.Compile(regexString)
if err != nil {
log.Println("regex: %s", err)
return ""
}
text = regex.ReplaceAllLiteralString(text, "")
regexString = "<span( [^>]*)?>"
regex, err = regexp.Compile(regexString)
if err != nil {
log.Println("regex: %s", err)
return ""
}
text = regex.ReplaceAllLiteralString(text, "")
text = html.UnescapeString(text)
return text
}