58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"scm/crt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func GetCerts(w http.ResponseWriter, r *http.Request) {
|
|
var certs_info []map[string]any
|
|
entires, err := os.ReadDir(os.Getenv("CERTS_PATH"))
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
for _, entry := range entires {
|
|
if entry.IsDir() {
|
|
if entry.Name() == ".git" || entry.Name() == "root" {
|
|
continue
|
|
}
|
|
id, err := uuid.NewRandom()
|
|
if err != nil {
|
|
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
|
return
|
|
}
|
|
certdata, err := os.ReadFile(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
|
if err != nil {
|
|
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
|
return
|
|
}
|
|
keydata, err := os.ReadFile(fmt.Sprintf("%s/%s/%s.key", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
|
if err != nil {
|
|
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
|
return
|
|
}
|
|
new_cert := make(map[string]any)
|
|
new_cert["id"] = id.String()
|
|
new_cert["domain"] = entry.Name()
|
|
new_cert["href"] = fmt.Sprintf("https://%s", entry.Name())
|
|
new_cert["cert"] = string(certdata)
|
|
new_cert["key"] = string(keydata)
|
|
new_cert["certinfo"] = crt.ParseCert(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
|
certs_info = append(certs_info, new_cert)
|
|
}
|
|
}
|
|
jsondata, err := json.Marshal(certs_info)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(jsondata)
|
|
}
|