/
blog.go
80 lines (62 loc) · 1.87 KB
/
blog.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
76
77
78
79
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/sohlich/goblog/blog"
"github.com/sohlich/goblog/repository"
"code.google.com/p/gcfg"
)
const defaultConfig = `
[mongo]
port = 27017
host= localhost`
var router *mux.Router
type Config struct {
Port string
Host string
}
type configFile struct {
Mongo Config
}
//Main method launching server
func main() {
Init()
defer cleanUp()
//Define rest
router = mux.NewRouter()
http.Handle("/", blog.HttpSecurityInterceptor(router))
router.HandleFunc("/articles/{permalink}", blog.GetPost)
router.HandleFunc("/register", blog.RegisterFormProcess).Methods("POST")
router.HandleFunc("/register", blog.RegisterForm).Methods("GET")
router.HandleFunc("/login", blog.LoginFormProcess).Methods("POST")
router.HandleFunc("/login", blog.LoginForm).Methods("GET")
router.HandleFunc("/logout", blog.Logout).Methods("GET")
router.HandleFunc("/new", blog.InsertPost).Methods("POST")
router.HandleFunc("/new", blog.InsertPostForm).Methods("GET")
router.HandleFunc("/admin", blog.AdminInterface).Methods("GET")
router.PathPrefix("/css").Handler(http.FileServer(http.Dir("./static")))
router.PathPrefix("/js").Handler(http.FileServer(http.Dir("./static")))
router.HandleFunc("/", blog.Index).Methods("GET")
log.Fatal(http.ListenAndServe(":8080", nil)) //http handler handles request first
}
func Init() {
config := LoadConfiguration("application.conf")
repository.SetupMongo(config.Host,config.Port)
repository.PostRepository()
repository.UserRepository()
}
func cleanUp() {
repository.CloseMongoSession()
}
func LoadConfiguration(cfgFile string) Config {
var err error
var cfg configFile
if cfgFile != "" {
err = gcfg.ReadFileInto(&cfg, cfgFile)
} else {
err = gcfg.ReadStringInto(&cfg, defaultConfig)
}
if err != nil {log.Panic(err)}
return cfg.Mongo
}