~sotirisp/kindleto

df38fb868306e5c7f9b9600e5b2a01b402f1f7cd — Sotiris Papatheodorou 1 year, 9 months ago 5bc47a2
Add homepage setting

It's now possible to set the homepage to an arbitrary URL.
4 files changed, 23 insertions(+), 27 deletions(-)

M handlers.go
M kindleto.go
M settings/settings.go
M templates/template_strings.go
M handlers.go => handlers.go +14 -11
@@ 209,10 209,9 @@ func proxy(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/?url="+gmi.QueryEscape(targetURL), http.StatusFound)
	}

	var u *url.URL
	var err error = nil
	var u_string string
	if r.URL.Query().Get("url") == "" {
		if homepage == nil {
		if settings.Current.Homepage == "" {
			err := templates.Templates.ExecuteTemplate(w, "home.html.tmpl", templates.TemplateData{})
			if err != nil {
				log.Println("proxy:", err)


@@ 220,16 219,18 @@ func proxy(w http.ResponseWriter, r *http.Request) {
			}
			return
		} else {
			u = homepage
			u_string = settings.Current.Homepage
		}
	} else {
		u, err = url.Parse(r.URL.Query().Get("url"))
		if err != nil {
			err = fmt.Errorf("proxy: failed to parse URL: %v", err)
			log.Println(err)
			http.Error(w, err.Error(), 500)
			return
		}
		u_string = r.URL.Query().Get("url")
	}

	u, err := url.Parse(u_string)
	if err != nil {
		err = fmt.Errorf("proxy: failed to parse URL: %v", err)
		log.Println(err)
		http.Error(w, err.Error(), 500)
		return
	}

	switch u.Scheme {


@@ 330,6 331,8 @@ func handleSettings(w http.ResponseWriter, r *http.Request) {
				log.Println("handleSettings: writing settings to", settings.SettingsFile)
			}

			settings.Current.Homepage = r.FormValue("homepage")

			settings.Current.EnableClientCerts = r.FormValue("clientcerts") == "on"

			settings.Current.EnableTofu = r.FormValue("tofu") == "on"

M kindleto.go => kindleto.go +0 -15
@@ 9,18 9,15 @@ import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"strconv"

	"git.sr.ht/~sotirisp/kindleto/certificates"
	"git.sr.ht/~sotirisp/kindleto/settings"
	"git.sr.ht/~sotirisp/kindleto/tofu"
	"git.sr.ht/~sotirisp/kindleto/util"
)

var version string = "v0.6.1"
var homepage *url.URL

func usage() {
	fmt.Printf("Usage: %s [-v] [config]\n", os.Args[0])


@@ 55,18 52,6 @@ func main() {
		settings.Current.LogLevel = optLoglevel
	}

	{
		homepageFile := util.KindletoDir() + "/index.gmi"
		s, err := os.Stat(homepageFile)
		if err == nil && !s.IsDir() {
			homepage, err = url.Parse("file://" + homepageFile)
			if err != nil {
				homepage = nil
				log.Println("main: failed to parse homepage URL:", err)
			}
		}
	}

	certificates.LoadClientCerts()
	tofu.LoadKnownHosts()


M settings/settings.go => settings/settings.go +5 -0
@@ 13,6 13,10 @@ import (
)

type Settings struct {
	// The URL loaded when Kindleto starts or when the "Home" button is
	// pressed.
	Homepage string

	// Enable sending client certificates to servers requesting them.
	EnableClientCerts bool



@@ 40,6 44,7 @@ type Settings struct {
// NewSettings returns a Settings struct initialized to the default values.
func NewSettings() *Settings {
	return &Settings{
		Homepage:          "",
		EnableClientCerts: true,
		EnableTofu:        true,
		DefaultLang:       "en",

M templates/template_strings.go => templates/template_strings.go +4 -1
@@ 141,6 141,10 @@ Valid until: {{.NotAfter}}<br/>
<h1>Settings</h1>
<form action="/settings" method="POST">

<h2>Homepage</h2>
<p>The URL loaded when Kindleto starts or when the "Home" button is pressed.</p>
<input type="url" id="homepage" name="homepage" value="{{.Settings.Homepage}}">

<h2>Enable client certificates</h2>
<p>Enable sending TLS client certificates to servers that request them.</p>
<input type="checkbox" id="clientcerts" name="clientcerts" {{if .Settings.EnableClientCerts}}checked{{end}}>


@@ 222,7 226,6 @@ const Help string = `{{define "title"}}Kindleto help{{end}}
<div class="error">{{.Error}}</div>
{{end}}
<h1>Kindleto {{.Version}} help</h1>
<p>The Kindleto configuration files are in the <code>kindleto</code> directory inside the Kindle's USB storage. You can customize the homepage by creating the <a href="/?url=gemini%3A%2F%2Fgemini.circumlunar.space%2Fdocs%2Fgemtext.gmi">"text/gemini"</a> file <code>kindleto/index.gmi</code>.</p>
<p>Gemini client certificates are stored in <code>kindleto/certificates/</code>. The certificates and private key files should be named after the host they apply to and have a <code>.crt</code> and <code>.key</code> suffix respectively. For example:</p>
<pre><code>
kindleto/certificates/