92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
s3 "github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
//go:embed logo.svg
|
|
var logoFile embed.FS
|
|
|
|
var (
|
|
s3Client *s3.Client
|
|
bucketName = "terraform-registry"
|
|
hostname = os.Getenv("REGISTRY_HOSTNAME")
|
|
s3Prefix = os.Getenv("S3_PREFIX")
|
|
)
|
|
|
|
const VERSION = "0.0.2"
|
|
|
|
func main() {
|
|
if hostname == "" {
|
|
hostname = "localhost:5000"
|
|
}
|
|
if s3Prefix == "" {
|
|
s3Prefix = hostname
|
|
}
|
|
|
|
endpoint := os.Getenv("S3_ENDPOINT")
|
|
accessKeyID := os.Getenv("S3_ACCESS_KEY")
|
|
secretAccessKey := os.Getenv("S3_SECRET_KEY")
|
|
|
|
if os.Getenv("S3_BUCKET") != "" {
|
|
bucketName = os.Getenv("S3_BUCKET")
|
|
}
|
|
|
|
var err error
|
|
useSSL := os.Getenv("S3_USE_SSL") != "false"
|
|
log.Printf("S3 connecting: endpoint=%s bucket=%s useSSL=%v", endpoint, bucketName, useSSL)
|
|
s3Client, err = s3.New(endpoint, &s3.Options{
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
Secure: useSSL,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("S3 init failed: %v", err)
|
|
}
|
|
|
|
http.HandleFunc("/.well-known/terraform.json", discoveryHandler)
|
|
http.HandleFunc("/v1/providers/", router)
|
|
http.HandleFunc("/v1/proxy", proxyHandler)
|
|
http.HandleFunc("/docs/", docsHandler)
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(logoFile))))
|
|
http.Handle("/", http.HandlerFunc(rootHandler))
|
|
http.HandleFunc("/healthz", healthzHandler)
|
|
http.HandleFunc("/readyz", readyzHandler)
|
|
http.HandleFunc("/debug", debugHandler)
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "5000"
|
|
}
|
|
|
|
srv := &http.Server{Addr: ":" + port}
|
|
|
|
go func() {
|
|
log.Printf("Starting Registry Service on :%s (Bucket: %s, Endpoint: %s)\n", port, bucketName, endpoint)
|
|
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
|
log.Fatalf("HTTP server error: %v", err)
|
|
}
|
|
}()
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
log.Println("Shutting down server...")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
log.Fatalf("Server forced to shutdown: %v", err)
|
|
}
|
|
log.Println("Server stopped")
|
|
}
|