-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (89 loc) · 3.07 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/KFBI1706/TxtDump/api"
"github.com/KFBI1706/TxtDump/config"
"github.com/KFBI1706/TxtDump/html"
"github.com/KFBI1706/TxtDump/sql"
"github.com/gorilla/mux"
)
func main() {
conf := config.ParseConfig("development")
var defaultDir string
if conf.DBStringLocation != "" {
defaultDir = conf.DBStringLocation
} else {
defaultDir = "dbstring"
}
dir := flag.String("dir", defaultDir, "root-directory for important files such as dbstring")
dbdrop := flag.Bool("dropdb", false, "Drop current table and all data")
dbsetup := flag.Bool("setupdb", false, "Setup db when running")
//production flag should be an explicit env variable
port := flag.Int("port", conf.Port, "for using a custom port")
flag.Parse()
config.InitDB(*dir)
defer config.DB.Close()
addr := fmt.Sprintf(":%v", *port)
if *dbdrop || *dbsetup {
if *dbdrop {
err := sql.ClearOutDB()
if err != nil {
log.Println(err)
}
}
if *dbsetup {
err := sql.SetupDB()
if err != nil {
log.Println(err)
}
}
}
if err := sql.TestDBConnection(); err != nil {
log.Fatal(err)
}
log.Printf("%v Post(s) Currently in DB\n", sql.CountPosts())
router := mux.NewRouter()
a := router.PathPrefix("/api/v1/post").Subrouter()
w := router.PathPrefix("/post/").Subrouter()
router.HandleFunc("/", logging(html.DisplayIndex)).Methods("GET")
a.HandleFunc("/amount", logging(api.PostcounterAPI)).Methods("GET")
a.HandleFunc("/{id}/request", logging(api.RequestPostAPI)).Methods("GET")
a.HandleFunc("/{id}/request", logging(api.RequestPostWithPassAPI)).Methods("POST")
a.HandleFunc("/create", logging(api.CreatePostAPI)).Methods("POST")
a.HandleFunc("/{id}/edit", logging(api.EditPostAPI)).Methods("POST")
a.HandleFunc("/{id}/delete", logging(api.DeletePostAPI)).Methods("POST")
w.HandleFunc("/{id}/request", logging(html.RequestPostWeb)).Methods("GET")
w.HandleFunc("/{id}/request/decrypt", logging(html.RequestPostDecrypt)).Methods("POST")
w.HandleFunc("/{id}/edit", logging(html.EditPostTemplate))
w.HandleFunc("/{id}/edit/decrypt", logging(html.EditPostDecrypt))
w.HandleFunc("/{id}/edit/post", logging(html.EditPostForm)).Methods("POST")
w.HandleFunc("/{id}/delete", logging(html.DeletePostTemplate))
w.HandleFunc("/{id}/delete/post", logging(html.DeletePostForm)).Methods("POST")
w.HandleFunc("/create", logging(html.CreatePostTemplateWeb))
w.HandleFunc("/create/new", logging(html.CreatePostWeb))
router.HandleFunc("/documentation", logging(html.Documentation))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("front/"))))
if err := router.Walk(routerWalk); err != nil {
log.Fatal(err)
}
if err := http.ListenAndServe(addr, router); err != nil {
log.Fatal(err)
}
}
func logging(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.URL.Path)
f(w, r)
}
}
func routerWalk(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
t, err := route.GetPathTemplate()
if err != nil {
return err
}
fmt.Println(t)
return nil
}