96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Terraform Registry Protocol Structs
|
|
type Discovery struct {
|
|
ProvidersV1 string `json:"providers.v1"`
|
|
}
|
|
|
|
type VersionList struct {
|
|
ID string `json:"id"`
|
|
Versions []Version `json:"versions"`
|
|
Warnings []string `json:"warnings"`
|
|
}
|
|
|
|
type Version struct {
|
|
Version string `json:"version"`
|
|
Protocols []string `json:"protocols"`
|
|
Platforms []Platform `json:"platforms"`
|
|
}
|
|
|
|
type Platform struct {
|
|
OS string `json:"os"`
|
|
Arch string `json:"arch"`
|
|
}
|
|
|
|
type DownloadResponse struct {
|
|
Protocols []string `json:"protocols"`
|
|
OS string `json:"os"`
|
|
Arch string `json:"arch"`
|
|
Filename string `json:"filename"`
|
|
DownloadURL string `json:"download_url"`
|
|
ShasumsURL string `json:"shasums_url"`
|
|
ShasumsSignatureURL string `json:"shasums_signature_url"`
|
|
Shasum string `json:"shasum"`
|
|
SigningKeys SigningKeys `json:"signing_keys"`
|
|
}
|
|
|
|
type SigningKeys struct {
|
|
GPGPublicKeys []GPGPublicKey `json:"gpg_public_keys"`
|
|
}
|
|
|
|
type GPGPublicKey struct {
|
|
KeyID string `json:"key_id"`
|
|
ASCIIArmor string `json:"ascii_armor"`
|
|
}
|
|
|
|
func discoveryHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(Discovery{ProvidersV1: "/v1/providers/"})
|
|
}
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
tmpl, err := templateFS.ReadFile("templates/index.html")
|
|
if err != nil {
|
|
log.Printf("root: template read error: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
html := strings.Replace(string(tmpl), "{{.Version}}", VERSION, 1)
|
|
fmt.Fprint(w, html)
|
|
}
|
|
|
|
func healthzHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("ok"))
|
|
}
|
|
|
|
func readyzHandler(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
|
defer cancel()
|
|
_, err := s3Client.BucketExists(ctx, bucketName)
|
|
if err != nil {
|
|
msg := fmt.Sprintf("s3 unreachable: %v", err)
|
|
log.Printf("readyz: %s", msg)
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
w.Write([]byte(msg))
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("ok"))
|
|
}
|