-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
143 lines (127 loc) · 2.59 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime/debug"
"gopkg.in/mgo.v2"
)
var mongodbSession *mgo.Session
const DATABASE = "sample"
const MongoDBURL = "localhost:27017"
type User struct {
Username string `json:"username"`
}
type ResponseMessage struct {
Name string `json:"name"`
Message string `json:"message"`
}
func parseRequestBody(req *http.Request) []byte {
if body, err := ioutil.ReadAll(req.Body); err != nil {
panic(err)
} else {
return body
}
}
func setResponseJSON(w http.ResponseWriter, body interface{}) {
if response, err := json.Marshal(body); err != nil {
panic(err)
} else {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(response))
}
}
func setupHelloRoute() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
u := User{
Username: "Mohammad Haghighat",
}
setResponseJSON(w, u)
} else if r.Method == "GET" {
fmt.Fprintf(w, "hello world")
} else {
fmt.Fprintf(w, "method not allowed")
}
})
}
func setupWorldRoute() {
http.HandleFunc("/world", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
u := User{}
if err := json.Unmarshal(parseRequestBody(r), &u); err != nil {
panic(err)
}
if u.Username == "mhg" {
setResponseJSON(w, ResponseMessage{
Name: "Success",
Message: "Hello Mohammad",
})
} else {
fmt.Fprintf(w, "unknown user")
}
} else {
fmt.Fprintf(w, "method not allowed")
}
})
}
func setupHandlers() {
setupHelloRoute()
setupWorldRoute()
}
func setupDatabaseConnection() {
session, err := mgo.Dial(MongoDBURL)
if err != nil {
panic(err)
}
mongodbSession = session
}
func recoverRuntimeErrors() {
if e := recover(); e != nil {
log.Printf("[runtime error %q, stack trace is bellow] %v",
e, debug.Stack())
}
}
func main() {
setupDatabaseConnection()
setupHandlers()
recoverRuntimeErrors()
fmt.Println(calculate([][]int{
[]int{15, 100, -1},
[]int{51, 41, 3},
[]int{1, 31, 12},
}))
http.ListenAndServe("localhost:8080", nil)
}
func calculate(grid [][]int) []int {
result := []int{}
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[i]); j++ {
num := grid[i][j]
var isMin bool = true
for k := 0; k < len(grid); k++ {
if num > grid[k][j] {
isMin = false
break
}
}
if !isMin {
continue
}
isMin = true
for k := 0; k < len(grid[i]); k++ {
if num < grid[i][k] {
isMin = false
break
}
}
if !isMin {
continue
}
result = append(result, num)
}
}
return result
}