|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +type ShortcodeConfig struct { |
| 15 | + Shortcodes map[string]string `json:"shortcodes"` |
| 16 | + Icons map[string]string `json:"icons"` |
| 17 | +} |
| 18 | + |
| 19 | +type ServerConfig struct { |
| 20 | + Port int `json:"port"` |
| 21 | + ShowURLOnStart bool `json:"showURLOnStart"` |
| 22 | + GermanFolder string `json:"germanFolder"` |
| 23 | + EnglishFolder string `json:"englishFolder"` |
| 24 | +} |
| 25 | + |
| 26 | +type Config struct { |
| 27 | + // Shortcodes ShortcodeConfig `json:"shortcodes"` |
| 28 | + Shortcodes map[string]string `json:"shortcodes"` |
| 29 | + Icons map[string]string `json:"icons"` |
| 30 | + Server ServerConfig `json:"server"` |
| 31 | +} |
| 32 | + |
| 33 | +var config Config |
| 34 | + |
| 35 | +func main() { |
| 36 | + // Load configuration |
| 37 | + loadConfig() |
| 38 | + |
| 39 | + // Serve static files |
| 40 | + fs := http.FileServer(http.Dir("static")) |
| 41 | + http.Handle("/", fs) |
| 42 | + |
| 43 | + // API endpoints |
| 44 | + http.HandleFunc("/api/config", handleConfig) |
| 45 | + http.HandleFunc("/api/save", handleSave) |
| 46 | + http.HandleFunc("/api/load", handleLoad) |
| 47 | + http.HandleFunc("/api/list", handleList) |
| 48 | + |
| 49 | + // Determine the address to listen on |
| 50 | + addr := fmt.Sprintf(":%d", config.Server.Port) |
| 51 | + |
| 52 | + // Show URL on start if configured |
| 53 | + if config.Server.ShowURLOnStart { |
| 54 | + log.Printf("Server starting on http://localhost%s", addr) |
| 55 | + } else { |
| 56 | + log.Printf("Server starting on port %d", config.Server.Port) |
| 57 | + } |
| 58 | + |
| 59 | + log.Fatal(http.ListenAndServe(addr, nil)) |
| 60 | +} |
| 61 | + |
| 62 | +func loadConfig() { |
| 63 | + file, err := ioutil.ReadFile("config.json") |
| 64 | + if err != nil { |
| 65 | + log.Fatal("Error reading config file:", err) |
| 66 | + } |
| 67 | + |
| 68 | + err = json.Unmarshal(file, &config) |
| 69 | + if err != nil { |
| 70 | + log.Fatal("Error parsing config file:", err) |
| 71 | + } |
| 72 | + |
| 73 | + // Set default values if not specified |
| 74 | + if config.Server.Port == 0 { |
| 75 | + config.Server.Port = 8080 |
| 76 | + } |
| 77 | + if config.Server.GermanFolder == "" { |
| 78 | + config.Server.GermanFolder = "/content/de/blog" |
| 79 | + } |
| 80 | + if config.Server.EnglishFolder == "" { |
| 81 | + config.Server.EnglishFolder = "/content/en/blog" |
| 82 | + } |
| 83 | + |
| 84 | + // Convert relative paths to absolute paths |
| 85 | + absPath, err := filepath.Abs(config.Server.GermanFolder) |
| 86 | + if err == nil { |
| 87 | + config.Server.GermanFolder = absPath |
| 88 | + } |
| 89 | + absPath, err = filepath.Abs(config.Server.EnglishFolder) |
| 90 | + if err == nil { |
| 91 | + config.Server.EnglishFolder = absPath |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func handleConfig(w http.ResponseWriter, r *http.Request) { |
| 96 | + w.Header().Set("Content-Type", "application/json") |
| 97 | + json.NewEncoder(w).Encode(config) |
| 98 | +} |
| 99 | + |
| 100 | +func handleSave(w http.ResponseWriter, r *http.Request) { |
| 101 | + if r.Method != http.MethodPost { |
| 102 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + filename := r.URL.Query().Get("file") |
| 107 | + if filename == "" { |
| 108 | + http.Error(w, "Filename is required", http.StatusBadRequest) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + content, err := ioutil.ReadAll(r.Body) |
| 113 | + if err != nil { |
| 114 | + http.Error(w, "Error reading request body", http.StatusInternalServerError) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + fullPath := getFullPath(filename) |
| 119 | + err = ioutil.WriteFile(fullPath, content, 0644) |
| 120 | + if err != nil { |
| 121 | + http.Error(w, "Error saving file", http.StatusInternalServerError) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + w.WriteHeader(http.StatusOK) |
| 126 | +} |
| 127 | + |
| 128 | +func handleLoad(w http.ResponseWriter, r *http.Request) { |
| 129 | + filename := r.URL.Query().Get("file") |
| 130 | + if filename == "" { |
| 131 | + http.Error(w, "Filename is required", http.StatusBadRequest) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + fullPath := getFullPath(filename) |
| 136 | + content, err := ioutil.ReadFile(fullPath) |
| 137 | + if err != nil { |
| 138 | + if os.IsNotExist(err) { |
| 139 | + http.Error(w, "File not found", http.StatusNotFound) |
| 140 | + } else { |
| 141 | + http.Error(w, "Error reading file", http.StatusInternalServerError) |
| 142 | + } |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + w.Header().Set("Content-Type", "text/plain") |
| 147 | + w.Write(content) |
| 148 | +} |
| 149 | + |
| 150 | +func handleList(w http.ResponseWriter, r *http.Request) { |
| 151 | + files := make(map[string][]string) |
| 152 | + |
| 153 | + germanFiles, err := listFiles(config.Server.GermanFolder) |
| 154 | + if err != nil { |
| 155 | + http.Error(w, "Error reading German directory", http.StatusInternalServerError) |
| 156 | + return |
| 157 | + } |
| 158 | + files["de"] = germanFiles |
| 159 | + |
| 160 | + englishFiles, err := listFiles(config.Server.EnglishFolder) |
| 161 | + if err != nil { |
| 162 | + http.Error(w, "Error reading English directory", http.StatusInternalServerError) |
| 163 | + return |
| 164 | + } |
| 165 | + files["en"] = englishFiles |
| 166 | + |
| 167 | + w.Header().Set("Content-Type", "application/json") |
| 168 | + json.NewEncoder(w).Encode(files) |
| 169 | +} |
| 170 | + |
| 171 | +func listFiles(dir string) ([]string, error) { |
| 172 | + var files []string |
| 173 | + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + if !info.IsDir() && strings.HasSuffix(info.Name(), ".md") { |
| 178 | + relPath, err := filepath.Rel(dir, path) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + files = append(files, relPath) |
| 183 | + } |
| 184 | + return nil |
| 185 | + }) |
| 186 | + return files, err |
| 187 | +} |
| 188 | + |
| 189 | +func getFullPath(filename string) string { |
| 190 | + parts := strings.SplitN(filename, "/", 2) |
| 191 | + if len(parts) != 2 { |
| 192 | + return filename |
| 193 | + } |
| 194 | + |
| 195 | + lang, file := parts[0], parts[1] |
| 196 | + switch lang { |
| 197 | + case "de": |
| 198 | + return filepath.Join(config.Server.GermanFolder, file) |
| 199 | + case "en": |
| 200 | + return filepath.Join(config.Server.EnglishFolder, file) |
| 201 | + default: |
| 202 | + return filename |
| 203 | + } |
| 204 | +} |
0 commit comments