-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
61 lines (52 loc) · 1.61 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
# File init.go
# Author: Dan Huckson
# Date: 20160401
*/
package gobase
import (
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/unodan/cache"
"github.com/unodan/gobase/control/app"
)
func ServeStatic(r *mux.Router, base string) {
staticPaths := map[string]string{
"images": base + "/images/",
"javascript": base + "/javascript/",
"stylesheets": base + "/stylesheets/",
}
for pathName, pathValue := range staticPaths {
pathPrefix := "/" + pathName + "/"
r.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue))))
}
}
func init() {
var (
appID = "mwa"
appName = "My Web Application"
)
uri := "/"
log.Printf("\n\n--- %s %s %s", appName, uri, strings.Repeat("-", 80-len(appName+uri)-9))
log.Println("init, Info: application [ " + appName + " ] has started")
r := mux.NewRouter()
ca := app.Setup(&app.Cache{
ID: appID,
Title: appName,
})
func(handlers map[string]func(http.ResponseWriter, *http.Request, string, *cache.Store)) {
r.NotFoundHandler = app.MakeHandler(app.Help, "/help", ca)
for route := range handlers {
r.HandleFunc(route, app.MakeHandler(handlers[route], route, ca))
}
ServeStatic(r, ".")
http.Handle("/", r)
log.Println("init, Info: application [ " + ca.Get("Title").(string) + " ] has initialized successfully")
log.Println("init, Info: server [ " + ca.Get("HostName").(string) + " ], waiting for requests on port [ " + ca.Get("HostPort").(string) + " ]")
}(map[string]func(http.ResponseWriter, *http.Request, string, *cache.Store){
"/": app.Home,
"/somepage": app.Somepage,
})
}