-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
107 lines (96 loc) · 2.3 KB
/
example.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
logger "github.com/sirupsen/logrus"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
)
type ParsedJson struct {
Test1 struct {
Bla string `json:"bla"`
Foo string `json:"foo"`
} `json:"test1"`
Test2 []string `json:"test2"`
}
type Myyaml struct {
Http struct {
Port int `yaml:"port"`
} `yaml:"http"`
Path struct {
JSON string `yaml:"Json"`
Ok string `yaml:"OK"`
Params string `yaml:"Params"`
} `yaml:"path"`
Log struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
} `yaml:"log"`
}
func logging(myyaml string) {
level, err := logger.ParseLevel(myyaml)
if err != nil {
panic(err)
}
logger.SetLevel(level)
logger.SetFormatter(&logger.JSONFormatter{})
file, err := os.OpenFile("logs.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
logger.SetOutput(file)
}
func parseYaml() Myyaml {
var myyaml Myyaml
yamlFile, err := ioutil.ReadFile("/workspaces/rest/myyaml.yml")
test := yaml.Unmarshal([]byte(yamlFile), &myyaml)
if test != nil {
panic(err)
}
return myyaml
}
func returnRes(w http.ResponseWriter, r *http.Request) {
logger.Info("called returnRes")
w.WriteHeader(200)
}
func param(w http.ResponseWriter, r *http.Request) {
logger.Info("called param")
query := r.URL.Query().Get("param1")
if len(query) <= 0 {
w.WriteHeader(400)
w.Write([]byte("param error"))
logger.Error("param error")
return
}
w.WriteHeader(200)
w.Write([]byte(query))
}
func postJson(w http.ResponseWriter, r *http.Request) {
logger.Info("called postJson")
w.Header().Set("Content-Type", "application/json")
var parsedJson ParsedJson
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&parsedJson)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("json error"))
logger.Error("json error")
return
}
w.WriteHeader(200)
responseString := parsedJson.Test1.Foo + ": " + strings.Join(parsedJson.Test2, ",")
fmt.Println(responseString)
}
func main() {
myyaml := parseYaml()
logging(myyaml.Log.Level)
r := mux.NewRouter()
r.HandleFunc(fmt.Sprint(myyaml.Path.Params), param)
r.HandleFunc(fmt.Sprint(myyaml.Path.Ok), returnRes)
r.HandleFunc(fmt.Sprint(myyaml.Path.JSON), postJson).Methods("POST")
http.ListenAndServe(":"+fmt.Sprint(myyaml.Http.Port), r)
}