Files
linuxservice/main.go

122 lines
3.2 KiB
Go

package main
import (
"html/template"
"log"
"net/http"
"os"
)
// Configuration holds all website configuration
type Config struct {
CompanyName string
KVK string
Email string
Phone string
Port string
}
// PageData holds data for template rendering
type PageData struct {
CompanyName string
Title string
KVK string
Email string
Phone string
}
// Server holds the application state
type Server struct {
config Config
templates *template.Template
}
// getEnv returns environment variable or default value
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// NewServer creates a new server instance
func NewServer() *Server {
// Configuration - can be overridden with environment variables
config := Config{
CompanyName: getEnv("COMPANY_NAME", "Hogeland Linux"),
KVK: getEnv("KVK", "12345678"), // Replace with actual KVK number
Email: getEnv("EMAIL", "info@hogelandlinux.nl"),
Phone: getEnv("PHONE", "+31 6 12345678"),
Port: ":" + getEnv("PORT", "8080"),
}
// Parse templates with error handling
templates, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalf("Failed to parse templates: %v", err)
}
return &Server{
config: config,
templates: templates,
}
}
// createPageData creates PageData with the given title
func (s *Server) createPageData(title string) PageData {
return PageData{
CompanyName: s.config.CompanyName,
Title: title,
KVK: s.config.KVK,
Email: s.config.Email,
Phone: s.config.Phone,
}
}
// renderTemplate renders a template with error handling
func (s *Server) renderTemplate(w http.ResponseWriter, templateName string, data PageData) {
if err := s.templates.ExecuteTemplate(w, templateName, data); err != nil {
log.Printf("Template execution error: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
// homeHandler handles the home page
func (s *Server) homeHandler(w http.ResponseWriter, r *http.Request) {
data := s.createPageData("Linux Migratie Service - Uw Computer Nieuw Leven Geven")
s.renderTemplate(w, "index.html", data)
}
// contactHandler handles the contact page
func (s *Server) contactHandler(w http.ResponseWriter, r *http.Request) {
data := s.createPageData("Contact - " + s.config.CompanyName)
s.renderTemplate(w, "contact.html", data)
}
// healthHandler provides a simple health check endpoint
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"healthy","service":"linuxservice"}`))
}
// setupRoutes configures all HTTP routes
func (s *Server) setupRoutes() {
// Static files
fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
// Page routes
http.HandleFunc("/", s.homeHandler)
http.HandleFunc("/contact", s.contactHandler)
http.HandleFunc("/health", s.healthHandler)
}
func main() {
server := NewServer()
server.setupRoutes()
log.Printf("Server starting on %s", server.config.Port)
log.Fatal(http.ListenAndServe(server.config.Port, nil))
}