diff --git a/config/cfg.go b/config/cfg.go
index 76c2d98..1ddb182 100644
--- a/config/cfg.go
+++ b/config/cfg.go
@@ -5,6 +5,8 @@ import "os"
type (
Config struct {
DatabaseURL string
+ VpubDatabaseURL string
+ VpubAESKey string
SessionKey string
Env string
CertFile string
@@ -22,6 +24,8 @@ type (
func New() *Config {
return &Config{
DatabaseURL: os.Getenv("DATABASE_URL"),
+ VpubDatabaseURL: os.Getenv("VPUB_DATABASE_URL"),
+ VpubAESKey: os.Getenv("VPUB_AES_KEY"),
SessionKey: os.Getenv("SESSION_KEY"),
Env: os.Getenv("ENV"),
CertFile: os.Getenv("CERT_FILE"),
diff --git a/main.go b/main.go
index 200c4ba..a36b1fa 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@ import (
"log"
"status/config"
"status/storage"
+ "status/vpub"
"status/web"
)
@@ -15,7 +16,11 @@ func main() {
log.Fatal(err)
}
data := storage.New(db)
+ v, err := vpub.New(cfg.VpubDatabaseURL, []byte(cfg.VpubAESKey))
+ if err != nil {
+ log.Fatal(err)
+ }
log.Fatal(
- web.Serve(data, cfg),
+ web.Serve(data, v, cfg),
)
}
diff --git a/vpub/vpub.go b/vpub/vpub.go
new file mode 100644
index 0000000..9a828a5
--- /dev/null
+++ b/vpub/vpub.go
@@ -0,0 +1,45 @@
+package vpub
+
+import (
+ "crypto/aes"
+ "database/sql"
+ "encoding/hex"
+ "fmt"
+)
+
+type Vpub struct {
+ db *sql.DB
+ key []byte
+}
+
+func New(databaseURL string, key []byte) (Vpub, error) {
+ db, err := sql.Open("postgres", databaseURL)
+ return Vpub{db: db, key: key}, err
+}
+
+func EncryptAES(key []byte, plaintext string) string {
+ c, err := aes.NewCipher(key)
+ if err != nil {
+ fmt.Println(err)
+ return ""
+ }
+ out := make([]byte, len(plaintext))
+ c.Encrypt(out, []byte(plaintext))
+ return hex.EncodeToString(out)
+}
+
+func (v Vpub) FindOrCreateKey(name string) string {
+ padded := fmt.Sprintf("%16s", name)
+ key := EncryptAES(v.key, padded)[0:20]
+
+ query := `
+ insert into keys (key) values ($1) ON CONFLICT do nothing
+ `
+
+ _, err := v.db.Exec(query, key[:20])
+ if err != nil {
+ fmt.Println(err)
+ }
+
+ return key
+}
diff --git a/web/handler/common.go b/web/handler/common.go
index 45a3bf9..932222b 100644
--- a/web/handler/common.go
+++ b/web/handler/common.go
@@ -27,7 +27,7 @@ var TplCommonMap = map[string]string{