2024-11-06 22:38:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
// Author:Mateusz Kędziora https://mkedziora.pl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-11-07 10:50:03 +00:00
|
|
|
"log"
|
|
|
|
"mkedziora/fast-links/kvstore"
|
2024-11-06 22:38:59 +00:00
|
|
|
"net/http"
|
2024-11-07 20:47:12 +00:00
|
|
|
"os"
|
2024-11-06 22:38:59 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-11-07 10:50:03 +00:00
|
|
|
var store *kvstore.KVStore
|
2024-11-06 22:38:59 +00:00
|
|
|
|
|
|
|
func init() {
|
2024-11-07 10:50:03 +00:00
|
|
|
var err error
|
2024-11-07 20:47:12 +00:00
|
|
|
filePath := os.Getenv("DATA_FILE_PATH")
|
|
|
|
store, err = kvstore.NewKVStore(filePath)
|
2024-11-07 10:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create KVStore: %v", err)
|
|
|
|
}
|
2024-11-06 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var input struct {
|
|
|
|
Data string `json:"value"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Expire string `json:"expire,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
|
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-07 10:50:03 +00:00
|
|
|
// mu.Lock()
|
2024-11-06 22:38:59 +00:00
|
|
|
if input.ID == "" {
|
|
|
|
input.ID = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
expireString, err := strconv.Atoi(input.Expire)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expireSeconds := 30
|
|
|
|
if expireString > 0 {
|
|
|
|
if expireString > 86400 {
|
|
|
|
http.Error(w, fmt.Sprintf("Maksymalny czas wygaśnięcia to %d sekund", 86400), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expireSeconds = expireString
|
|
|
|
}
|
|
|
|
|
|
|
|
expirationTime := time.Now().Add(time.Duration(expireSeconds) * time.Second)
|
|
|
|
|
2024-11-07 10:50:03 +00:00
|
|
|
store.SetWithTimestamp("key_"+input.ID, input.Data, expirationTime)
|
|
|
|
// dataMap[input.ID] = DataEntry{
|
|
|
|
// Value: input.Data,
|
|
|
|
// Timestamp: expirationTime,
|
|
|
|
// }
|
|
|
|
// mu.Unlock()
|
2024-11-06 22:38:59 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := r.URL.Query().Get("id")
|
|
|
|
if id == "" {
|
|
|
|
id = "default"
|
|
|
|
}
|
|
|
|
|
2024-11-07 10:50:03 +00:00
|
|
|
// mu.Lock()
|
|
|
|
// entry, exists := dataMap[id]
|
|
|
|
// mu.Unlock()
|
|
|
|
|
|
|
|
entry, exists := store.Get("key_" + id)
|
2024-11-06 22:38:59 +00:00
|
|
|
|
|
|
|
if !exists || time.Since(entry.Timestamp) > 30*time.Second {
|
2024-11-07 10:50:03 +00:00
|
|
|
store.Delete("key_" + id)
|
2024-11-06 22:38:59 +00:00
|
|
|
http.Error(w, "No data available", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"data": entry.Value})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUrlHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := r.URL.Query().Get("id")
|
|
|
|
if id == "" {
|
|
|
|
id = "default"
|
|
|
|
}
|
|
|
|
|
2024-11-07 10:50:03 +00:00
|
|
|
entry, exists := store.Get("key_" + id)
|
2024-11-06 22:38:59 +00:00
|
|
|
if !exists || time.Since(entry.Timestamp) > 30*time.Second {
|
2024-11-07 10:50:03 +00:00
|
|
|
store.Delete("key_" + id)
|
2024-11-06 22:38:59 +00:00
|
|
|
http.Error(w, "No data available", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Referrer-Policy", "no-referrer")
|
|
|
|
http.Redirect(w, r, entry.Value, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-11-07 10:50:03 +00:00
|
|
|
|
2024-11-06 22:38:59 +00:00
|
|
|
// Obsługa endpointów API
|
|
|
|
http.HandleFunc("/api/set", setHandler)
|
|
|
|
http.HandleFunc("/api/get", getHandler)
|
|
|
|
http.HandleFunc("/api/url", getUrlHandler)
|
|
|
|
|
|
|
|
// Serwowanie plików statycznych
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.ServeFile(w, r, "public/index.html")
|
|
|
|
} else {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "public/app.html")
|
|
|
|
})
|
|
|
|
|
2024-11-07 00:39:39 +00:00
|
|
|
http.HandleFunc("/css/index.css", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "public/css/index.css")
|
2024-11-06 22:38:59 +00:00
|
|
|
})
|
|
|
|
|
2024-11-07 00:39:39 +00:00
|
|
|
http.HandleFunc("/css/app.css", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "public/css/app.css")
|
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/js/index.js", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "public/js/index.js")
|
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/js/app.js", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "public/js/app.js")
|
2024-11-06 22:38:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2024-11-07 10:50:03 +00:00
|
|
|
|
|
|
|
for id, timestamp := range store.GetAll() {
|
2024-11-06 22:38:59 +00:00
|
|
|
if time.Now().After(timestamp.Timestamp) {
|
2024-11-07 10:50:03 +00:00
|
|
|
store.Delete(id)
|
2024-11-06 22:38:59 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Uruchomienie serwera
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|