-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add server package with version and models endpoints
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/presbrey/ollamafarm" | ||
"github.com/presbrey/ollamafarm/server" | ||
) | ||
|
||
func main() { | ||
farm := ollamafarm.New() | ||
|
||
// Register your Ollama clients here | ||
// For example: | ||
// farm.RegisterURL("http://localhost:11434", nil) | ||
|
||
s := server.NewServer(farm) | ||
|
||
http.HandleFunc("/version", s.VersionHandler) | ||
http.HandleFunc("/models", s.ModelsHandler) | ||
|
||
log.Println("Server starting on :8080") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/presbrey/ollamafarm" | ||
) | ||
|
||
type Server struct { | ||
Farm *ollamafarm.Farm | ||
} | ||
|
||
func NewServer(farm *ollamafarm.Farm) *Server { | ||
return &Server{Farm: farm} | ||
} | ||
|
||
func (s *Server) VersionHandler(w http.ResponseWriter, r *http.Request) { | ||
ollama := s.Farm.First(nil) | ||
if ollama == nil { | ||
http.Error(w, "No available Ollama instances", http.StatusServiceUnavailable) | ||
return | ||
} | ||
|
||
ctx := r.Context() | ||
version, err := ollama.Client().Version(ctx) | ||
if err != nil { | ||
http.Error(w, "Failed to get version", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(map[string]string{"version": version}) | ||
} | ||
|
||
func (s *Server) ModelsHandler(w http.ResponseWriter, r *http.Request) { | ||
models := s.Farm.AllModels() | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(models) | ||
} |