This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwiki.go
91 lines (75 loc) · 2.35 KB
/
wiki.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
80
81
82
83
84
85
86
87
88
89
90
91
package wiki
import (
"database/sql"
"html/template"
"log"
"net/http"
"github.com/suzuken/wiki/controller"
"github.com/suzuken/wiki/db"
"github.com/suzuken/wiki/view"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/csrf"
)
// Server is whole server implementation for this wiki app.
// This holds database connection and router settings.
type Server struct {
db *sql.DB
handler http.Handler
}
// Close makes the database connection to close.
func (s *Server) Close() error {
return s.db.Close()
}
// Init initialize server state. Connecting to database, compiling templates,
// and settings router.
func (s *Server) Init(dbconf, env string, debug bool) {
cs, err := db.NewConfigsFromFile(dbconf)
if err != nil {
log.Fatalf("cannot open database configuration. exit. %s", err)
}
db, err := cs.Open(env)
if err != nil {
log.Fatalf("db initialization failed: %s", err)
}
// In debug mode, we compile templates on every request.
view.Init(template.FuncMap{
"LoggedIn": controller.LoggedIn,
"CurrentName": controller.CurrentName,
"Flash": controller.Flash,
}, debug)
s.db = db
s.Route()
}
// New returns server object.
func New() *Server {
return &Server{}
}
// csrfProtectKey should have 32 byte length.
var csrfProtectKey = []byte("32-byte-long-auth-key")
// Run starts running http server.
func (s *Server) Run(addr string) {
log.Printf("start listening on %s", addr)
// NOTE: when you serve on TLS, make csrf.Secure(true)
CSRF := csrf.Protect(
csrfProtectKey, csrf.Secure(false))
http.ListenAndServe(addr, context.ClearHandler(CSRF(s.handler)))
}
// Route setting router for this wiki.
func (s *Server) Route() {
mux := http.NewServeMux()
article := &controller.Article{DB: s.db}
user := &controller.User{DB: s.db}
mux.Handle("/authtest", GET(Auth(controller.AuthTestHandler)))
mux.Handle("/new", GET(controller.NewArticleHandler))
mux.Handle("/article/", GET(article.Get))
mux.Handle("/article/edit/", GET(Auth(article.Edit)))
mux.Handle("/save", POST(Auth(article.Save)))
mux.Handle("/delete", POST(Auth(article.Delete)))
mux.Handle("/logout", handler(user.LogoutHandler))
mux.Handle("/", GET(article.Root))
mux.Handle("/signup", handler(user.SignupHandler))
mux.Handle("/login", handler(user.LoginHandler))
mux.Handle("/static", http.FileServer(http.Dir("./static")))
s.handler = mux
}