2025-08-06 22:31:43 +05:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2026-03-11 16:00:34 +05:00
|
|
|
"log"
|
|
|
|
|
"log/slog"
|
2025-08-06 22:31:43 +05:00
|
|
|
"net/http"
|
2026-03-11 16:00:34 +05:00
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2025-08-06 22:31:43 +05:00
|
|
|
|
2026-03-11 16:00:34 +05:00
|
|
|
controllerHkp "hkp-clavis/internal/controller/hkp"
|
|
|
|
|
"hkp-clavis/internal/model"
|
|
|
|
|
"hkp-clavis/internal/repo/storage/postgresql"
|
|
|
|
|
storageService "hkp-clavis/internal/service/storage/serv"
|
2025-08-06 22:31:43 +05:00
|
|
|
|
|
|
|
|
hkp "github.com/emersion/go-openpgp-hkp"
|
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2026-03-11 16:00:34 +05:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
"github.com/lmittmann/tint"
|
2025-08-06 22:31:43 +05:00
|
|
|
)
|
|
|
|
|
|
2026-03-11 16:00:34 +05:00
|
|
|
var Logger *slog.Logger
|
2025-08-06 22:31:43 +05:00
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
2026-03-11 16:00:34 +05:00
|
|
|
Logger = slog.New(tint.NewHandler(os.Stdout, nil))
|
|
|
|
|
|
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
|
log.Println("No .env file found, reading from system environment")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listenAddress := os.Getenv("LISTEN_ADDRESS")
|
|
|
|
|
listenPort := os.Getenv("LISTEN_PORT")
|
|
|
|
|
|
|
|
|
|
listenAddress = fmt.Sprintf("%s:%s", listenAddress, listenPort)
|
|
|
|
|
|
|
|
|
|
pgUser := os.Getenv("POSTGRES_USER")
|
|
|
|
|
pgPassword := os.Getenv("POSTGRES_PASSWORD")
|
|
|
|
|
pgAddress := os.Getenv("POSTGRES_ADDRESS")
|
|
|
|
|
pgPort := os.Getenv("POSTGRES_PORT")
|
|
|
|
|
pgDatabase := os.Getenv("POSTGRES_DATABASE")
|
|
|
|
|
|
|
|
|
|
// By default all uids in key does not verify
|
|
|
|
|
// For dev/debug you can set it to true
|
|
|
|
|
verify := os.Getenv("VERIFY_DEFAULT")
|
|
|
|
|
defaultVerify := getEnvAsBool(verify, true)
|
|
|
|
|
|
|
|
|
|
pgUri := fmt.Sprintf(
|
|
|
|
|
"postgresql://%s:%s@%s:%s/%s",
|
|
|
|
|
pgUser,
|
|
|
|
|
pgPassword,
|
|
|
|
|
pgAddress,
|
|
|
|
|
pgPort,
|
|
|
|
|
pgDatabase)
|
|
|
|
|
|
|
|
|
|
// For memory allocation controller
|
|
|
|
|
keyMngr := model.NewKeyManager()
|
|
|
|
|
|
|
|
|
|
dbpool, err := pgxpool.New(context.TODO(), pgUri)
|
2025-08-06 22:31:43 +05:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 16:00:34 +05:00
|
|
|
db := postgresql.New(context.TODO(), dbpool, keyMngr)
|
|
|
|
|
storage := storageService.New(db, keyMngr, defaultVerify)
|
2025-08-06 22:31:43 +05:00
|
|
|
|
2026-03-11 16:00:34 +05:00
|
|
|
controller, err := controllerHkp.New(storage)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2025-08-06 22:31:43 +05:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 16:00:34 +05:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
hkpHandler := hkp.Handler{
|
|
|
|
|
Adder: controller,
|
|
|
|
|
Lookuper: controller,
|
|
|
|
|
}
|
|
|
|
|
mux.HandleFunc("/pks/lookup", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/pgp-keys")
|
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
|
|
hkpHandler.ServeHTTP(w, r)
|
2025-08-06 22:31:43 +05:00
|
|
|
})
|
2026-03-11 16:00:34 +05:00
|
|
|
mux.HandleFunc("/pks/add", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
hkpHandler.ServeHTTP(w, r)
|
2025-08-06 22:31:43 +05:00
|
|
|
})
|
2026-03-11 16:00:34 +05:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2025-08-06 22:31:43 +05:00
|
|
|
fmt.Fprint(w, "Index Page")
|
|
|
|
|
})
|
2026-03-11 16:00:34 +05:00
|
|
|
|
|
|
|
|
handler := LogMiddleware(mux)
|
|
|
|
|
|
|
|
|
|
Logger.Info("Server is listening on address: %s", handler)
|
|
|
|
|
http.ListenAndServe(listenAddress, handler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getEnvAsBool(name string, defaultVal bool) bool {
|
|
|
|
|
valStr, exists := os.LookupEnv(name) // Use LookupEnv for better checking
|
|
|
|
|
if !exists || valStr == "" {
|
|
|
|
|
return defaultVal
|
|
|
|
|
}
|
|
|
|
|
if val, err := strconv.ParseBool(valStr); err == nil {
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
return defaultVal
|
2025-08-06 22:31:43 +05:00
|
|
|
}
|