Admin Interface & Handlers

* In Progress, Not Done Yet
+ Added Handlers to cover public and admin handlers
+ Added WebServer
+ InitHandlers used the new class
This commit is contained in:
Thorsten 2014-11-06 17:49:04 +01:00
parent f33f7b5c29
commit c1e8ca4b07
10 changed files with 126 additions and 23 deletions

13
Handlers/AddHandler.go Normal file
View File

@ -0,0 +1,13 @@
package Handlers
import (
"net/http"
)
func AddPublicHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
muxPublic.HandleFunc(pattern, handler)
}
func AddAdminHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
muxAdmin.HandleFunc(pattern, handler)
}

15
Handlers/GetMuxes.go Normal file
View File

@ -0,0 +1,15 @@
package Handlers
import (
"net/http"
)
func GetPublicMux() (mux *http.ServeMux) {
mux = muxPublic
return
}
func GetAdminMux() (mux *http.ServeMux) {
mux = muxAdmin
return
}

5
Handlers/Init.go Normal file
View File

@ -0,0 +1,5 @@
package Handlers
func init() {
}

12
Handlers/Variables.go Normal file
View File

@ -0,0 +1,12 @@
package Handlers
import (
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"net/http"
)
var (
senderName LM.Sender = `System::Handlers`
muxPublic *http.ServeMux = http.NewServeMux()
muxAdmin *http.ServeMux = http.NewServeMux()
)

View File

@ -2,6 +2,7 @@ package System
import (
"github.com/SommerEngineering/Ocean/ConfigurationDB"
"github.com/SommerEngineering/Ocean/Handlers"
"github.com/SommerEngineering/Ocean/ICCC"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
@ -9,7 +10,6 @@ import (
"github.com/SommerEngineering/Ocean/Robots"
"github.com/SommerEngineering/Ocean/StaticFiles"
"github.com/SommerEngineering/Ocean/WebContent"
"net/http"
)
func InitHandlers() {
@ -17,15 +17,15 @@ func InitHandlers() {
initSystem()
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Register now all system handlers.`)
http.HandleFunc(`/framework/`, WebContent.HandlerDeliverFramework)
http.HandleFunc(`/staticFiles/`, StaticFiles.HandlerStaticFiles)
http.HandleFunc(`/next/number`, NumGen.HandlerGetNext)
http.HandleFunc(`/robots.txt`, Robots.HandlerRobots)
http.HandleFunc(`/ICCC`, ICCC.ICCCHandler)
Handlers.AddPublicHandler(`/framework/`, WebContent.HandlerDeliverFramework)
Handlers.AddPublicHandler(`/staticFiles/`, StaticFiles.HandlerStaticFiles)
Handlers.AddPublicHandler(`/next/number`, NumGen.HandlerGetNext)
Handlers.AddPublicHandler(`/robots.txt`, Robots.HandlerRobots)
Handlers.AddPublicHandler(`/ICCC`, ICCC.ICCCHandler)
if ConfigurationDB.Read(`MapStaticFiles2Root`) == "true" {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `The static files are mapped to the root.`)
http.HandleFunc(`/`, StaticFiles.HandlerMapStaticFiles2Root)
Handlers.AddPublicHandler(`/`, StaticFiles.HandlerMapStaticFiles2Root)
}
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Done with registering all system handler.`)

View File

@ -1,22 +1,9 @@
package System
import (
"github.com/SommerEngineering/Ocean/ICCC"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"github.com/SommerEngineering/Ocean/Tools"
"net/http"
"github.com/SommerEngineering/Ocean/WebServer"
)
func StartAndBlockForever() {
ipAddressPort := Tools.LocalIPAddressAndPort()
// Tell the whole cluster, that we are up and ready:
data := ICCCStartUpMessage{}
data.IPAddressAndPort = ipAddressPort
ICCC.WriteMessage2All(ICCC.ChannelSYSTEM, `System::Start`, data)
// Start and block:
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Web server is now listening.`, `Configuration for hostname and port.`, ipAddressPort)
http.ListenAndServe(ipAddressPort, nil)
WebServer.Start()
}

View File

@ -1,4 +1,4 @@
package System
package WebServer
type ICCCStartUpMessage struct {
IPAddressAndPort string

34
WebServer/Init.go Normal file
View File

@ -0,0 +1,34 @@
package WebServer
import (
"github.com/SommerEngineering/Ocean/Handlers"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"github.com/SommerEngineering/Ocean/Tools"
"net/http"
"time"
)
func init() {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Init the web server now.`)
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Done init the web server.`)
serverPublicAddressPort := Tools.LocalIPAddressAndPort()
serverPublic = &http.Server{}
serverPublic.Addr = serverPublicAddressPort
serverPublic.Handler = Handlers.GetPublicMux()
serverPublic.ReadTimeout = 10 * time.Second
serverPublic.WriteTimeout = 10 * time.Second
serverPublic.MaxHeaderBytes = 1024
serverPublic.SetKeepAlivesEnabled(true)
serverAdmin = &http.Server{}
serverAdmin.Addr = serverAdminAddressPort
serverAdmin.Handler = Handlers.GetAdminMux()
serverAdmin.ReadTimeout = 10 * time.Second
serverAdmin.WriteTimeout = 10 * time.Second
serverAdmin.MaxHeaderBytes = 1024
serverAdmin.SetKeepAlivesEnabled(true)
}

23
WebServer/Start.go Normal file
View File

@ -0,0 +1,23 @@
package WebServer
import (
"github.com/SommerEngineering/Ocean/ICCC"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
)
func Start() {
if serverPublic != nil {
// Tell the whole cluster, that we are up and ready:
data := ICCCStartUpMessage{}
data.IPAddressAndPort = serverPublicAddressPort
ICCC.WriteMessage2All(ICCC.ChannelSYSTEM, `System::Start`, data)
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Web server is now listening.`, `Configuration for hostname and port.`, serverPublicAddressPort)
go serverPublic.ListenAndServe()
}
if serverAdmin != nil {
go serverAdmin.ListenAndServe()
}
}

14
WebServer/Variables.go Normal file
View File

@ -0,0 +1,14 @@
package WebServer
import (
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"net/http"
)
var (
senderName LM.Sender = `System::WebServer`
serverPublic *http.Server = nil
serverAdmin *http.Server = nil
serverPublicAddressPort string = ""
serverAdminAddressPort string = ""
)