-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
99 lines (80 loc) · 2.67 KB
/
main.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
92
93
94
95
96
97
98
99
package main
import (
"compress/gzip"
"errors"
"flag"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/GeertJohan/go.rice"
"github.com/daaku/go.httpgzip"
"github.com/gorilla/mux"
)
const (
limitPosts = 10
limitTopics = 10
)
var listen = flag.String("listen", "localhost:8080", "host and port to listen on")
var db = flag.String("db", "forum.db", "sqlite3 database file")
func backup() error {
src, err := os.Open(*db)
defer src.Close()
if err != nil {
return errors.New("could not open database to backup")
}
backupPath := path.Join(filepath.Dir(*db), "backup")
err = os.MkdirAll(backupPath, 0755)
if err != nil {
return errors.New("could not create backup")
}
destFile := path.Join(backupPath, filepath.Base(*db)+".gz")
dest, err := os.Create(destFile)
defer dest.Close()
if err != nil {
return err
}
gzipWriter := gzip.NewWriter(dest)
_, err = io.Copy(gzipWriter, src)
if err != nil {
return err
}
return gzipWriter.Close()
}
func main() {
flag.Parse()
err := backup()
if err != nil {
log.Panicln(err)
}
log.Println("backup complete")
app := newApp()
defer app.destroy()
log.Println("database opened")
r := mux.NewRouter()
staticBox := rice.MustFindBox("static").HTTPBox()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox())))
r.HandleFunc("/", app.handleIndex)
f := r.PathPrefix("/forum").Subrouter()
f.HandleFunc("/{id:[0-9]+}", app.handleForum).Methods("GET")
f.HandleFunc("/{id:[0-9]+}/page/{page:[0-9]+}", app.handleForum).Methods("GET")
f.HandleFunc("/{id:[0-9]+}/add", app.handleLoginRequired(app.handleAddTopic, "/forum")).Methods("GET")
f.HandleFunc("/{id:[0-9]+}/add", app.handleLoginRequired(app.handleSaveTopic, "/forum")).Methods("POST")
t := r.PathPrefix("/topic").Subrouter()
t.HandleFunc("/{id:[0-9]+}", app.handleTopic).Methods("GET")
t.HandleFunc("/{id:[0-9]+}/page/{page:[0-9]+}", app.handleTopic).Methods("GET")
t.HandleFunc("/{id:[0-9]+}/add", app.handleLoginRequired(app.handleAddPost, "/topic")).Methods("GET")
t.HandleFunc("/{id:[0-9]+}/add", app.handleLoginRequired(app.handleSavePost, "/topic")).Methods("POST")
t.HandleFunc("/{id:[0-9]+}/delete", app.handleLoginRequired(app.handleDeletePost, "/topic")).Methods("POST")
u := r.PathPrefix("/user").Subrouter()
u.HandleFunc("/add", app.handleRegister).Methods("GET")
u.HandleFunc("/add", app.saveRegister).Methods("POST")
u.HandleFunc("/login", app.handleLogin).Methods("GET")
u.HandleFunc("/login", app.saveLogin).Methods("POST")
u.HandleFunc("/logout", app.handleLogout)
http.Handle("/", httpgzip.NewHandler(r))
log.Printf("Serving on %s\n", *listen)
log.Fatal(http.ListenAndServe(*listen, nil))
}