|
| 1 | +package flint |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/md5" |
| 5 | + "crypto/sha256" |
| 6 | + "crypto/sha512" |
| 7 | + "encoding/hex" |
| 8 | + "encoding/json" |
| 9 | + "html/template" |
| 10 | + "mime/multipart" |
| 11 | + "net/http" |
| 12 | + "strconv" |
| 13 | + "sync" |
| 14 | + |
| 15 | + "golang.org/x/crypto/bcrypt" |
| 16 | +) |
| 17 | + |
| 18 | +type Context struct { |
| 19 | + Writer http.ResponseWriter |
| 20 | + Request *http.Request |
| 21 | +} |
| 22 | + |
| 23 | +func (c *Context) FormBcrypt(key string) string { |
| 24 | + password := c.FormData(key) // Formdan gelen veriyi al |
| 25 | + cost := 12 |
| 26 | + hash, err := bcrypt.GenerateFromPassword([]byte(password), cost) |
| 27 | + if err != nil { |
| 28 | + LogError("FormBcrypt()", err.Error()) |
| 29 | + return "" |
| 30 | + } |
| 31 | + return string(hash) |
| 32 | +} |
| 33 | + |
| 34 | +func (c *Context) Redirect(status int, url string) { |
| 35 | + http.Redirect(c.Writer, c.Request, url, status) |
| 36 | +} |
| 37 | + |
| 38 | +func (c *Context) FormFile(key string) (multipart.File, *multipart.FileHeader, error) { |
| 39 | + return c.Request.FormFile(key) |
| 40 | +} |
| 41 | + |
| 42 | +func (c *Context) FormData(key string) string { |
| 43 | + return c.Request.FormValue(key) |
| 44 | +} |
| 45 | + |
| 46 | +func (c *Context) FormInt(key string) int { |
| 47 | + val := c.FormData(key) |
| 48 | + num, err := strconv.Atoi(val) |
| 49 | + if err != nil { |
| 50 | + LogError("FormInt()", err.Error()) |
| 51 | + return 0 |
| 52 | + } |
| 53 | + return num |
| 54 | +} |
| 55 | + |
| 56 | +func (c *Context) FormMD5(key string) string { |
| 57 | + hash := md5.Sum([]byte(c.FormData(key))) |
| 58 | + return hex.EncodeToString(hash[:]) |
| 59 | +} |
| 60 | + |
| 61 | +func (c *Context) FormSHA256(key string) string { |
| 62 | + hash := sha256.Sum256([]byte(c.FormData(key))) |
| 63 | + return hex.EncodeToString(hash[:]) |
| 64 | +} |
| 65 | + |
| 66 | +func (c *Context) FormSHA512(key string) string { |
| 67 | + hash := sha512.Sum512([]byte(c.FormData(key))) |
| 68 | + return hex.EncodeToString(hash[:]) |
| 69 | +} |
| 70 | + |
| 71 | +func (c *Context) Delete() bool { |
| 72 | + return c.Request.Method == http.MethodDelete |
| 73 | +} |
| 74 | + |
| 75 | +func (c *Context) Post() bool { |
| 76 | + return c.Request.Method == http.MethodPost |
| 77 | +} |
| 78 | + |
| 79 | +func (c *Context) Get() bool { |
| 80 | + return c.Request.Method == http.MethodGet |
| 81 | +} |
| 82 | + |
| 83 | +func (c *Context) File(filepath string) { |
| 84 | + http.ServeFile(c.Writer, c.Request, filepath) |
| 85 | +} |
| 86 | + |
| 87 | +func (c *Context) JSON(status int, data interface{}) { |
| 88 | + c.Writer.Header().Set("Content-Type", "application/json") |
| 89 | + c.Writer.WriteHeader(status) |
| 90 | + json.NewEncoder(c.Writer).Encode(data) |
| 91 | +} |
| 92 | + |
| 93 | +func (c *Context) String(status int, text string) { |
| 94 | + c.Writer.Header().Set("Content-Type", "text/plain") |
| 95 | + c.Writer.WriteHeader(status) |
| 96 | + c.Writer.Write([]byte(text)) |
| 97 | +} |
| 98 | + |
| 99 | +// Template cache |
| 100 | +var tmplCache = make(map[string]*template.Template) |
| 101 | +var tmplLock sync.RWMutex |
| 102 | + |
| 103 | +func (c *Context) HTML(status int, tmplPath string, data interface{}) { |
| 104 | + tmplLock.RLock() |
| 105 | + tmpl := tmplCache[tmplPath] |
| 106 | + tmplLock.RUnlock() |
| 107 | + |
| 108 | + if tmpl == nil { |
| 109 | + t, err := template.ParseFiles(tmplPath) |
| 110 | + if err != nil { |
| 111 | + http.Error(c.Writer, err.Error(), 500) |
| 112 | + return |
| 113 | + } |
| 114 | + tmplLock.Lock() |
| 115 | + tmplCache[tmplPath] = t |
| 116 | + tmplLock.Unlock() |
| 117 | + tmpl = t |
| 118 | + } |
| 119 | + |
| 120 | + c.Writer.Header().Set("Content-Type", "text/html") |
| 121 | + c.Writer.WriteHeader(status) |
| 122 | + tmpl.Execute(c.Writer, data) |
| 123 | +} |
| 124 | + |
| 125 | +func (c *Context) Default404() { |
| 126 | + c.Writer.WriteHeader(http.StatusNotFound) |
| 127 | + c.Writer.Write([]byte(` |
| 128 | +<head> |
| 129 | + <meta charset="UTF-8"> |
| 130 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 131 | + <title>Page Not Found - 404 Error Flint</title> |
| 132 | + <style> |
| 133 | + :root { |
| 134 | + --primary-color: #218c87; |
| 135 | + --primary-dark: #1a6f6b; |
| 136 | + --primary-light: #e8f4f3; |
| 137 | + --text-color: #333; |
| 138 | + --white: #ffffff; |
| 139 | + } |
| 140 | + |
| 141 | + * { |
| 142 | + margin: 0; |
| 143 | + padding: 0; |
| 144 | + box-sizing: border-box; |
| 145 | + font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
| 146 | + } |
| 147 | + |
| 148 | + body { |
| 149 | + background-color: #f9f9f9; |
| 150 | + color: var(--text-color); |
| 151 | + display: flex; |
| 152 | + flex-direction: column; |
| 153 | + align-items: center; |
| 154 | + justify-content: center; |
| 155 | + height: 100vh; |
| 156 | + text-align: center; |
| 157 | + padding: 20px; |
| 158 | + } |
| 159 | + |
| 160 | + .container { |
| 161 | + max-width: 600px; |
| 162 | + width: 100%; |
| 163 | + } |
| 164 | + |
| 165 | + .error-image { |
| 166 | + width: 200px; |
| 167 | + height: 200px; |
| 168 | + margin-bottom: 30px; |
| 169 | + object-fit: contain; |
| 170 | + } |
| 171 | + |
| 172 | + h1 { |
| 173 | + font-size: 5rem; |
| 174 | + color: var(--primary-color); |
| 175 | + margin-bottom: 20px; |
| 176 | + } |
| 177 | + |
| 178 | + h2 { |
| 179 | + font-size: 1.8rem; |
| 180 | + margin-bottom: 15px; |
| 181 | + color: var(--text-color); |
| 182 | + } |
| 183 | + |
| 184 | + p { |
| 185 | + font-size: 1.1rem; |
| 186 | + margin-bottom: 30px; |
| 187 | + line-height: 1.6; |
| 188 | + color: #666; |
| 189 | + } |
| 190 | + |
| 191 | + .btn { |
| 192 | + display: inline-block; |
| 193 | + background-color: var(--primary-color); |
| 194 | + color: var(--white); |
| 195 | + padding: 12px 30px; |
| 196 | + border-radius: 50px; |
| 197 | + text-decoration: none; |
| 198 | + font-weight: 600; |
| 199 | + transition: all 0.3s ease; |
| 200 | + border: 2px solid var(--primary-color); |
| 201 | + margin: 0 10px; |
| 202 | + } |
| 203 | + |
| 204 | + .btn:hover { |
| 205 | + background-color: var(--primary-dark); |
| 206 | + border-color: var(--primary-dark); |
| 207 | + transform: translateY(-2px); |
| 208 | + box-shadow: 0 5px 15px rgba(33, 140, 135, 0.3); |
| 209 | + } |
| 210 | + |
| 211 | + .btn-outline { |
| 212 | + background-color: transparent; |
| 213 | + color: var(--primary-color); |
| 214 | + } |
| 215 | + |
| 216 | + .btn-outline:hover { |
| 217 | + background-color: var(--primary-light); |
| 218 | + color: var(--primary-dark); |
| 219 | + } |
| 220 | + |
| 221 | + .animation { |
| 222 | + animation: float 3s ease-in-out infinite; |
| 223 | + } |
| 224 | + |
| 225 | + @keyframes float { |
| 226 | + 0%, 100% { |
| 227 | + transform: translateY(0); |
| 228 | + } |
| 229 | + 50% { |
| 230 | + transform: translateY(-15px); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @media (max-width: 768px) { |
| 235 | + h1 { |
| 236 | + font-size: 3.5rem; |
| 237 | + } |
| 238 | + |
| 239 | + h2 { |
| 240 | + font-size: 1.4rem; |
| 241 | + } |
| 242 | + |
| 243 | + .btn { |
| 244 | + display: block; |
| 245 | + margin: 10px auto; |
| 246 | + width: 80%; |
| 247 | + max-width: 250px; |
| 248 | + } |
| 249 | + } |
| 250 | + </style> |
| 251 | +</head> |
| 252 | +<body> |
| 253 | + <div class="container"> |
| 254 | + <!-- Add your own image here --> |
| 255 | + <img src="assets/flint.jpg" alt="404 Error" class="error-image animation"> |
| 256 | + |
| 257 | + <h1>404</h1> |
| 258 | + <h2>Page Not Found</h2> |
| 259 | + <p>The page you are looking for might have been removed, had its name changed, or is temporarily unavailable. Please try to find what you need from our homepage.</p> |
| 260 | + |
| 261 | + <div class="buttons"> |
| 262 | + <a href="/" class="btn">Return to Homepage</a> |
| 263 | + <a href="https://t.me/Grayvort3x" class="btn btn-outline">Contact Support</a> |
| 264 | + </div> |
| 265 | + </div> |
| 266 | +
|
| 267 | + <script> |
| 268 | + // Simple animation effects |
| 269 | + document.addEventListener('DOMContentLoaded', function() { |
| 270 | + const image = document.querySelector('.error-image'); |
| 271 | + |
| 272 | + // Pause animation on hover |
| 273 | + image.addEventListener('mouseenter', function() { |
| 274 | + this.style.animationPlayState = 'paused'; |
| 275 | + }); |
| 276 | + |
| 277 | + // Resume animation when mouse leaves |
| 278 | + image.addEventListener('mouseleave', function() { |
| 279 | + this.style.animationPlayState = 'running'; |
| 280 | + }); |
| 281 | + |
| 282 | + // Button click effect |
| 283 | + const buttons = document.querySelectorAll('.btn'); |
| 284 | + buttons.forEach(button => { |
| 285 | + button.addEventListener('click', function() { |
| 286 | + this.style.transform = 'scale(0.95)'; |
| 287 | + setTimeout(() => { |
| 288 | + this.style.transform = 'scale(1)'; |
| 289 | + }, 200); |
| 290 | + }); |
| 291 | + }); |
| 292 | + }); |
| 293 | + </script> |
| 294 | +</body> |
| 295 | +</html> |
| 296 | + `)) |
| 297 | +} |
| 298 | + |
| 299 | +func (c *Context) Template404(templatePath string, data ...any) { |
| 300 | + var d any |
| 301 | + if len(data) > 0 { |
| 302 | + d = data[0] |
| 303 | + } else { |
| 304 | + d = map[string]any{} |
| 305 | + } |
| 306 | + c.HTML(http.StatusNotFound, templatePath, d) |
| 307 | +} |
0 commit comments