-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (53 loc) · 1 KB
/
main.go
File metadata and controls
60 lines (53 loc) · 1 KB
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
package main
import (
"assignment-3/handler"
"assignment-3/model"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/mux"
)
func writefile() {
data := model.RandomValueStatus()
file, _ := json.MarshalIndent(data, "", "")
err := ioutil.WriteFile("file/status.json", file, 0644)
if err != nil {
fmt.Printf("%v \n", err)
}
}
func main() {
wd, _ := os.Getwd()
//check is debug true
if strings.Contains(wd, "cmd") {
os.Chdir("..")
os.Chdir("..")
}
ticker := time.NewTicker(14 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
writefile()
case <-quit:
ticker.Stop()
return
}
}
}()
r := mux.NewRouter()
r.HandleFunc("/assignment3", handler.MainHandler)
fmt.Println("Now listening on port 127.0.0.1:8080")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}