Added configuration from environment variables
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
func addRoutes(
|
||||
mux *http.ServeMux,
|
||||
config *Config,
|
||||
) {
|
||||
// Static files
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", handlers.HandleStatic()))
|
||||
@@ -19,6 +20,6 @@ func addRoutes(
|
||||
mux.Handle("GET /about", handlers.HandlePage(page.About()))
|
||||
|
||||
// Login page and handlers
|
||||
mux.Handle("GET /login", handlers.HandleLoginPage())
|
||||
mux.Handle("GET /login", handlers.HandleLoginPage(config.TrustedHost))
|
||||
mux.Handle("POST /login", handlers.HandleLoginRequest())
|
||||
}
|
||||
|
||||
@@ -1,19 +1,50 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"projectreshoot/middleware"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
TrustedHost string
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
||||
func NewServer() http.Handler {
|
||||
func GetConfig() (*Config, error) {
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
fmt.Println(".env file not found.")
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
Host: os.Getenv("HOST"),
|
||||
Port: os.Getenv("PORT"),
|
||||
TrustedHost: os.Getenv("TRUSTED_HOST"),
|
||||
}
|
||||
if config.Host == "" {
|
||||
return nil, errors.New("Envar not set: HOST")
|
||||
}
|
||||
if config.Port == "" {
|
||||
return nil, errors.New("Envar not set: PORT")
|
||||
}
|
||||
if config.TrustedHost == "" {
|
||||
return nil, errors.New("Envar not set: TRUSTED_HOST")
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func NewServer(config *Config) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
mux,
|
||||
config,
|
||||
)
|
||||
var handler http.Handler = mux
|
||||
handler = middleware.Logging(handler)
|
||||
|
||||
Reference in New Issue
Block a user