完善 Scert 前后端与部署支持
This commit is contained in:
1 parent
afdb947bea
commit
7f0af703ed
85 files changed
+7530
-800
No files matched your search
@@ -0,0 +1,17 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandlerReturnsServiceUnavailableWhenRuntimeIsNil(t *testing.T) {
|
||||
var runtime *Runtime
|
||||
request := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
||||
response := httptest.NewRecorder()
|
||||
runtime.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("空运行时状态码错误:%d", response.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
type Runtime struct {
|
||||
core *app.App
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func Open(configPath, baseDir string) (*Runtime, error) {
|
||||
configPath = strings.TrimSpace(configPath)
|
||||
baseDir = strings.TrimSpace(baseDir)
|
||||
if configPath == "" {
|
||||
configPath = filepath.Join(defaultPaths(baseDir).Config, "scert.json")
|
||||
}
|
||||
if baseDir == "" {
|
||||
baseDir = filepath.Dir(filepath.Dir(configPath))
|
||||
}
|
||||
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
cfg = defaultConfig(baseDir)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
core, err := app.NewWithConfigPath(ctx, cfg, configPath)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
if core.Renewal != nil && cfg.SetupReady {
|
||||
core.Renewal.Start(ctx)
|
||||
}
|
||||
return &Runtime{core: core, cancel: cancel}, nil
|
||||
}
|
||||
|
||||
func (r *Runtime) Handler() http.Handler {
|
||||
if r == nil || r.core == nil || r.core.HTTP == nil {
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(writer, "Scert 核心服务未初始化", http.StatusServiceUnavailable)
|
||||
})
|
||||
}
|
||||
return r.core.HTTP.Handler()
|
||||
}
|
||||
|
||||
func (r *Runtime) Close() error {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
if r.cancel != nil {
|
||||
r.cancel()
|
||||
}
|
||||
if r.core != nil {
|
||||
return r.core.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultConfig(baseDir string) config.Config {
|
||||
cfg := config.Default(baseDir)
|
||||
if runtime.GOOS == "linux" && strings.TrimSpace(baseDir) != "" {
|
||||
install := filepath.Join(baseDir, "Scert")
|
||||
cfg.Paths.Install = install
|
||||
cfg.Paths.Certificate = filepath.Join(install, "ssl")
|
||||
cfg.Paths.Config = filepath.Join(install, "config")
|
||||
cfg.Paths.Data = filepath.Join(install, "data")
|
||||
cfg.Paths.Log = filepath.Join(install, "logs")
|
||||
cfg.Paths.Backup = filepath.Join(install, "backups")
|
||||
cfg.Database.DSN = filepath.Join(cfg.Paths.Data, "scert.db")
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func defaultPaths(baseDir string) config.PathsConfig {
|
||||
return defaultConfig(baseDir).Paths
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenCreatesUninitializedRuntime(t *testing.T) {
|
||||
baseDir := t.TempDir()
|
||||
runtime, err := Open(filepath.Join(baseDir, "config", "scert.json"), baseDir)
|
||||
if err != nil {
|
||||
t.Fatalf("打开桌面核心服务失败:%v", err)
|
||||
}
|
||||
defer runtime.Close()
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, "http://127.0.0.1/api/v1/setup/status", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.RemoteAddr = "127.0.0.1:1234"
|
||||
response := newRecordingResponse()
|
||||
runtime.Handler().ServeHTTP(response, request)
|
||||
if response.status != http.StatusOK {
|
||||
t.Fatalf("初始化状态接口状态码错误:%d,响应:%s", response.status, response.body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenRejectsInvalidConfiguration(t *testing.T) {
|
||||
baseDir := t.TempDir()
|
||||
configPath := filepath.Join(baseDir, "config", "scert.json")
|
||||
if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(configPath, []byte("{invalid"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Open(configPath, baseDir); err == nil {
|
||||
t.Fatal("无效配置不应打开成功")
|
||||
}
|
||||
}
|
||||
|
||||
type recordingResponse struct {
|
||||
header http.Header
|
||||
status int
|
||||
body string
|
||||
}
|
||||
|
||||
func newRecordingResponse() *recordingResponse {
|
||||
return &recordingResponse{header: make(http.Header)}
|
||||
}
|
||||
|
||||
func (r *recordingResponse) Header() http.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *recordingResponse) WriteHeader(status int) {
|
||||
r.status = status
|
||||
}
|
||||
|
||||
func (r *recordingResponse) Write(body []byte) (int, error) {
|
||||
if r.status == 0 {
|
||||
r.status = http.StatusOK
|
||||
}
|
||||
r.body += string(body)
|
||||
return len(body), nil
|
||||
}
|
||||
Reference in new issue
Block a user