-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (53 loc) · 1.5 KB
/
main.go
File metadata and controls
63 lines (53 loc) · 1.5 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
package main
import (
"database/sql"
"log"
"net/http"
_ "github.com/lib/pq"
"github.com/warrenharper/restapi/auth"
"github.com/warrenharper/restapi/configuration"
"github.com/warrenharper/restapi/configuration/confighandler"
"github.com/warrenharper/restapi/utils/request"
"github.com/warrenharper/restapi/utils/response"
)
func SetupDB() *sql.DB {
db, err := sql.Open("postgres", "user=tenable password=insecure dbname=restapi")
if err != nil {
log.Fatal(err)
}
db.Exec("DELETE FROM users")
db.Exec("DELETE FROM configuration")
db.Exec("DELETE FROM sessions")
return db
}
func main() {
var (
db = SetupDB()
authentication *auth.Auth = &auth.Auth{db}
configHandler http.Handler = confighandler.Handler{
configuration.ConfigurationController{db},
}
)
authentication.RegisterUser(auth.User{Username: "john_doe", Password: "password"})
configHandler = authentication.VerifySessions(configHandler)
mux := http.NewServeMux()
// Login
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
if !request.Is(r, "POST") {
response.MethodNotAllowed(w)
return
}
authentication.HandleLogin(w, r)
return
})
//Logout
mux.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
if !request.Is(r, "POST") {
response.MethodNotAllowed(w)
return
}
authentication.HandleLogout(w, r)
})
mux.Handle("/configurations/", http.StripPrefix("/configurations", configHandler))
log.Fatal(http.ListenAndServe(":8080", mux))
}