-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (40 loc) · 1.21 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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
h "github.com/parikshitg/go-min-web/handlers"
"github.com/parikshitg/go-min-web/middlewares"
)
func main() {
var err error
// Open a database
h.Db, err = sql.Open("mysql", "root:parikshitg@tcp(127.0.0.1:3306)/testingdb")
if err != nil {
fmt.Println("Db Open Error:", err)
}
defer h.Db.Close()
fmt.Println("Successfully connected to Database.")
// Create a database table
query := `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL,
created_at DATETIME,
PRIMARY KEY (id)
);`
if _, err := h.Db.Exec(query); err != nil {
log.Fatal(err)
}
fmt.Println("Successfully Create Table")
http.Handle("/login", middlewares.UnauthenticatedUser(h.Login))
http.Handle("/logout", middlewares.AuthenticatedUser(h.Logout))
http.Handle("/register", middlewares.UnauthenticatedUser(h.Register))
http.Handle("/dashboard", middlewares.AuthenticatedUser(h.Dashboard))
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
}