Difference between revisions of "Go openssl http api"
Jump to navigation
Jump to search
(Created page with "``` package main import ( "fmt" "io/ioutil" "net/http" "os" "os/exec" "github.com/labstack/echo/v4" ) func main() { e := echo.New() e.POST("/generate-certificate",...") |
|||
| Line 1: | Line 1: | ||
| + | ``` | ||
| + | openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj "/CN=example.com" -addext "subjectAltName = DNS:example.com,DNS:www.example.com" | ||
| + | |||
| + | ``` | ||
| + | |||
``` | ``` | ||
package main | package main | ||
Revision as of 14:12, 4 May 2024
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj "/CN=example.com" -addext "subjectAltName = DNS:example.com,DNS:www.example.com"
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.POST("/generate-certificate", func(c echo.Context) error {
// Generate a self-signed certificate using OpenSSL
cmd := exec.Command("openssl", "req", "-x509", "-newkey", "rsa:4096", "-keyout", "key.pem", "-out", "cert.pem", "-days", "365", "-subj", "/CN=example.com")
output, err := cmd.CombinedOutput()
if err != nil {
return c.String(http.StatusInternalServerError, fmt.Sprintf("Error generating certificate: %s", err))
}
return c.String(http.StatusOK, fmt.Sprintf("Certificate generated successfully:\n%s", output))
})
e.GET("/get-certificate/:filename", func(c echo.Context) error {
// Serve generated certificate files
filename := c.Param("filename")
c.Response().Header().Set(echo.HeaderContentType, "application/x-pem-file")
return c.File(filename)
})
e.Start(":8080")
}