-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (112 loc) · 2.94 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
html "html/template"
"net/http"
"github.com/BurntSushi/toml"
"github.com/UCCNetworkingSociety/go-ldap"
"github.com/go-chi/chi"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
)
var (
conf config
homeTemplate = html.Must(html.ParseFiles("templates/wrapper.html", "templates/home.html"))
loginTemplate = html.Must(html.ParseFiles("templates/wrapper.html", "templates/login.html"))
fileTemplate = html.Must(html.ParseFiles("templates/wrapper.html", "templates/file.html"))
)
type config struct {
CookieHost string `toml:"cookie_host"`
LDAPKey string `toml:"LDAP_Key"`
LDAPHost string `toml:"LDAP_Host"`
LDAPUser string `toml:"LDAP_User"`
LDAPBaseDN string `toml:"LDAP_BaseDN"`
}
func init() {
store.Options = &sessions.Options{
Domain: "127.0.0.1",
MaxAge: 60 * 10,
HttpOnly: true,
Path: "/",
}
}
func loadConfig() error {
if _, err := toml.DecodeFile("settings.conf", &conf); err != nil {
fmt.Println(err)
return err
}
return nil
}
func home(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "text/html")
if err := homeTemplate.ExecuteTemplate(w, "main", nil); err != nil {
fmt.Println(err)
}
}
func login(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "text/html")
if err := loginTemplate.ExecuteTemplate(w, "main", nil); err != nil {
fmt.Println(err)
}
}
func loginSubmit(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
r.ParseForm()
user := r.PostFormValue("user")
pass := r.PostFormValue("pass")
u, err := ldap.GetUserFromLDAP(user, pass, conf.LDAPBaseDN, conf.LDAPUser, conf.LDAPKey, conf.LDAPHost)
if err != nil {
fmt.Fprint(w, err)
return
}
session, err := store.New(r, "id")
if err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
session.Values["user"] = u
if err := session.Save(r, w); err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, "/upload", http.StatusFound)
}
func file(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "text/html")
if err := fileTemplate.ExecuteTemplate(w, "main", nil); err != nil {
fmt.Println(err)
}
}
func fileSubmit(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
r.ParseForm()
file, _, err := r.FormFile("file")
if err != nil {
return
}
defer file.Close()
var b []byte
file.Read(b)
fmt.Fprint(w, b)
}
func main() {
if loadConfig() != nil {
return
}
r := chi.NewRouter()
r.HandleFunc("/", home)
r.Route("/login", func(r chi.Router) {
r.HandleFunc("/", login)
r.With(isAlreadyLoggedIn).Post("/post", loginSubmit)
})
r.Route("/upload", func(r chi.Router) {
r.Use(isLoggedIn)
r.Get("/", file)
r.Post("/submit", fileSubmit)
})
fmt.Println("listening on http://localhost:8080")
http.ListenAndServe(":8080", context.ClearHandler(r))
}