package main import ( "database/sql" "embed" "fmt" "html/template" "io" "io/fs" "log" "net" "net/http" "os" "strings" "time" _ "github.com/mattn/go-sqlite3" ) //go:embed templates/*.tmpl var tmplFS embed.FS //go:embed *.svg *.css var static embed.FS var templates *template.Template var devMode bool var db *sql.DB type Pop struct { PopID int City string Country string Lat float64 Lon float64 RegionName string IP string DNS string Status string Provider string } type PageData struct { Page string Title string Desc string ActiveNav string Pops []Pop } func loadTemplates() { if devMode { templates = template.Must(template.ParseGlob("templates/*.tmpl")) } else { sub, _ := fs.Sub(tmplFS, "templates") templates = template.Must(template.ParseFS(sub, "*.tmpl")) } } func loadPops() []Pop { rows, err := db.Query("SELECT pop_id, city, country, lat, lon, region_name, ip, dns, status, provider FROM pops ORDER BY pop_id") if err != nil { log.Printf("pops query error: %v", err) return nil } defer rows.Close() var pops []Pop for rows.Next() { var p Pop if err := rows.Scan(&p.PopID, &p.City, &p.Country, &p.Lat, &p.Lon, &p.RegionName, &p.IP, &p.DNS, &p.Status, &p.Provider); err != nil { log.Printf("pops scan error: %v", err) continue } pops = append(pops, p) } return pops } func render(w http.ResponseWriter, data PageData) { if devMode { loadTemplates() } w.Header().Set("Content-Type", "text/html; charset=utf-8") if err := templates.ExecuteTemplate(w, "base.tmpl", data); err != nil { log.Printf("template error: %v", err) http.Error(w, "Internal error", 500) } } func geoHandler(w http.ResponseWriter, r *http.Request) { ip := r.Header.Get("X-Forwarded-For") if ip == "" { ip = r.RemoteAddr } if i := strings.LastIndex(ip, ":"); i >= 0 { ip = ip[:i] } ip = strings.Trim(ip, "[]") resp, err := http.Get("https://ipapi.co/" + ip + "/json/") if err != nil { http.Error(w, `{"error":"geo failed"}`, 502) return } defer resp.Body.Close() w.Header().Set("Content-Type", "application/json") io.Copy(w, resp.Body) } func main() { if _, err := os.Stat("templates"); err == nil { devMode = true log.Println("dev mode: templates loaded from disk") } loadTemplates() var err error db, err = sql.Open("sqlite3", "clavitor.db?mode=ro") if err != nil { log.Fatalf("failed to open clavitor.db: %v", err) } defer db.Close() port := os.Getenv("PORT") if port == "" { port = "8099" } http.HandleFunc("/geo", geoHandler) http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { host := r.URL.Query().Get("host") if host == "" { http.Error(w, `{"error":"missing host"}`, 400) return } start := time.Now() conn, err := net.DialTimeout("tcp", host+":1984", 5*time.Second) if err != nil { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"error":"unreachable"}`)) return } conn.Close() ms := time.Since(start).Milliseconds() w.Header().Set("Content-Type", "application/json") fmt.Fprintf(w, `{"ms":%d}`, ms) }) http.HandleFunc("/hosted", func(w http.ResponseWriter, r *http.Request) { data := PageData{Page: "hosted", Title: "clavitor — Hosted", ActiveNav: "hosted"} data.Pops = loadPops() render(w, data) }) http.HandleFunc("/install", func(w http.ResponseWriter, r *http.Request) { render(w, PageData{Page: "install", Title: "Self-host — clavitor", Desc: "Self-host clavitor in 30 seconds. One binary, no dependencies.", ActiveNav: "install"}) }) http.HandleFunc("/pricing", func(w http.ResponseWriter, r *http.Request) { render(w, PageData{Page: "pricing", Title: "Pricing — clavitor", Desc: "Free self-hosted or $12/year hosted (launch price). No tiers, no per-seat, no contact sales.", ActiveNav: "pricing"}) }) http.HandleFunc("/privacy", func(w http.ResponseWriter, r *http.Request) { render(w, PageData{Page: "privacy", Title: "Privacy Policy — clavitor"}) }) http.HandleFunc("/terms", func(w http.ResponseWriter, r *http.Request) { render(w, PageData{Page: "terms", Title: "Terms of Service — clavitor"}) }) http.HandleFunc("/sources", func(w http.ResponseWriter, r *http.Request) { render(w, PageData{Page: "sources", Title: "Sources — clavitor"}) }) http.HandleFunc("/styleguide", func(w http.ResponseWriter, r *http.Request) { render(w, PageData{Page: "styleguide", Title: "clavitor — Styleguide"}) }) http.HandleFunc("/glass", func(w http.ResponseWriter, r *http.Request) { data := PageData{Page: "glass", Title: "Looking Glass — clavitor"} data.Pops = loadPops() render(w, data) }) // Catch-all: index page at "/" or static files or .html redirects http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { render(w, PageData{Page: "index", Title: "clavitor — AI-native password manager", Desc: "Field-level encryption for password managers that live alongside AI assistants. Your AI gets what it needs. Your secrets stay yours."}) return } // Redirect old .html URLs to clean paths if strings.HasSuffix(r.URL.Path, ".html") { clean := strings.TrimSuffix(r.URL.Path, ".html") if clean == "/index" { clean = "/" } http.Redirect(w, r, clean, http.StatusMovedPermanently) return } http.FileServer(http.FS(static)).ServeHTTP(w, r) }) log.Printf("clavitor-web starting on :%s", port) if err := http.ListenAndServe(":"+port, nil); err != nil { log.Fatal(err) } }