-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (161 loc) · 4.56 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"dslife-backend/framework"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
)
type user struct {
TOKEN string `json:"TOKEN"`
}
// LoginUser is LoginUser
type LoginUser struct {
ID string `json:"id"`
PASSWORD string `json:"password"`
}
type FileUrl struct {
FILENAME string `json:"filename"`
URL string `json:"fileurl"`
}
type TokenStr struct {
TOKEN string `json:"token"`
USERINFO framework.UserInfo `json:"userinfo"`
}
func login(w http.ResponseWriter, r *http.Request) {
var UserInfo LoginUser
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
if err := json.Unmarshal(reqBody, &UserInfo); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
return
}
userinfo, token, isCorrect := framework.GetTokenFromId(UserInfo.ID, UserInfo.PASSWORD)
if isCorrect {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(TokenStr{TOKEN: token, USERINFO: userinfo}); err != nil {
panic(err)
}
return
} else {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(404)
if err := json.NewEncoder(w).Encode("Incorrect ID/PW"); err != nil {
panic(err)
}
return
}
}
type RegisterForm struct {
Id string `json:"id"`
PASSWORD string `json:"password"`
Name string `json:"name"`
Number string `json:"number"`
Room int `json:"room"`
Type string `json:"user_type"`
}
func Register(w http.ResponseWriter, r *http.Request) {
var userinfo = RegisterForm{}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
if err := json.Unmarshal(reqBody, &userinfo); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
return
}
fmt.Println(userinfo)
_, err = framework.CreateUser(userinfo.Id, userinfo.PASSWORD, userinfo.Name, userinfo.Number, userinfo.Room, userinfo.Type)
if err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(503)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode("{ 'status': 'successful' }"); err != nil {
panic(err)
}
}
// PostNotice is Bad Server Fuck You
func PostNotice(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(200000)
fileList := []FileUrl{}
if err != nil {
fmt.Fprintln(w, err)
return
}
author := r.Header.Values("Auth")
formdata := r.MultipartForm
files := formdata.File["multiplefiles"]
content := formdata.Value["content"][0]
title := formdata.Value["title"][0]
for i, _ := range files {
file, err := files[i].Open()
defer file.Close()
if err != nil {
fmt.Fprintln(w, err)
return
}
path := "./static/" + b64.StdEncoding.EncodeToString([]byte(content+title))
os.Mkdir(path, 0666)
out, err := os.OpenFile(path+"/"+files[i].Filename, os.O_WRONLY|os.O_CREATE, 0666)
defer out.Close()
fileList = append(fileList, FileUrl{FILENAME: files[i].Filename, URL: "http://localhost:8080/static/" + b64.StdEncoding.EncodeToString([]byte(content+title)) + "/" + files[i].Filename})
if err != nil {
fmt.Fprintf(w, "Fuck you man")
}
_, _ = io.Copy(out, file)
_, _ = io.WriteString(w, "File Upload Successful!")
}
jsondata, err := json.Marshal(fileList)
if err != nil {
fmt.Fprintf(w, "Fuck you man")
}
framework.InsertPOST("board", author[0], "notice", title, content, string(jsondata))
if err != nil {
fmt.Fprintf(w, "DB ERROR")
panic(err)
}
}
// GetPost is
func GetPost(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.FormValue("id"))
fmt.Printf(r.FormValue("id"))
if err != nil {
panic(err)
}
res := framework.GetPOST(id)
if res == "" {
} else {
fmt.Fprintf(w, res)
}
}
func main() {
framework.InitDB()
router := mux.NewRouter().StrictSlash(true)
api := router.PathPrefix("/api/").Subrouter()
api.HandleFunc("/login", login).Methods("POST")
api.HandleFunc("/upload", PostNotice).Methods("POST")
api.Path("/post").Queries("id", "{[0-9]*?}").HandlerFunc(GetPost).Methods("GET")
api.HandleFunc("/register", Register).Methods("POST")
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
log.Fatal(http.ListenAndServe(":8080", router))
}