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
host = "127.0.0.1"
[admin]
port = 8003
host = "127.0.0.1"
[file]
path = "/tmp/datta2"

@ -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)
}

@ -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) {

@ -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"

Loading…
Cancel
Save