-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (71 loc) · 1.9 KB
/
main.go
File metadata and controls
88 lines (71 loc) · 1.9 KB
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
/**
* Entry point into the HomeworkHub web server.
*
*/
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"html/template"
"log"
"net/http"
)
var (
tpl *template.Template
authenticated = false
database *sql.DB
err error
user User
)
func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
func main() {
initializeDb()
defer func() {
err = database.Close()
if err != nil {
panic(err)
}
}()
// Route for index page
http.HandleFunc("/", indexHandler)
// Route for static assets
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static"))))
// Route for posts
http.HandleFunc("/h/", postViewHandler)
http.Handle("/h/img/", http.StripPrefix("/h/img", http.FileServer(http.Dir("./posts"))))
http.HandleFunc("/login", loginHandler)
http.HandleFunc("/logout", logoutHandler)
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/upload", uploadHandler)
http.HandleFunc("/comment/", commentHandler)
port := ":3000"
log.Printf("Server running on port %s...\n", port)
serve := http.ListenAndServe(port, nil)
if serve != nil {
panic(serve)
}
}
func initializeDb() {
database, _ = sql.Open("sqlite3", "./db.sqlite3")
statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS userInfo (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)")
_, err = statement.Exec()
if err != nil {
panic(err)
}
statement, _ = database.Prepare("CREATE TABLE IF NOT EXISTS postInfo (postId INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, title TEXT, extension TEXT)")
_, err = statement.Exec()
if err != nil {
panic(err)
}
statement, _ = database.Prepare("CREATE TABLE IF NOT EXISTS commentSection (postId INTEGER, username TEXT, comment TEXT)")
_, err = statement.Exec()
if err != nil {
panic(err)
}
err = database.Ping()
if err != nil {
panic(err)
}
}