Separate admin port

master v0.3.1
Caj Larsson 3 years ago
parent 5bad2c5335
commit d1e53c2337

@ -0,0 +1 @@
package dataswamp

@ -0,0 +1 @@
package dataswamp

@ -2,6 +2,10 @@
port = 8002 port = 8002
host = "127.0.0.1" host = "127.0.0.1"
[admin]
port = 8003
host = "127.0.0.1"
[file] [file]
path = "/tmp/datta2" path = "/tmp/datta2"

@ -19,8 +19,10 @@ type Router interface {
type Bog struct { type Bog struct {
router Router router Router
adminRouter Router
file_service dataswamp.SwampFileService file_service dataswamp.SwampFileService
address string address string
adminAddress string
logger dataswamp.Logger logger dataswamp.Logger
} }
@ -40,22 +42,12 @@ func buildNamespaceRepository(config DatabaseConfig) namespace.Repository {
} }
func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" { ref := swampfile.FileReference{r.URL.Path, r.Header["User-Agent"][0]}
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 { if r.URL.Path == "/" {
panic(err) http.NotFound(w, r)
}
return return
} }
ref := swampfile.FileReference{r.URL.Path, r.Header["User-Agent"][0]}
switch r.Method { switch r.Method {
case "GET": case "GET":
@ -105,8 +97,24 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) {
return 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() { func (b *Bog) routes() {
b.router.HandleFunc("/", b.fileHandler) b.router.HandleFunc("/", b.fileHandler)
b.adminRouter.HandleFunc("/", b.dashboardHandler)
} }
func (b *Bog) cleanNamespaces() { func (b *Bog) cleanNamespaces() {
@ -118,7 +126,8 @@ func (b *Bog) cleanNamespaces() {
func New(config *Configuration) *Bog { func New(config *Configuration) *Bog {
b := new(Bog) b := new(Bog)
b.address = config.bindAddress() b.address = config.Server.bindAddress()
b.adminAddress = config.Admin.bindAddress()
fsSwampRepo := buildFileDataRepository(config.File) fsSwampRepo := buildFileDataRepository(config.File)
nsRepo := buildNamespaceRepository(config.Database) nsRepo := buildNamespaceRepository(config.Database)
@ -133,6 +142,7 @@ func New(config *Configuration) *Bog {
) )
b.logger = logger b.logger = logger
b.router = new(http.ServeMux) b.router = new(http.ServeMux)
b.adminRouter = new(http.ServeMux)
b.routes() b.routes()
return b return b
} }
@ -140,5 +150,6 @@ func New(config *Configuration) *Bog {
func (b *Bog) Run() { func (b *Bog) Run() {
b.logger.Info("Starting bog on address: %s", b.address) b.logger.Info("Starting bog on address: %s", b.address)
go b.cleanNamespaces() go b.cleanNamespaces()
go func(){ http.ListenAndServe(b.adminAddress, b.adminRouter) }()
http.ListenAndServe(b.address, b.router) http.ListenAndServe(b.address, b.router)
} }

@ -60,14 +60,15 @@ type LoggingConfig struct {
type Configuration struct { type Configuration struct {
Server ServerConfig Server ServerConfig
Admin ServerConfig
File FileConfig File FileConfig
Database DatabaseConfig Database DatabaseConfig
Quota QuotaConfig Quota QuotaConfig
Logging LoggingConfig Logging LoggingConfig
} }
func (c *Configuration) bindAddress() string { func (c ServerConfig) bindAddress() string {
return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) return fmt.Sprintf("%s:%d", c.Host, c.Port)
} }
func ConfigFromToml(toml_data string) (*Configuration, error) { func ConfigFromToml(toml_data string) (*Configuration, error) {

@ -8,11 +8,15 @@ import (
func TestConfiguration(t *testing.T) { func TestConfiguration(t *testing.T) {
is := is.New(t) is := is.New(t)
c, _ := ConfigFromToml( c, _ := ConfigFromToml(`
`[server] [server]
port = 8002 port = 8002
host = "127.0.0.1" host = "127.0.0.1"
[admin]
port = 8001
host = "127.0.0.1"
[file] [file]
path = "/tmp/datta2" path = "/tmp/datta2"

Loading…
Cancel
Save