2025-06-14 01:38:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2025-06-14 02:31:17 +02:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
2025-06-14 01:38:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Item struct {
|
2025-06-14 02:31:17 +02:00
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
2025-06-14 01:56:00 +02:00
|
|
|
type UserInfo struct {
|
2025-06-14 02:31:17 +02:00
|
|
|
Username string `json:"username"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Groups string `json:"groups"`
|
|
|
|
Headers map[string]string `json:"headers"`
|
2025-06-14 01:56:00 +02:00
|
|
|
}
|
|
|
|
|
2025-06-14 01:38:32 +02:00
|
|
|
var db *sql.DB
|
|
|
|
|
|
|
|
func initDB() {
|
2025-06-14 02:31:17 +02:00
|
|
|
var err error
|
|
|
|
db, err = sql.Open("sqlite", "items.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createTable := `
|
2025-06-14 01:38:32 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS items (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
name TEXT NOT NULL
|
|
|
|
);`
|
2025-06-14 02:31:17 +02:00
|
|
|
|
|
|
|
_, err = db.Exec(createTable)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to create table:", err)
|
|
|
|
}
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2025-06-14 02:31:17 +02:00
|
|
|
if len(os.Args) > 1 && os.Args[1] == "--watch" {
|
|
|
|
watchAndRestart()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize database
|
|
|
|
initDB()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Your actual API
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
http.HandleFunc("/randomnumber", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "%d", rand.Intn(1000))
|
|
|
|
})
|
2025-06-14 01:38:32 +02:00
|
|
|
|
|
|
|
http.HandleFunc("/slowtest", func(w http.ResponseWriter, r *http.Request) {
|
2025-06-14 02:31:17 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
fmt.Fprintf(w, "true")
|
2025-06-14 01:38:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/welcome", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "welcome to shinyspace play api testing")
|
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "POST" {
|
|
|
|
r.ParseMultipartForm(32 << 20) // 32MB max memory
|
|
|
|
text := r.FormValue("text")
|
|
|
|
fmt.Fprintf(w, "you said %s", text)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
fmt.Fprintf(w, "Only POST method allowed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Items API endpoints
|
|
|
|
http.HandleFunc("/items", handleItems)
|
|
|
|
http.HandleFunc("/items/", handleItemDelete)
|
2025-06-14 02:31:17 +02:00
|
|
|
|
2025-06-14 01:56:00 +02:00
|
|
|
// Auth endpoints
|
|
|
|
http.HandleFunc("/auth/user", handleAuthUser)
|
2025-06-14 02:31:17 +02:00
|
|
|
|
|
|
|
log.Println("API running on :3847")
|
|
|
|
http.ListenAndServe("127.0.0.1:3847", nil)
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func watchAndRestart() {
|
2025-06-14 02:31:17 +02:00
|
|
|
binaryPath, _ := os.Executable()
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
|
|
|
start := func() {
|
|
|
|
if cmd != nil && cmd.Process != nil {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
cmd = exec.Command(binaryPath)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
start() // Initial start
|
|
|
|
|
|
|
|
lastMod := getModTime(binaryPath)
|
|
|
|
for {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
if mod := getModTime(binaryPath); mod.After(lastMod) {
|
|
|
|
log.Println("Binary changed, restarting...")
|
|
|
|
lastMod = mod
|
|
|
|
start()
|
|
|
|
}
|
|
|
|
}
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getModTime(path string) time.Time {
|
2025-06-14 02:31:17 +02:00
|
|
|
info, _ := os.Stat(path)
|
|
|
|
return info.ModTime()
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleItems(w http.ResponseWriter, r *http.Request) {
|
2025-06-14 02:31:17 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
getItems(w, r)
|
|
|
|
case "POST":
|
|
|
|
createItem(w, r)
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
|
|
|
|
}
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getItems(w http.ResponseWriter, r *http.Request) {
|
2025-06-14 02:31:17 +02:00
|
|
|
rows, err := db.Query("SELECT id, name FROM items ORDER BY id")
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to fetch items"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var items []Item
|
|
|
|
for rows.Next() {
|
|
|
|
var item Item
|
|
|
|
err := rows.Scan(&item.ID, &item.Name)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to scan items"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we always return an array, even if empty
|
|
|
|
if items == nil {
|
|
|
|
items = []Item{}
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(items)
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createItem(w http.ResponseWriter, r *http.Request) {
|
2025-06-14 02:31:17 +02:00
|
|
|
var item Item
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&item)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid JSON"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Name == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Name is required"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := db.Exec("INSERT INTO items (name) VALUES (?)", item.Name)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to create item"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := result.LastInsertId()
|
|
|
|
item.ID = int(id)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
json.NewEncoder(w).Encode(item)
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleItemDelete(w http.ResponseWriter, r *http.Request) {
|
2025-06-14 02:31:17 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if r.Method != "DELETE" {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract ID from URL path /items/{id}
|
|
|
|
path := strings.TrimPrefix(r.URL.Path, "/items/")
|
|
|
|
id, err := strconv.Atoi(path)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid item ID"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := db.Exec("DELETE FROM items WHERE id = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to delete item"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, _ := result.RowsAffected()
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Item not found"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "Item deleted successfully"})
|
2025-06-14 01:38:32 +02:00
|
|
|
}
|
2025-06-14 01:56:00 +02:00
|
|
|
|
|
|
|
func handleAuthUser(w http.ResponseWriter, r *http.Request) {
|
2025-06-14 02:31:17 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if r.Method != "GET" {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all headers for debugging
|
|
|
|
headers := make(map[string]string)
|
|
|
|
for name, values := range r.Header {
|
|
|
|
headers[name] = strings.Join(values, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract common Authelia headers
|
|
|
|
userInfo := UserInfo{
|
|
|
|
Username: r.Header.Get("Remote-User"),
|
|
|
|
Name: r.Header.Get("Remote-Name"),
|
|
|
|
Email: r.Header.Get("Remote-Email"),
|
|
|
|
Groups: r.Header.Get("Remote-Groups"),
|
|
|
|
Headers: headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(userInfo)
|
2025-06-14 01:56:00 +02:00
|
|
|
}
|