70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"github.com/smjklake/glancr/internal/tokenizer"
|
|
)
|
|
|
|
type API struct {
|
|
tokenizer *tokenizer.Tokenizer
|
|
}
|
|
|
|
func NewAPI() *API {
|
|
return &API{
|
|
tokenizer: tokenizer.NewTokenizer(),
|
|
}
|
|
}
|
|
|
|
// GET /api/file?path=example.go&start=1&end=100
|
|
func (a *API) HandleFile(w http.ResponseWriter, r *http.Request) {
|
|
filePath := r.URL.Query().Get("path")
|
|
startLine := r.URL.Query().Get("start")
|
|
endLine := r.URL.Query().Get("end")
|
|
|
|
// Read file
|
|
content, err := ioutil.ReadFile(filePath)
|
|
if err != nil {
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
// Detect language from extension
|
|
language := detectLanguage(filepath.Ext(filePath))
|
|
|
|
// Tokenize
|
|
doc := a.tokenizer.TokenizeDocument(string(content), language)
|
|
|
|
// Handle range requests for virtual scrolling
|
|
if startLine != "" && endLine != "" {
|
|
start, _ := strconv.Atoi(startLine)
|
|
end, _ := strconv.Atoi(endLine)
|
|
|
|
if start > 0 && end <= len(doc.Lines) {
|
|
doc.Lines = doc.Lines[start-1 : end]
|
|
}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(doc)
|
|
}
|
|
|
|
func detectLanguage(ext string) string {
|
|
switch ext {
|
|
case ".go":
|
|
return "go"
|
|
case ".js", ".jsx":
|
|
return "javascript"
|
|
case ".ts", ".tsx":
|
|
return "javascript" // Can add typescript later
|
|
case ".md":
|
|
return "markdown"
|
|
default:
|
|
return "text"
|
|
}
|
|
}
|