package main

import (
	"fmt"
	"log"
	"net/http"
	"runtime/debug"
)

var (
	version   = "dev"
	buildTime = "unknown"
	gitCommit = "unknown"
)

func main() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/version", versionHandler)

	fmt.Println("Server starting on :8080")
	fmt.Printf("Version: %s\n", version)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello from Go! Modify this message and watch air reload automatically.\n")
}

func versionHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")

	fmt.Fprintf(w, "Version: %s\n", version)
	fmt.Fprintf(w, "Build Time: %s\n", buildTime)
	fmt.Fprintf(w, "Git Commit: %s\n", gitCommit)

	if info, ok := debug.ReadBuildInfo(); ok {
		fmt.Fprintf(w, "Go Version: %s\n", info.GoVersion)
		fmt.Fprintf(w, "Module: %s\n", info.Main.Path)

		for _, setting := range info.Settings {
			if setting.Key == "vcs.revision" || setting.Key == "vcs.time" {
				fmt.Fprintf(w, "%s: %s\n", setting.Key, setting.Value)
			}
		}
	}
}
