Skip to content

Commit

Permalink
setup reverse proxy for /api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ili16 committed Dec 27, 2023
1 parent 78182d6 commit 2c1d294
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import (
"github.com/gin-gonic/gin"
"io"
"net/http"
"net/http/httputil"
"net/url"
)

var OllamaURL = "http://ollama-service.ba-kovacevic:11434"

func main() {
router := gin.Default()
router.GET("/", getInfo)

router.Any("/api/*proxyPath", proxy)

router.Run(":8080")
}

func getInfo(c *gin.Context) {

resp, err := http.Get("http://ollama-service.ba-kovacevic:11434")
resp, err := http.Get(OllamaURL)

if err != nil {
//
Expand All @@ -30,3 +36,21 @@ func getInfo(c *gin.Context) {
defer resp.Body.Close()

}

func proxy(c *gin.Context) {
remote, err := url.Parse(OllamaURL)
if err != nil {
panic(err)
}

proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = c.Param("proxyPath")
}

proxy.ServeHTTP(c.Writer, c.Request)
}

0 comments on commit 2c1d294

Please sign in to comment.