-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
104 lines (92 loc) · 3.01 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
import (
"encoding/json"
"fmt"
"log"
"net/http"
"gopkg.in/mgo.v2/bson"
"github.com/gorilla/mux"
. "github.com/moezilla/antispam-api/config"
. "github.com/moezilla/antispam-api/db"
. "github.com/moezilla/antispam-api/type"
}
func init() {
fmt.Println("Server Starting...")
}
var config = Config{}
var db = MoviesDB{}
func respondWithError(scammer http.ResponseWriter, code int, msg string) {
respondWithJSON(scammer, code, map[string]string{"error": msg})
}
func respondWithJSON(scammer http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
scammer.Header().Set("Content-Type", "application/json")
scammer.WriteHeader(code)
scammer.Write(response)
}
func FindScammer(scammer http.ResponseWriter, router *http.Request) {
params := mux.Vars(router)
antispam, err := db.FindById(params["id"])
if err != nil {
respondWithError(scammer, http.StatusBadRequest, "Who is this user")
return
}
respondWithJson(scammer, http.StatusOK, antispam)
}
func BanScammer(scammer http.ResponseWriter, router *http.Request) {
defer router.Body.Close()
var antispam Antispam
if err := json.NewDecoder(router.Body).Decode(&antispam); err != nil {
respondWithError(scammer, http.StatusBadRequest, "Failed Request")
return
}
antispam.ID = bson.NewObjectId()
if err := db.Insert(antispam); err != nil {
respondWithError(scammer, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(scammer, http.StatusCreated, antispam)
}
func UpdateBanScammer(scammer http.ResponseWriter, router *http.Request) {
defer router.Body.Close()
var antispam Antispam
if err := json.NewDecoder(router.Body).Decode(&antispam); err != nil {
respondWithError(scammer, http.StatusBadRequest, "Failed Scammer")
return
}
if err := db.Update(antispam); err != nil {
respondWithError(scammer, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(scammer, http.StatusOK, map[string]string{"success"})
}
func UnbanScammer(scammer http.ResponseWriter, router *http.Request) {
defer router.Body.Close()
var antispam Antispam
if err := json.NewDecoder(router.Body).Decode(&antispam); err != nil {
respondWithError(scammer, http.StatusBadRequest, "Failed Request")
return
}
if err := db.Delete(antispam); err != nil {
respondWithError(scammer, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(scammer, http.StatusOK, map[string]string{"success"})
}
func init() {
config.Read()
db.Server = config.Server
fmt.Println("Server Name:", config.Server)
db.Database = config.Database
fmt.Println("Database:", config.Database)
db.Connect()
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/banscammer", BanScammer).Methods("POST")
router.HandleFunc("/banscammer", UpdateBanScammer).Methods("PUT")
router.HandleFunc("/unban", UnbanScammer).Methods("DELETE")
router.HandleFunc("/findscammer/{id}", FindScammer).Methods("GET")
var port = ":3000"
fmt.Println("Server running in port:", port)
log.Fatal(http.ListenAndServe(port, router))
}