Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e0137a

Browse files
added project skeleton
0 parents commit 9e0137a

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed

‎README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Description
2+
3+
This project is a *Go language* learning project with simple RestFul services. It has global array inside, not real DB connection so when server down added JSON's will be deleted.
4+
5+
# How can run?
6+
7+
First of all, you need to have *Go* in your computer. For Mac you can install with brew easily.
8+
9+
```
10+
brew install go
11+
```
12+
13+
If everything is OK, you should encounter an output like this at terminal when wrote *go version*.
14+
15+
```
16+
go version
17+
go version go1.9.2 darwin/amd64
18+
```
19+
20+
For run the project, in the project directory you need to write following command.
21+
22+
```
23+
go run main.go
24+
```
25+
26+
If everything works correctly, you can start the CRUD operations with following URL.
27+
28+
```
29+
http://127.0.0.1:3000
30+
```
31+
32+
# URL's and Example
33+
34+
List all of user (Need To Use GET method)
35+
```
36+
http://127.0.0.1:3000/getAll
37+
```
38+
Add new User with JSON type ((Need To Use POST method))
39+
```
40+
http://127.0.0.1:3000/newUser
41+
42+
{
43+
"id": 1,
44+
"name": "mockName",
45+
"surname": "mockSurname",
46+
"age": 30
47+
}
48+
```
49+
List one user with the given Id (Need To Use GET method)
50+
```
51+
http://127.0.0.1:3000/users/1
52+
```
53+
Update one user with the given Id (Need To Use PUT method)
54+
```
55+
http://127.0.0.1:3000/users/1
56+
```
57+
Delete one user with the given Id (Need To Use DELETE method)
58+
```
59+
http://127.0.0.1:3000/users/1
60+
```

‎main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"time"
6+
"Simple-GO-RestFul/muxes"
7+
)
8+
9+
func main() {
10+
11+
s := &http.Server{
12+
Addr: ":3000",
13+
Handler: muxes.SERVE(),
14+
ReadTimeout: 10 * time.Second,
15+
WriteTimeout: 10 * time.Second,
16+
MaxHeaderBytes: 1 << 20,
17+
}
18+
19+
s.ListenAndServe()
20+
21+
}

‎muxes/muxes.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package muxes
2+
3+
import (
4+
"net/http"
5+
"encoding/json"
6+
"io/ioutil"
7+
"strings"
8+
"strconv"
9+
"log"
10+
)
11+
12+
type (
13+
User struct {
14+
Id int
15+
Name string
16+
Surname string
17+
Age int
18+
}
19+
)
20+
21+
type Users = []User
22+
23+
var users = Users{
24+
User{
25+
0,
26+
"Fatih",
27+
"Totrakanlı",
28+
27,
29+
},
30+
}
31+
32+
func SERVE() *http.ServeMux {
33+
log.Print("Server started at http://127.0.0.1:3000 port.")
34+
mux := http.NewServeMux()
35+
36+
mux.HandleFunc("/newUser", func(w http.ResponseWriter, req *http.Request) {
37+
if req.Method != "POST" {
38+
http.NotFound(w, req)
39+
return
40+
}
41+
var newUser = convertRequestToUser(req)
42+
43+
okStatus(w)
44+
users = append(users, newUser)
45+
log.Printf("New User %s %s added successfully.", newUser.Name, newUser.Surname)
46+
json.NewEncoder(w).Encode(newUser)
47+
48+
return
49+
})
50+
51+
mux.HandleFunc("/getAll", func(w http.ResponseWriter, req *http.Request) {
52+
if req.Method != "GET" {
53+
http.NotFound(w, req)
54+
return
55+
}
56+
57+
okStatus(w)
58+
log.Printf("All users listed successfully.")
59+
json.NewEncoder(w).Encode(users)
60+
61+
return
62+
})
63+
64+
mux.HandleFunc("/users/", func(w http.ResponseWriter, req *http.Request) {
65+
var method = req.Method
66+
if method != "GET" && method != "DELETE" && method != "PUT" {
67+
http.NotFound(w, req)
68+
return
69+
}
70+
71+
id, err := strconv.Atoi(strings.TrimPrefix(req.URL.Path, "/users/"))
72+
if err != nil {
73+
panic(err)
74+
}
75+
76+
okStatus(w)
77+
78+
for index, user := range users {
79+
if user.Id == id {
80+
if method == "GET" {
81+
log.Printf("The user who is name %s %s listed successfully.", user.Name, user.Surname)
82+
json.NewEncoder(w).Encode(user)
83+
return
84+
}
85+
86+
if method == "DELETE" {
87+
users = remove(users, index)
88+
log.Printf("The user who is name %s %s deleted successfully.", user.Name, user.Surname)
89+
json.NewEncoder(w).Encode(users)
90+
return
91+
}
92+
93+
if method == "PUT" {
94+
var newUser = convertRequestToUser(req)
95+
users[index] = newUser
96+
log.Printf("The user who is name %s %s updated successfully.", user.Name, user.Surname)
97+
json.NewEncoder(w).Encode(users)
98+
return
99+
}
100+
101+
}
102+
}
103+
104+
json.NewEncoder(w).Encode(nil)
105+
return
106+
})
107+
108+
return mux
109+
}
110+
111+
func okStatus(w http.ResponseWriter) {
112+
w.Header().Set("Content-Type", "text/json; charset=utf-8")
113+
w.WriteHeader(http.StatusOK)
114+
115+
return
116+
}
117+
118+
func remove(slice Users, s int) Users {
119+
return append(slice[:s], slice[s+1:]...)
120+
}
121+
122+
func convertRequestToUser(req *http.Request) User {
123+
body, err := ioutil.ReadAll(req.Body)
124+
if err != nil {
125+
panic(err)
126+
}
127+
var newUser User
128+
err = json.Unmarshal(body, &newUser)
129+
if err != nil {
130+
panic(err)
131+
}
132+
133+
return newUser
134+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /