camlistored: Refactoring main function into smaller pieces.

Change-Id: I66cc428c961f4b0582f20d906da4cb621a7e3aaa
This commit is contained in:
Brett Slatkin 2011-09-30 04:18:12 +00:00
parent 69ec51052f
commit 8a96dc4037
1 changed files with 45 additions and 28 deletions

View File

@ -147,25 +147,20 @@ type handlerConfig struct {
} }
type handlerLoader struct { type handlerLoader struct {
ws *webserver.Server installer HandlerInstaller
baseURL string baseURL string
config map[string]*handlerConfig // prefix -> config config map[string]*handlerConfig // prefix -> config
handler map[string]interface{} // prefix -> http.Handler / func / blobserver.Storage handler map[string]interface{} // prefix -> http.Handler / func / blobserver.Storage
} }
type HandlerInstaller interface {
Handle(path string, handler http.Handler)
}
func main() { func main() {
flag.Parse() flag.Parse()
configPath := *flagConfigFile config, configPath := getConfig()
if !filepath.IsAbs(configPath) {
configPath = filepath.Join(osutil.CamliConfigDir(), configPath)
}
config, err := jsonconfig.ReadFile(configPath)
if err != nil {
exitFailure("%v", err)
}
ws := webserver.New() ws := webserver.New()
baseURL := ws.BaseURL() baseURL := ws.BaseURL()
@ -179,23 +174,55 @@ func main() {
} }
} }
uiPath, err := setup(ws, baseURL, config)
if err != nil {
exitFailure("Error parsing config %s: %v", configPath, err)
}
ws.Listen()
if uiPath != "" {
uiURL := ws.BaseURL() + uiPath
log.Printf("UI available at %s", uiURL)
if runtime.GOOS == "windows" {
// Might be double-clicking an icon with no shell window?
// Just open the URL for them.
osutil.OpenURL(uiURL)
}
}
ws.Serve()
}
func getConfig() (config jsonconfig.Obj, configPath string) {
configPath = *flagConfigFile
if !filepath.IsAbs(configPath) {
configPath = filepath.Join(osutil.CamliConfigDir(), configPath)
}
config, err := jsonconfig.ReadFile(configPath)
if err != nil {
exitFailure("%v", err)
}
return config, configPath
}
func setup(hi HandlerInstaller, baseURL string, config jsonconfig.Obj) (uiPath string, err os.Error) {
auth.AccessPassword = config.OptionalString("password", "") auth.AccessPassword = config.OptionalString("password", "")
if url := config.OptionalString("baseURL", ""); url != "" { if url := config.OptionalString("baseURL", ""); url != "" {
baseURL = url baseURL = url
} }
prefixes := config.RequiredObject("prefixes") prefixes := config.RequiredObject("prefixes")
if err = config.Validate(); err != nil { if err := config.Validate(); err != nil {
exitFailure("configuration error in root object's keys in %s: %v", configPath, err) return "", fmt.Errorf("configuration error in root object's keys: %v", err)
} }
hl := &handlerLoader{ hl := &handlerLoader{
ws: ws, installer: hi,
baseURL: baseURL, baseURL: baseURL,
config: make(map[string]*handlerConfig), config: make(map[string]*handlerConfig),
handler: make(map[string]interface{}), handler: make(map[string]interface{}),
} }
uiPath := ""
for prefix, vei := range prefixes { for prefix, vei := range prefixes {
if !strings.HasPrefix(prefix, "/") { if !strings.HasPrefix(prefix, "/") {
exitFailure("prefix %q doesn't start with /", prefix) exitFailure("prefix %q doesn't start with /", prefix)
@ -229,18 +256,8 @@ func main() {
} }
} }
hl.setupAll() hl.setupAll()
ws.Listen()
if uiPath != "" { return uiPath, nil
uiURL := ws.BaseURL() + uiPath
log.Printf("UI available at %s", uiURL)
if runtime.GOOS == "windows" {
// Might be double-clicking an icon with no shell window?
// Just open the URL for them.
osutil.OpenURL(uiURL)
}
}
ws.Serve()
} }
func (hl *handlerLoader) FindHandlerByTypeIfLoaded(htype string) (prefix string, handler interface{}, err os.Error) { func (hl *handlerLoader) FindHandlerByTypeIfLoaded(htype string) (prefix string, handler interface{}, err os.Error) {
@ -324,7 +341,7 @@ func (hl *handlerLoader) setupHandler(prefix string) {
h.prefix, stype, err) h.prefix, stype, err)
} }
hl.handler[h.prefix] = pstorage hl.handler[h.prefix] = pstorage
hl.ws.Handle(prefix+"camli/", makeCamliHandler(prefix, hl.baseURL, pstorage)) hl.installer.Handle(prefix+"camli/", makeCamliHandler(prefix, hl.baseURL, pstorage))
return return
} }
@ -334,5 +351,5 @@ func (hl *handlerLoader) setupHandler(prefix string) {
h.prefix, h.htype, err) h.prefix, h.htype, err)
} }
hl.handler[prefix] = hh hl.handler[prefix] = hh
hl.ws.Handle(prefix, &httputil.PrefixHandler{prefix, hh}) hl.installer.Handle(prefix, &httputil.PrefixHandler{prefix, hh})
} }