~yerinalexey/gobin

3ca827036bd6322c25e0d050e69cae68eadf3e0c — Alexey Yerin 5 months ago 7626ff7
Use Host: header instead of base URL
4 files changed, 19 insertions(+), 13 deletions(-)

M config.example.toml
M config/config.go
M main.go
M paste/paste.go
M config.example.toml => config.example.toml +4 -4
@@ 16,7 16,7 @@ port = 4000
templateRoot = "./templates"
staticRoot = "./static"

# Base URL for permalinks
#
# FIXME: This setting will be gone at some point
baseUrl = "http://localhost"
# Assume http:// on permalinks, setting this to true is not recommended
# in production environments
#insecure = true
insecure = false

M config/config.go => config/config.go +1 -5
@@ 16,7 16,7 @@ type Config struct {
	TemplateRoot string
	StaticRoot string

	BaseUrl string
	Insecure bool
}

func New(configFile string) (*Config, error) {


@@ 41,10 41,6 @@ func New(configFile string) (*Config, error) {
		return nil, errors.New("Port can't be 0")
	}

	if cfg.BaseUrl == "" {
		return nil, errors.New("BaseUri should be set")
	}

	if cfg.TemplateRoot == "" {
		return nil, errors.New("TemplateRoot should be set")
	}

M main.go => main.go +1 -2
@@ 83,8 83,7 @@ func main() {
			return
		}

		// FIXME: Better way to create a link
		link := fmt.Sprintf("%s/paste/%d", config.BaseUrl, id)
		link := paste.CreatePermalink(id, req.Host, config.Insecure)

		tmpl.ExecuteTemplate(w, "saved", link)
	}).Methods("POST")

M paste/paste.go => paste/paste.go +13 -2
@@ 2,6 2,7 @@ package paste

import (
	"context"
	"fmt"
	"strconv"
	"strings"



@@ 16,9 17,9 @@ type Paste struct {
}

type PublicPaste struct {
	Id int
	Id      int
	Summary string
	Link string
	Link    string
}

// Get paste by it's id. Returns nil and error if paste does not exist.


@@ 81,3 82,13 @@ func GetPublicPastes(pool *pgxpool.Pool, router *mux.Router) ([]PublicPaste, err

	return pastes, nil
}

// CreatePermalink creates an absolute link to a paste
func CreatePermalink(id int, host string, insecure bool) string {
	protocol := "https"
	if insecure {
		protocol = "http"
	}

	return fmt.Sprintf("%s://%s/paste/%d", protocol, host, id)
}