api.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "github.com/ollama/ollama/x/build"
  8. "github.com/ollama/ollama/x/client/ollama/apitype"
  9. "github.com/ollama/ollama/x/oweb"
  10. "github.com/ollama/ollama/x/registry"
  11. regtype "github.com/ollama/ollama/x/registry"
  12. )
  13. // Common API Errors
  14. var (
  15. errUnqualifiedRef = oweb.Invalid("invalid", "name", "must be fully qualified")
  16. errRefNotFound = oweb.Invalid("not_found", "name", "no such model")
  17. )
  18. type Server struct {
  19. Build *build.Server
  20. }
  21. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  22. oweb.Serve(s.serveHTTP, w, r)
  23. }
  24. func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) error {
  25. switch r.URL.Path {
  26. case "/v1/push":
  27. return s.handlePush(w, r)
  28. default:
  29. return oweb.ErrNotFound
  30. }
  31. }
  32. func want(r *http.Request, method, path string) bool {
  33. return r.Method == method && r.URL.Path == path
  34. }
  35. func (s *Server) handlePush(_ http.ResponseWriter, r *http.Request) error {
  36. if r.Method != "POST" {
  37. return oweb.ErrMethodNotAllowed
  38. }
  39. params, err := oweb.DecodeJSON[apitype.PushRequest](r.Body)
  40. if err != nil {
  41. return err
  42. }
  43. if params.Name == "" {
  44. return oweb.Missing("name")
  45. }
  46. const registryURLTODO = "http://localhost:8888"
  47. man, err := s.Build.ManifestData(params.Name)
  48. if err != nil {
  49. if errors.Is(err, build.ErrNotFound) {
  50. return errRefNotFound
  51. }
  52. return err
  53. }
  54. c := registry.Client{BaseURL: registryURLTODO}
  55. requirements, err := c.Push(r.Context(), params.Name, man, nil)
  56. if err != nil {
  57. return err
  58. }
  59. var uploads []regtype.CompletePart
  60. for _, rq := range requirements {
  61. l, err := s.Build.LayerFile(rq.Digest)
  62. if err != nil {
  63. return err
  64. }
  65. err = func() error {
  66. f, err := os.Open(l)
  67. if err != nil {
  68. return err
  69. }
  70. defer f.Close()
  71. etag, err := registry.PushLayer(r.Context(), rq.URL, rq.Offset, rq.Size, f)
  72. if err != nil {
  73. return err
  74. }
  75. uploads = append(uploads, regtype.CompletePart{
  76. URL: rq.URL,
  77. ETag: etag,
  78. })
  79. return nil
  80. }()
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. // commit the manifest to the registry
  86. requirements, err = c.Push(r.Context(), params.Name, man, &registry.PushParams{
  87. CompleteParts: uploads,
  88. })
  89. if err != nil {
  90. return err
  91. }
  92. for _, r := range requirements {
  93. err = errors.Join(err, fmt.Errorf("push failed for %q", r.Digest))
  94. }
  95. return err
  96. }
  97. func (s *Server) handlePull(w http.ResponseWriter, r *http.Request) error {
  98. return oweb.ErrNotFound
  99. }