drone/gitrpc/server/server.go
Johannes Batzill c55c53deab [API] Add Create + Delete Branch API (+ Basic GIT Error Propagation) (#70)
This change adds the following:
- create / delete branch
- basic error propagation from git to user (notfound, conflict, invalidinput)
- create repo root folder in server instead of service constructor
2022-11-10 20:57:31 -08:00

87 lines
1.9 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package server
import (
"errors"
"os"
"path/filepath"
"github.com/harness/gitness/gitrpc/internal/gitea"
"github.com/harness/gitness/gitrpc/internal/service"
"github.com/harness/gitness/gitrpc/internal/storage"
"github.com/harness/gitness/gitrpc/rpc"
"net"
"code.gitea.io/gitea/modules/setting"
"google.golang.org/grpc"
)
const (
repoSubdirName = "repos"
)
type Server struct {
*grpc.Server
Bind string
}
// TODO: this wiring should be done by wire.
func NewServer(bind string, gitRoot string) (*Server, error) {
// Create repos folder
reposRoot := filepath.Join(gitRoot, repoSubdirName)
if _, err := os.Stat(reposRoot); errors.Is(err, os.ErrNotExist) {
if err = os.MkdirAll(reposRoot, 0o700); err != nil {
return nil, err
}
}
// TODO: should be subdir of gitRoot? What is it being used for?
setting.Git.HomePath = "home"
adapter, err := gitea.New()
if err != nil {
return nil, err
}
s := grpc.NewServer()
store := storage.NewLocalStore()
// initialize services
repoService, err := service.NewRepositoryService(adapter, store, reposRoot)
if err != nil {
return nil, err
}
// initialize services
refService, err := service.NewReferenceService(adapter, reposRoot)
if err != nil {
return nil, err
}
httpService, err := service.NewHTTPService(adapter, reposRoot)
if err != nil {
return nil, err
}
// register services
rpc.RegisterRepositoryServiceServer(s, repoService)
rpc.RegisterReferenceServiceServer(s, refService)
rpc.RegisterSmartHTTPServiceServer(s, httpService)
return &Server{
Server: s,
Bind: bind,
}, nil
}
func (s *Server) Start() error {
lis, err := net.Listen("tcp", s.Bind)
if err != nil {
return err
}
return s.Server.Serve(lis)
}
func (s *Server) Stop() error {
s.Server.GracefulStop()
return nil
}