code view
This commit is contained in:
parent
f6b6c8a7e6
commit
3a601b2bdd
2 changed files with 86 additions and 0 deletions
82
internal/codeview/codeview.go
Normal file
82
internal/codeview/codeview.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package codeview
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/kulak/gemini"
|
||||
)
|
||||
|
||||
/*
|
||||
* Codeview is a feature that proxies my personal git server (forgejo)
|
||||
* Allowing me to render code natively in gemini
|
||||
*
|
||||
* Features:
|
||||
* - [ ] Individual file view with line numbers?
|
||||
* - [ ] Dir view with links to files
|
||||
* - [ ] Project root view with markdown to gemtxt conversion and file tree
|
||||
*/
|
||||
|
||||
// https://git.travisshears.com/travisshears/personal-gemini-capsule/raw/branch/main/internal/guestbook/guestbook.go
|
||||
// /codeview/raw/personal-gemini-capsule/src/branch/main/internal/guestbook/guestbook.go
|
||||
func HandleRequest(w gemini.ResponseWriter, req *gemini.Request) {
|
||||
path := req.URL.Path
|
||||
|
||||
switch {
|
||||
// case path == "/gemlog" || path == "/gemlog/":
|
||||
// g.serveIndex(w, req)
|
||||
case strings.HasPrefix(path, "/codeview/raw/"):
|
||||
serveFile(w, req, path)
|
||||
default:
|
||||
w.WriteStatusMsg(gemini.StatusNotFound, "Page not found")
|
||||
}
|
||||
}
|
||||
|
||||
func serveFile(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
||||
path = strings.TrimPrefix(path, "/codeview/raw/")
|
||||
rawURL := strings.Replace(path, "src", "raw", 1)
|
||||
rawURL = "https://git.travisshears.com/travisshears/" + rawURL
|
||||
remoteURL := "https://git.travisshears.com/travisshears/" + path
|
||||
httpReq, err := http.NewRequest("GET", rawURL, nil)
|
||||
if err != nil {
|
||||
slog.Error("Failed to create request to git server", "error", err)
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
res, err := http.DefaultClient.Do(httpReq)
|
||||
if err != nil {
|
||||
slog.Error("Failed to send request to git server", "error", err)
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
slog.Error("Failed to read res body", "error", err)
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
slog.Error("unexpected status code", "statusCode", res.StatusCode, "body", string(body))
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
|
||||
var content strings.Builder
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
// filename := strings.TrimPrefix(remoteURL, "https://git.travisshears.com/travisshears/")
|
||||
// content.WriteString(fmt.Sprintf("# File: %s\n", filename))
|
||||
content.WriteString("# File CodeView\n")
|
||||
content.WriteString(fmt.Sprintf("This is a code view which proxies my personal git server. On the clearnet the following file is available at:\n=> %s %s\n\n", remoteURL, path))
|
||||
content.WriteString("----------------------------\n\n")
|
||||
content.WriteString("```code\n")
|
||||
content.Write(body)
|
||||
content.WriteString("```\n")
|
||||
content.WriteString("\n\n-----------------------------\n\n=> / Back to home\n")
|
||||
w.WriteBody([]byte(content.String()))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue