~yerinalexey/gobin

b91ecee071be538cee4dd27628186ed4deb30a49 — Alexey Yerin 5 months ago e05e232
refactor: move configuration to config module
2 files changed, 65 insertions(+), 35 deletions(-)

A config/config.go
M main.go
A config/config.go => config/config.go +54 -0
@@ 0,0 1,54 @@
// Application configuration

package config

import (
	"os"
	"errors"
	"strconv"
)

type Config struct {
	DbUri string
	Port int

	TemplateRoot string
	StaticRoot string

	BaseUrl string
}

func New() (*Config, error) {
	db_uri := os.Getenv("DATABASE_URI")
	if db_uri == "" {
		return nil, errors.New("DATABASE_URI is not set")
	}

	var port int = 4000

	if v := os.Getenv("PORT"); v != "" {
		var err error
		port, err = strconv.Atoi(v)

		if err != nil {
			return nil, errors.New("PORT is not an integer")
		}
	}

	base_url := os.Getenv("BASE_URL")
	if base_url == "" {
		base_url = "http://localhost:4000"
	}

	template_root := os.Getenv("TEMPLATE_ROOT")
	if template_root == "" {
		template_root = "./templates"
	}

	static_root := os.Getenv("STATIC_ROOT")
	if template_root == "" {
		static_root = "./static"
	}

	return &Config{db_uri, port, template_root, static_root, base_url}, nil
}

M main.go => main.go +11 -35
@@ 21,10 21,10 @@ import (
	"html/template"
	"log"
	"net/http"
	"os"
	"path"
	"strconv"

	"git.sr.ht/~yerinalexey/gobin/config"
	"git.sr.ht/~yerinalexey/gobin/paste"

	"github.com/gorilla/mux"


@@ 32,43 32,19 @@ import (
)

func main() {
	db_uri := os.Getenv("DATABASE_URI")
	if db_uri == "" {
		log.Fatalln("Can't connect to the database: DATABASE_URI is not set")
	config, err := config.New()

	if err != nil {
		log.Fatalln("Invalid configuration:", err)
	}

	pool, err := pgxpool.Connect(context.Background(), db_uri)
	pool, err := pgxpool.Connect(context.Background(), config.DbUri)
	if err != nil {
		log.Fatalln("Can't connect to the database: ", err)
	}
	defer pool.Close()

	var port int = 4000

	if v := os.Getenv("PORT"); v != "" {
		port, err = strconv.Atoi(v)

		if err != nil {
			log.Fatalln("PORT is not an integer")
		}
	}

	base_url := os.Getenv("BASE_URL")
	if base_url == "" {
		base_url = "http://localhost:4000"
	}

	template_root := os.Getenv("TEMPLATE_ROOT")
	if template_root == "" {
		template_root = "./templates"
	}

	static_root := os.Getenv("STATIC_ROOT")
	if template_root == "" {
		static_root = "./static"
	}

	tmpl := template.Must(template.ParseGlob(path.Join(template_root, "*.html")))
	tmpl := template.Must(template.ParseGlob(path.Join(config.TemplateRoot, "*.html")))

	r := mux.NewRouter()
	r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {


@@ 104,7 80,7 @@ func main() {
		}

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

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


@@ 135,7 111,7 @@ func main() {
	}).Name("paste")

	// Static files
	fs := http.FileServer(http.Dir(static_root))
	fs := http.FileServer(http.Dir(config.StaticRoot))
	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))

	// Not found page


@@ 144,6 120,6 @@ func main() {
		tmpl.ExecuteTemplate(w, "not-found", nil)
	})

	log.Printf("Staring on port %d\n", port)
	log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%d", port), r))
	log.Printf("Staring on port %d\n", config.Port)
	log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), r))
}