-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
75 lines (59 loc) · 1.89 KB
/
server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"html/template"
"log"
"net/http"
_ "net/http/pprof"
"os"
"github.com/oooska/ircwebchat/chat"
"github.com/oooska/ircwebchat/controllers"
_ "github.com/mattn/go-sqlite3"
)
//Starts a basic http server with the ircwebchat Handler registered
func main() {
log.SetFlags(log.Lshortfile | log.Ltime)
t := populateTemplates()
err := chat.SetupPersistence("sqlite3", "my super secret key of dooooooooooooooooooooooooooom", "db.sqlite")
if err != nil {
log.Fatalf("Recieved error starting DB: %s", err.Error())
}
//Start sessions that are enabled
chat.StartChats()
//Register handlers
controllers.Register(t, "static/", nil)
/*go func() {
log.Printf("Starting TLS server on :8443")
log.Fatal(http.ListenAndServeTLS(":8443", "tls/fullchain.pem", "tls/privkey.pem", nil))
}()*/
log.Printf("Starting http server on :8080")
go log.Fatal(http.ListenAndServe(":8080", nil))
}
func populateTemplates() *template.Template {
result := template.New("templates")
basePath := "templates"
templatePaths := parseTemplateDirectory(basePath)
result, err := result.ParseFiles(templatePaths...)
if err != nil {
log.Fatalf("Error parsing templates: %s", err.Error())
}
return result
}
func parseTemplateDirectory(basePath string) []string {
templateFolder, err := os.Open(basePath)
defer templateFolder.Close()
if err != nil {
log.Fatalf("Unable to open templates folder %s.", basePath)
}
templatePathsRaw, _ := templateFolder.Readdir(-1)
templatePaths := new([]string)
for _, pathInfo := range templatePathsRaw {
if !pathInfo.IsDir() {
*templatePaths = append(*templatePaths, basePath+"/"+pathInfo.Name())
log.Printf("Adding %s to list of templates", basePath+"/"+pathInfo.Name())
} else {
subtemplatePaths := parseTemplateDirectory(basePath + "/" + pathInfo.Name())
*templatePaths = append(*templatePaths, subtemplatePaths...)
}
}
return *templatePaths
}