-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (139 loc) · 2.81 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/julienschmidt/httprouter"
)
var templateDir = "templates"
const (
Name = "Alexandr Krykovliuk"
Email = "[email protected]"
Project = "Project"
Port = "33654"
)
// Copyright - handle name and email.
type Copyright struct {
Name, Email string
}
// Years - handle copyright years.
type Years struct {
From, To int
}
// Data - handle vars for licenes.
type Data struct {
Project string
Years
Copyright
}
func main() {
router := httprouter.New()
router.GET("/:license", License)
port, exsit := os.LookupEnv("PORT")
if !exsit {
port = Port
}
templatesBaseDir, exsit := os.LookupEnv("TEMPLATES")
if exsit {
templateDir = path.Join(templatesBaseDir, templateDir)
}
panic(http.ListenAndServe(net.JoinHostPort("localhost", port), router))
}
// License - return license.
func License(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
license := ps.ByName("license")
re := regexp.MustCompile(`\-\d$`)
if r := re.MatchString(license); r {
license += ".0"
}
lt := path.Join(templateDir, license+".txt")
if _, err := os.Stat(lt); os.IsNotExist(err) {
Error(w)
return
}
t, err := template.ParseFiles(lt)
if err != nil {
Error(w)
return
}
n, e, y, p := Params(r.URL.Query())
copy := Copyright{n, e}
data := &Data{p, y, copy}
if err := t.Execute(w, data); err != nil {
Error(w)
return
}
}
// Error - return 404 http status.
func Error(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Not Found.")
}
// Params - return query params if passed or defalts.
func Params(q url.Values) (string, string, Years, string) {
date := time.Now()
Year, _, _ := date.Date()
years := q.Get("y")
yearsList := strings.SplitN(years, "-", 2)
name := q.Get("n")
email := q.Get("e")
project := q.Get("p")
n, ok := Default(name, Name).(string)
if !ok {
n = name
}
e, ok := Default(email, Email).(string)
if !ok {
e = email
}
y, ok := Default(yearsList, Year).(Years)
if !ok {
y = Years{To: Year}
}
p, ok := Default(project, Project).(string)
if !ok {
p = project
}
return n, e, y, p
}
// Default - retrun default for false.
func Default(v interface{}, def interface{}) interface{} {
switch v.(type) {
default:
v = def
case bool:
if v == false {
v = def
}
case int:
if v.(int) <= 0 {
v = def
}
case string:
if v == "" {
v = def
}
case []string:
years, ok := v.([]string)
if !ok || len(years) == 0 || years[0] == "" {
v = Years{To: def.(int)}
break
}
y := Years{}
y.To, _ = strconv.Atoi(strings.Trim(years[0], " "))
if len(years) > 1 {
y.From, _ = strconv.Atoi(strings.Trim(years[0], " "))
y.To, _ = strconv.Atoi(strings.Trim(years[1], " "))
}
v = y
}
return v
}