Skip to content

Commit f032648

Browse files
authored
Merge pull request #148 from Go-In/dev
Dev
2 parents 3e5d016 + 260669c commit f032648

File tree

7 files changed

+86
-78
lines changed

7 files changed

+86
-78
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66

77
esdata/nodes
88
esdata/elasticsearch
9+
codegen/codegen

codegen/data.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
type Data struct {
4+
Price string
5+
Currency string
6+
Reuse string
7+
}

codegen/entrypoint.sh

-2
This file was deleted.

codegen/httphandle.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/go-redis/redis"
10+
)
11+
12+
func homePage(w http.ResponseWriter, r *http.Request) {
13+
var m Payload
14+
m.Status = "OK"
15+
b, _ := json.Marshal(m)
16+
17+
w.Header().Set("Content-Type", "application/json")
18+
19+
fmt.Fprintf(w, string(b))
20+
}
21+
22+
func save(w http.ResponseWriter, r *http.Request) {
23+
data := Data{r.PostFormValue("price"), r.PostFormValue("currency"), r.PostFormValue("reuse")}
24+
key := RandStringRunes(13)
25+
dataToStr, _ := json.Marshal(data)
26+
err := client.Set(key, string(dataToStr), 0).Err()
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
resPayload := Payload{"OK", string(key), data}
32+
33+
res, err := json.Marshal(resPayload)
34+
35+
w.Header().Set("Content-Type", "application/json")
36+
fmt.Fprintf(w, string(res))
37+
}
38+
39+
func load(w http.ResponseWriter, r *http.Request) {
40+
key := r.PostFormValue("key")
41+
val, err := client.Get(key).Result()
42+
var data Data
43+
44+
resPayload := Payload{Key: key}
45+
if err == redis.Nil {
46+
resPayload.Status = "NOT_FOUND"
47+
} else if err != nil {
48+
resPayload.Status = "INTERNAL_SERVER_ERROR"
49+
} else {
50+
error := json.Unmarshal([]byte(val), &data)
51+
if error != nil {
52+
panic(error)
53+
}
54+
resPayload.Value = data
55+
resPayload.Status = "OK"
56+
57+
}
58+
res, err := json.Marshal(resPayload)
59+
w.Header().Set("Content-Type", "application/json")
60+
fmt.Fprintf(w, string(res))
61+
62+
}
63+
64+
func handleRequests() {
65+
http.HandleFunc("/", homePage)
66+
http.HandleFunc("/save", save)
67+
http.HandleFunc("/load", load)
68+
fmt.Println("Listen and serve on PORT 8081")
69+
log.Fatal(http.ListenAndServe(":8081", nil))
70+
}

codegen/main.go

-75
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"log"
75
"math/rand"
8-
"net/http"
96
"time"
107

118
"github.com/go-redis/redis"
129
)
1310

14-
type Data struct {
15-
Price string
16-
Currency string
17-
Reuse string
18-
}
19-
20-
type Payload struct {
21-
Status string
22-
Key string
23-
Value Data
24-
}
25-
2611
func init() {
2712
NewClient()
2813
rand.Seed(time.Now().UnixNano())
@@ -50,66 +35,6 @@ func NewClient() {
5035
fmt.Println(pong, err)
5136
}
5237

53-
func homePage(w http.ResponseWriter, r *http.Request) {
54-
var m Payload
55-
m.Status = "OK"
56-
b, _ := json.Marshal(m)
57-
58-
w.Header().Set("Content-Type", "application/json")
59-
60-
fmt.Fprintf(w, string(b))
61-
}
62-
63-
func save(w http.ResponseWriter, r *http.Request) {
64-
data := Data{r.PostFormValue("price"), r.PostFormValue("currency"), r.PostFormValue("reuse")}
65-
key := RandStringRunes(13)
66-
dataToStr, _ := json.Marshal(data)
67-
err := client.Set(key, string(dataToStr), 0).Err()
68-
if err != nil {
69-
panic(err)
70-
}
71-
72-
resPayload := Payload{"OK", string(key), data}
73-
74-
res, err := json.Marshal(resPayload)
75-
76-
w.Header().Set("Content-Type", "application/json")
77-
fmt.Fprintf(w, string(res))
78-
}
79-
80-
func load(w http.ResponseWriter, r *http.Request) {
81-
key := r.PostFormValue("key")
82-
val, err := client.Get(key).Result()
83-
var data Data
84-
85-
resPayload := Payload{Key: key}
86-
if err == redis.Nil {
87-
resPayload.Status = "NOT_FOUND"
88-
} else if err != nil {
89-
resPayload.Status = "INTERNAL_SERVER_ERROR"
90-
} else {
91-
error := json.Unmarshal([]byte(val), &data)
92-
if error != nil {
93-
panic(error)
94-
}
95-
resPayload.Value = data
96-
resPayload.Status = "OK"
97-
98-
}
99-
res, err := json.Marshal(resPayload)
100-
w.Header().Set("Content-Type", "application/json")
101-
fmt.Fprintf(w, string(res))
102-
103-
}
104-
105-
func handleRequests() {
106-
http.HandleFunc("/", homePage)
107-
http.HandleFunc("/save", save)
108-
http.HandleFunc("/load", load)
109-
fmt.Println("Listen and serve on PORT 8081")
110-
log.Fatal(http.ListenAndServe(":8081", nil))
111-
}
112-
11338
func main() {
11439
handleRequests()
11540
}

codegen/payload.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
type Payload struct {
4+
Status string
5+
Key string
6+
Value Data
7+
}

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ services:
5454
- "8081:8081"
5555
volumes:
5656
- ./codegen:/go/src/codegen
57-
command: go run main.go
57+
command: /bin/ash -c "go build; ./codegen"

0 commit comments

Comments
 (0)