-
Notifications
You must be signed in to change notification settings - Fork 0
/
serv.go
116 lines (111 loc) · 3.06 KB
/
serv.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
108
109
110
111
112
113
114
115
116
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/iobear/uxt/uxt"
)
func handler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch {
case strings.HasPrefix(path, "/current"):
fmt.Fprint(w, uxt.GetCurrentUnixTime())
case strings.HasPrefix(path, "/since/"):
parts := strings.Split(path, "/")
if len(parts) != 3 {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
unixTime, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
http.Error(w, "Invalid timestamp", http.StatusBadRequest)
return
}
fmt.Fprint(w, uxt.GetTimeSince(unixTime))
case strings.HasPrefix(path, "/convert/"):
parts := strings.Split(path, "/")
if len(parts) < 3 {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
unixTime, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
http.Error(w, "Invalid timestamp", http.StatusBadRequest)
return
}
format := ""
if len(parts) == 4 {
format = parts[3]
}
result, err := uxt.ConvertUnixTimeToFormattedString(unixTime, format)
if err != nil {
http.Error(w, "Error converting timestamp", http.StatusInternalServerError)
return
}
fmt.Fprint(w, result)
case strings.HasPrefix(path, "/plus/"):
parts := strings.Split(path, "/")
if len(parts) != 3 {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
value, err := strconv.Atoi(parts[2])
if err != nil {
http.Error(w, "Invalid number", http.StatusBadRequest)
return
}
result, err := uxt.AdjustCurrentUnixTime(value)
if err != nil {
http.Error(w, "Error calculating time", http.StatusInternalServerError)
return
}
fmt.Fprint(w, result)
case strings.HasPrefix(path, "/minus/"):
parts := strings.Split(path, "/")
if len(parts) != 3 {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
value, err := strconv.Atoi(parts[2])
if err != nil {
http.Error(w, "Invalid number", http.StatusBadRequest)
return
}
result, err := uxt.AdjustCurrentUnixTime(-value) // Note the minus sign here to subtract
if err != nil {
http.Error(w, "Error calculating time", http.StatusInternalServerError)
return
}
fmt.Fprint(w, result)
case strings.HasPrefix(path, "/rfc3339/"):
parts := strings.Split(path, "/")
if len(parts) != 3 {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
value, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
http.Error(w, "Invalid number", http.StatusBadRequest)
return
}
result, err := uxt.ConvertUnixTimeToFormattedString(value, time.RFC3339)
if err != nil {
http.Error(w, "Error converting time", http.StatusInternalServerError)
return
}
fmt.Fprint(w, result)
default:
http.Error(w, "Not Found", http.StatusNotFound)
}
}
func RunServer(port string) {
http.HandleFunc("/", handler)
http.ListenAndServe(":"+port, nil)
}