mirror of
https://github.com/Xevion/glance.git
synced 2025-12-18 04:12:10 -06:00
Restructure & refactor codebase
This commit is contained in:
@@ -2,9 +2,14 @@ package glance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var buildVersion = "dev"
|
||||
|
||||
func Main() int {
|
||||
options, err := parseCliOptions()
|
||||
|
||||
@@ -15,6 +20,11 @@ func Main() int {
|
||||
|
||||
switch options.intent {
|
||||
case cliIntentServe:
|
||||
// remove in v0.10.0
|
||||
if serveUpdateNoticeIfConfigLocationNotMigrated(options.configPath) {
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := serveApp(options.configPath); err != nil {
|
||||
fmt.Println(err)
|
||||
return 1
|
||||
@@ -22,7 +32,7 @@ func Main() int {
|
||||
case cliIntentConfigValidate:
|
||||
contents, _, err := parseYAMLIncludes(options.configPath)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to parse config file: %v\n", err)
|
||||
fmt.Printf("could not parse config file: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -33,7 +43,7 @@ func Main() int {
|
||||
case cliIntentConfigPrint:
|
||||
contents, _, err := parseYAMLIncludes(options.configPath)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to parse config file: %v\n", err)
|
||||
fmt.Printf("could not parse config file: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -54,12 +64,12 @@ func serveApp(configPath string) error {
|
||||
|
||||
onChange := func(newContents []byte) {
|
||||
if stopServer != nil {
|
||||
log.Println("Config file changed, attempting to restart server")
|
||||
log.Println("Config file changed, reloading...")
|
||||
}
|
||||
|
||||
config, err := newConfigFromYAML(newContents)
|
||||
if err != nil {
|
||||
log.Printf("Config file is invalid: %v", err)
|
||||
log.Printf("Config has errors: %v", err)
|
||||
|
||||
if !hadValidConfigOnStartup {
|
||||
close(exitChannel)
|
||||
@@ -70,7 +80,11 @@ func serveApp(configPath string) error {
|
||||
hadValidConfigOnStartup = true
|
||||
}
|
||||
|
||||
app := newApplication(config)
|
||||
app, err := newApplication(config)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create application: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if stopServer != nil {
|
||||
if err := stopServer(); err != nil {
|
||||
@@ -80,7 +94,7 @@ func serveApp(configPath string) error {
|
||||
|
||||
go func() {
|
||||
var startServer func() error
|
||||
startServer, stopServer = app.Server()
|
||||
startServer, stopServer = app.server()
|
||||
|
||||
if err := startServer(); err != nil {
|
||||
log.Printf("Failed to start server: %v", err)
|
||||
@@ -94,7 +108,7 @@ func serveApp(configPath string) error {
|
||||
|
||||
configContents, configIncludes, err := parseYAMLIncludes(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse config file: %w", err)
|
||||
return fmt.Errorf("parsing config: %w", err)
|
||||
}
|
||||
|
||||
stopWatching, err := configFilesWatcher(configPath, configContents, configIncludes, onChange, onErr)
|
||||
@@ -105,17 +119,54 @@ func serveApp(configPath string) error {
|
||||
|
||||
config, err := newConfigFromYAML(configContents)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse config file: %w", err)
|
||||
return fmt.Errorf("validating config file: %w", err)
|
||||
}
|
||||
|
||||
app := newApplication(config)
|
||||
app, err := newApplication(config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating application: %w", err)
|
||||
}
|
||||
|
||||
startServer, _ := app.Server()
|
||||
startServer, _ := app.server()
|
||||
if err := startServer(); err != nil {
|
||||
return fmt.Errorf("failed to start server: %w", err)
|
||||
return fmt.Errorf("starting server: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
<-exitChannel
|
||||
return nil
|
||||
}
|
||||
|
||||
func serveUpdateNoticeIfConfigLocationNotMigrated(configPath string) bool {
|
||||
if !isRunningInsideDockerContainer() {
|
||||
return false
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// glance.yml wasn't mounted to begin with or was incorrectly mounted as a directory
|
||||
if stat, err := os.Stat("glance.yml"); err != nil || stat.IsDir() {
|
||||
return false
|
||||
}
|
||||
|
||||
templateFile, _ := templateFS.Open("v0.7-update-notice-page.html")
|
||||
bodyContents, _ := io.ReadAll(templateFile)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.Write([]byte(bodyContents))
|
||||
})
|
||||
|
||||
server := http.Server{
|
||||
Addr: "localhost:8080",
|
||||
Handler: mux,
|
||||
}
|
||||
server.ListenAndServe()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user