forked from kwsorensen/loggy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (57 loc) · 1.5 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
package main
import (
"encoding/json"
"fmt"
"log"
loggio "loggio"
"net/http"
"github.com/google/uuid"
)
var Logs []Log
type Log struct {
Type string `json:"Type"`
Id uuid.UUID `json:"id"`
}
func defaultHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to loggy!\n")
}
func createLog(logType string) {
var logStruct Log
logUUID, err := uuid.NewUUID() // generate new UUID for the log
if err != nil {
log.Fatalln("Fatal Error Creating UUID:", err)
}
logStruct.Id = logUUID
logStruct.Type = logType
Logs = append(Logs, logStruct)
}
func returnLoggers(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(Logs)
if err != nil {
log.Fatal("Error Returning Logs: ", err)
}
fmt.Fprintln(w, string(data))
}
func defaultLogHandler(w http.ResponseWriter, r *http.Request) {
createLog("Default")
fmt.Fprintf(w, "Default Log Handler Event started")
log.Println("New Default Log Handler Starting...")
go loggio.CreateDefaultLog()
}
func jsonLogHandler(w http.ResponseWriter, r *http.Request) {
createLog("JSON")
fmt.Fprintf(w, "JSON Log Handler Event started")
log.Println("New JSON Log Handler Starting...")
go loggio.CreateJSONLog()
}
func main() {
http.HandleFunc("/", defaultHandler)
http.HandleFunc("/createDefaultLog", defaultLogHandler)
http.HandleFunc("/createJSONLog", jsonLogHandler)
http.HandleFunc("/getLogs", returnLoggers)
log.Println("Log Generator Starting!")
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}