From d1e53c23379f70add4ba59e964c99ca7a0bfd85e Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Sat, 7 May 2022 14:48:22 +0800 Subject: [PATCH] Separate admin port --- dataswamp/admin_service.go | 1 + dataswamp/admin_service_test.go | 1 + dataswamp/{services.go => swamp_service.go} | 0 ...services_test.go => swamp_service_test.go} | 0 default.toml | 4 ++ server/bog.go | 39 ++++++++++++------- server/configuration.go | 5 ++- server/configuration_test.go | 8 +++- 8 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 dataswamp/admin_service.go create mode 100644 dataswamp/admin_service_test.go rename dataswamp/{services.go => swamp_service.go} (100%) rename dataswamp/{services_test.go => swamp_service_test.go} (100%) diff --git a/dataswamp/admin_service.go b/dataswamp/admin_service.go new file mode 100644 index 0000000..79e5dd9 --- /dev/null +++ b/dataswamp/admin_service.go @@ -0,0 +1 @@ +package dataswamp diff --git a/dataswamp/admin_service_test.go b/dataswamp/admin_service_test.go new file mode 100644 index 0000000..79e5dd9 --- /dev/null +++ b/dataswamp/admin_service_test.go @@ -0,0 +1 @@ +package dataswamp diff --git a/dataswamp/services.go b/dataswamp/swamp_service.go similarity index 100% rename from dataswamp/services.go rename to dataswamp/swamp_service.go diff --git a/dataswamp/services_test.go b/dataswamp/swamp_service_test.go similarity index 100% rename from dataswamp/services_test.go rename to dataswamp/swamp_service_test.go diff --git a/default.toml b/default.toml index 33a699b..ce8c0df 100644 --- a/default.toml +++ b/default.toml @@ -2,6 +2,10 @@ port = 8002 host = "127.0.0.1" +[admin] + port = 8003 + host = "127.0.0.1" + [file] path = "/tmp/datta2" diff --git a/server/bog.go b/server/bog.go index e5e6afd..6a8b13e 100644 --- a/server/bog.go +++ b/server/bog.go @@ -19,8 +19,10 @@ type Router interface { type Bog struct { router Router + adminRouter Router file_service dataswamp.SwampFileService address string + adminAddress string logger dataswamp.Logger } @@ -40,22 +42,12 @@ func buildNamespaceRepository(config DatabaseConfig) namespace.Repository { } func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" { - templ, err := template.ParseFiles("server/views/dashboard.html") - - if err != nil { - panic(err) - } - - stats, _ := b.file_service.NamespaceStats() - err = templ.Execute(w, stats) + ref := swampfile.FileReference{r.URL.Path, r.Header["User-Agent"][0]} - if err != nil { - panic(err) - } + if r.URL.Path == "/" { + http.NotFound(w, r) return } - ref := swampfile.FileReference{r.URL.Path, r.Header["User-Agent"][0]} switch r.Method { case "GET": @@ -105,8 +97,24 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { return } +func (b *Bog) dashboardHandler(w http.ResponseWriter, r *http.Request) { + templ, err := template.ParseFiles("server/views/dashboard.html") + + if err != nil { + panic(err) + } + + stats, _ := b.file_service.NamespaceStats() + err = templ.Execute(w, stats) + + if err != nil { + panic(err) + } +} + func (b *Bog) routes() { b.router.HandleFunc("/", b.fileHandler) + b.adminRouter.HandleFunc("/", b.dashboardHandler) } func (b *Bog) cleanNamespaces() { @@ -118,7 +126,8 @@ func (b *Bog) cleanNamespaces() { func New(config *Configuration) *Bog { b := new(Bog) - b.address = config.bindAddress() + b.address = config.Server.bindAddress() + b.adminAddress = config.Admin.bindAddress() fsSwampRepo := buildFileDataRepository(config.File) nsRepo := buildNamespaceRepository(config.Database) @@ -133,6 +142,7 @@ func New(config *Configuration) *Bog { ) b.logger = logger b.router = new(http.ServeMux) + b.adminRouter = new(http.ServeMux) b.routes() return b } @@ -140,5 +150,6 @@ func New(config *Configuration) *Bog { func (b *Bog) Run() { b.logger.Info("Starting bog on address: %s", b.address) go b.cleanNamespaces() + go func(){ http.ListenAndServe(b.adminAddress, b.adminRouter) }() http.ListenAndServe(b.address, b.router) } diff --git a/server/configuration.go b/server/configuration.go index ead6ee9..2fa3173 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -60,14 +60,15 @@ type LoggingConfig struct { type Configuration struct { Server ServerConfig + Admin ServerConfig File FileConfig Database DatabaseConfig Quota QuotaConfig Logging LoggingConfig } -func (c *Configuration) bindAddress() string { - return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) +func (c ServerConfig) bindAddress() string { + return fmt.Sprintf("%s:%d", c.Host, c.Port) } func ConfigFromToml(toml_data string) (*Configuration, error) { diff --git a/server/configuration_test.go b/server/configuration_test.go index ce080d2..dd6e90d 100644 --- a/server/configuration_test.go +++ b/server/configuration_test.go @@ -8,11 +8,15 @@ import ( func TestConfiguration(t *testing.T) { is := is.New(t) - c, _ := ConfigFromToml( - `[server] + c, _ := ConfigFromToml(` +[server] port = 8002 host = "127.0.0.1" +[admin] + port = 8001 + host = "127.0.0.1" + [file] path = "/tmp/datta2"