From 6967b5606a372a1b6a2970332950905f3ca32f69 Mon Sep 17 00:00:00 2001 From: Romain de Laage Date: Sun, 28 Feb 2021 18:41:48 +0100 Subject: [PATCH] Add home page --- config.json | 4 +++- server.go | 29 +++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/config.json b/config.json index 08873f0..9aca3b3 100644 --- a/config.json +++ b/config.json @@ -2,5 +2,7 @@ "listen": "0.0.0.0:1965", "cert_path": "cert.pem", "key_path": "key.rsa", - "base_url": "https://mamot.fr" + "base_url": "https://mamot.fr", + "title": "MastoGem", + "home_message": "Welcome on MastoGem, a Mastodon proxy for Gemini.\\nYou can view the last 20 toots of a Mastodon account by providing its id, for example:\\n=> gemini://localhost/310515 My Mastodon account" } diff --git a/server.go b/server.go index b9fde0f..6ee4175 100644 --- a/server.go +++ b/server.go @@ -24,10 +24,12 @@ type Blog struct { } type Config struct { - Listen string `json:"listen"` - CertPath string `json:"cert_path"` - KeyPath string `json:"key_path"` - BaseURL string `json:"base_url"` + 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 { @@ -43,7 +45,7 @@ func main() { log.Println("Server successfully started") log.Println("Server is listening at " + config.Listen) - serve(listener, config.BaseURL) + serve(listener, config.BaseURL, config.Title, config.HomeMessage) } func getConfig() Config { @@ -56,6 +58,8 @@ func getConfig() Config { 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 @@ -93,14 +97,14 @@ func listen(address, certFile, keyFile string) net.Listener { return listener } -func serve(listener net.Listener, baseURL string) { +func serve(listener net.Listener, baseURL, title, home_message string) { for { conn, err := listener.Accept() if err != nil { log.Println(err) } - go handleConn(conn.(*tls.Conn), baseURL) + go handleConn(conn.(*tls.Conn), baseURL, title, home_message) } } @@ -132,7 +136,7 @@ func getPath(conn *tls.Conn) (string, error) { return parsedURL.Path, nil } -func handleConn(conn *tls.Conn, baseURL string) { +func handleConn(conn *tls.Conn, baseURL, title, home_message string) { defer conn.Close() path, err := getPath(conn) @@ -148,6 +152,15 @@ func handleConn(conn *tls.Conn, baseURL string) { path = path[1:] + if path == "" { + _, err = fmt.Fprintf(conn, "20 text/gemini\r\n# " + title + "\n\n" + home_message) + if err != nil { + log.Println("send error: %s", err) + return + } + return + } + _, err = strconv.ParseUint(path, 10, 64) if err != nil { log.Println("invalid request: %s", err)