Преглед изворни кода

Decouple gin-dex and create libgin

libgin subpackage will eventually be split out entirely and will hold
all code to be shared between GIN related projects.
Achilleas Koutsou пре 6 година
родитељ
комит
78651b9179

+ 4 - 1
docker/build.sh

@@ -6,10 +6,13 @@ set -e
 export GOPATH=/tmp/go
 export PATH=/usr/local/go/bin:${PATH}:${GOPATH}/bin
 
+# TODO: Vendor these (after cleanup)
 go get golang.org/x/crypto/bcrypt
 go get github.com/jteeuwen/go-bindata
-go get github.com/G-Node/gin-dex/gindex
+go get github.com/G-Node/gig
 go get github.com/G-Node/git-module
+go get github.com/G-Node/go-annex
+go get github.com/Sirupsen/logrus
 go get gopkg.in/yaml.v2
 
 cd ${GOPATH}/src/github.com/jteeuwen/go-bindata/go-bindata

+ 62 - 0
pkg/libgin/dex.go

@@ -0,0 +1,62 @@
+package libgin
+
+import (
+	"time"
+
+	"github.com/G-Node/gig"
+)
+
+// NOTE: TEMPORARY COPY FROM gin-dex
+
+type SearchRequest struct {
+	Token  string
+	CsrfT  string
+	UserID int64
+	Querry string
+	SType  int64
+}
+
+const (
+	SEARCH_MATCH = iota
+	SEARCH_FUZZY
+	SEARCH_WILDCARD
+	SEARCH_QUERRY
+	SEARCH_SUGGEST
+)
+
+type BlobSResult struct {
+	Source    *IndexBlob  `json:"_source"`
+	Score     float64     `json:"_score"`
+	Highlight interface{} `json:"highlight"`
+}
+
+type CommitSResult struct {
+	Source    *IndexCommit `json:"_source"`
+	Score     float64      `json:"_score"`
+	Highlight interface{}  `json:"highlight"`
+}
+
+type SearchResults struct {
+	Blobs   []BlobSResult
+	Commits []CommitSResult
+}
+
+type IndexBlob struct {
+	*gig.Blob
+	GinRepoName  string
+	GinRepoId    string
+	FirstCommit  string
+	Id           int64
+	Oid          gig.SHA1
+	IndexingTime time.Time
+	Content      string
+	Path         string
+}
+
+type IndexCommit struct {
+	*gig.Commit
+	GinRepoId    string
+	Oid          gig.SHA1
+	GinRepoName  string
+	IndexingTime time.Time
+}

+ 78 - 0
pkg/libgin/doi.go

@@ -0,0 +1,78 @@
+package libgin
+
+import (
+	"crypto/md5"
+	"encoding/hex"
+	"fmt"
+	"net/http"
+
+	log "gopkg.in/clog.v1"
+)
+
+// NOTE: TEMPORARY COPIES FROM gin-doi
+
+// uuidMap is a map between registered repositories and their UUIDs for datasets registered before the new UUID generation method was implemented.
+// This map is required because the current method of computing UUIDs differs from the older method and this lookup is used to handle the old-method UUIDs.
+var uuidMap = map[string]string{
+	"INT/multielectrode_grasp":                   "f83565d148510fede8a277f660e1a419",
+	"ajkumaraswamy/HB-PAC_disinhibitory_network": "1090f803258557299d287c4d44a541b2",
+	"steffi/Kleineidam_et_al_2017":               "f53069de4c4921a3cfa8f17d55ef98bb",
+	"Churan/Morris_et_al_Frontiers_2016":         "97bc1456d3f4bca2d945357b3ec92029",
+	"fabee/efish_locking":                        "6953bbf0087ba444b2d549b759de4a06",
+}
+
+// RepoPathToUUID computes a UUID from a repository path.
+func RepoPathToUUID(URI string) string {
+	if doi, ok := uuidMap[URI]; ok {
+		return doi
+	}
+	currMd5 := md5.Sum([]byte(URI))
+	return hex.EncodeToString(currMd5[:])
+}
+
+// DOIRegInfo holds all the metadata and information necessary for a DOI registration request.
+type DOIRegInfo struct {
+	Missing     []string
+	DOI         string
+	UUID        string
+	FileSize    int64
+	Title       string
+	Authors     []Author
+	Description string
+	Keywords    []string
+	References  []Reference
+	Funding     []string
+	License     *License
+	DType       string
+}
+
+type Author struct {
+	FirstName   string
+	LastName    string
+	Affiliation string
+	ID          string
+}
+
+type Reference struct {
+	Reftype string
+	Name    string
+	DOI     string
+}
+
+type License struct {
+	Name string
+	URL  string
+}
+
+func IsRegisteredDOI(doi string) bool {
+	url := fmt.Sprintf("https://doi.org/%s", doi)
+	resp, err := http.Get(url)
+	if err != nil {
+		log.Trace("Could not query for doi: %s at %s", doi, url)
+		return false
+	}
+	if resp.StatusCode != http.StatusNotFound {
+		return true
+	}
+	return false
+}

+ 5 - 0
pkg/libgin/libgin.go

@@ -0,0 +1,5 @@
+/*
+Package libgin provides functionality for features added for the G-Node GIN service.
+This is a temporary package. Functions and types have been copied from gin-doi and gin-dex services. The plan is for G-Node/GOGS (GIN) to not depend on any code from the added services. Instead, common functionality will be imported from a separate lib package. This subpackage is the first version of that.
+*/
+package libgin

+ 8 - 7
routes/api/v1/search/general.go

@@ -1,17 +1,18 @@
 package search
 
 import (
-	"github.com/G-Node/gogs/pkg/context"
-	"github.com/G-Node/gin-dex/gindex"
-	"net/http"
-	"github.com/G-Node/gogs/pkg/setting"
-	"encoding/json"
 	"bytes"
+	"encoding/json"
 	"io/ioutil"
+	"net/http"
+
+	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/libgin"
+	"github.com/G-Node/gogs/pkg/setting"
 )
 
 func Search(c *context.APIContext) {
-	if ! c.IsLogged {
+	if !c.IsLogged {
 		c.Status(http.StatusUnauthorized)
 		return
 	}
@@ -19,7 +20,7 @@ func Search(c *context.APIContext) {
 		c.Status(http.StatusNotImplemented)
 		return
 	}
-	ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName), UserID: c.User.ID,
+	ireq := libgin.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName), UserID: c.User.ID,
 		Querry: c.Params("querry"), CsrfT: c.GetCookie(setting.CSRFCookieName)}
 	data, err := json.Marshal(ireq)
 	if err != nil {

+ 6 - 5
routes/api/v1/search/suggest.go

@@ -1,12 +1,13 @@
 package search
 
 import (
-	"net/http"
-	"github.com/G-Node/gin-dex/gindex"
-	"encoding/json"
 	"bytes"
+	"encoding/json"
 	"io/ioutil"
+	"net/http"
+
 	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/libgin"
 	"github.com/G-Node/gogs/pkg/setting"
 )
 
@@ -15,8 +16,8 @@ func Suggest(c *context.APIContext) {
 		c.Status(http.StatusNotImplemented)
 		return
 	}
-	ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName),
-		Querry: c.Params("querry"), CsrfT: c.GetCookie(setting.CSRFCookieName), SType:gindex.SEARCH_SUGGEST}
+	ireq := libgin.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName),
+		Querry: c.Params("querry"), CsrfT: c.GetCookie(setting.CSRFCookieName), SType: libgin.SEARCH_SUGGEST}
 	if c.IsLogged {
 		ireq.UserID = c.User.ID
 	}

+ 4 - 74
routes/repo/view.go

@@ -7,15 +7,12 @@ package repo
 import (
 	"bufio"
 	"bytes"
-	"crypto/md5"
-	"encoding/hex"
 	"encoding/json"
 	"encoding/xml"
 	"fmt"
 	gotemplate "html/template"
 	"io"
 	"io/ioutil"
-	"net/http"
 	"os"
 	"path"
 	"strings"
@@ -25,6 +22,7 @@ import (
 	"github.com/G-Node/godML/odml"
 	"github.com/G-Node/gogs/models"
 	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/libgin"
 	"github.com/G-Node/gogs/pkg/markup"
 	"github.com/G-Node/gogs/pkg/setting"
 	"github.com/G-Node/gogs/pkg/template"
@@ -81,7 +79,7 @@ func renderDirectory(c *context.Context, treeLink string) {
 				log.Trace("Doi Blob could not be read:%v", err)
 			}
 			buf, err := ioutil.ReadAll(doiData)
-			doiInfo := DOIRegInfo{}
+			doiInfo := libgin.DOIRegInfo{}
 			err = yaml.Unmarshal(buf, &doiInfo)
 			if err != nil {
 				log.Trace("Doi Blob could not be unmarshalled:%v", err)
@@ -91,7 +89,7 @@ func renderDirectory(c *context.Context, treeLink string) {
 			doi := GDoiRepo(c, setting.Doi.DoiBase)
 			//ddata, err := ginDoi.GDoiMData(doi, "https://api.datacite.org/works/") //todo configure URL?
 
-			c.Data["DoiReg"] = IsRegisteredDOI(doi)
+			c.Data["DoiReg"] = libgin.IsRegisteredDOI(doi)
 			c.Data["doi"] = doi
 
 		}
@@ -516,74 +514,6 @@ func GDoiRepo(c *context.Context, doiBase string) string {
 	if c.Repo.Repository.IsFork && c.Repo.Owner.Name == "doi" {
 		repoN = c.Repo.Repository.BaseRepo.FullName()
 	}
-	uuid := repoPathToUUID(repoN)
+	uuid := libgin.RepoPathToUUID(repoN)
 	return doiBase + uuid[:6]
 }
-
-// NOTE: TEMPORARY COPIES FROM gin-doi
-
-// uuidMap is a map between registered repositories and their UUIDs for datasets registered before the new UUID generation method was implemented.
-// This map is required because the current method of computing UUIDs differs from the older method and this lookup is used to handle the old-method UUIDs.
-var uuidMap = map[string]string{
-	"INT/multielectrode_grasp":                   "f83565d148510fede8a277f660e1a419",
-	"ajkumaraswamy/HB-PAC_disinhibitory_network": "1090f803258557299d287c4d44a541b2",
-	"steffi/Kleineidam_et_al_2017":               "f53069de4c4921a3cfa8f17d55ef98bb",
-	"Churan/Morris_et_al_Frontiers_2016":         "97bc1456d3f4bca2d945357b3ec92029",
-	"fabee/efish_locking":                        "6953bbf0087ba444b2d549b759de4a06",
-}
-
-// repoPathToUUID computes a UUID from a repository path.
-func repoPathToUUID(URI string) string {
-	if doi, ok := uuidMap[URI]; ok {
-		return doi
-	}
-	currMd5 := md5.Sum([]byte(URI))
-	return hex.EncodeToString(currMd5[:])
-}
-
-// DOIRegInfo holds all the metadata and information necessary for a DOI registration request.
-type DOIRegInfo struct {
-	Missing     []string
-	DOI         string
-	UUID        string
-	FileSize    int64
-	Title       string
-	Authors     []Author
-	Description string
-	Keywords    []string
-	References  []Reference
-	Funding     []string
-	License     *License
-	DType       string
-}
-
-type Author struct {
-	FirstName   string
-	LastName    string
-	Affiliation string
-	ID          string
-}
-
-type Reference struct {
-	Reftype string
-	Name    string
-	DOI     string
-}
-
-type License struct {
-	Name string
-	URL  string
-}
-
-func IsRegisteredDOI(doi string) bool {
-	url := fmt.Sprintf("https://doi.org/%s", doi)
-	resp, err := http.Get(url)
-	if err != nil {
-		log.Trace("Could not query for doi: %s at %s", doi, url)
-		return false
-	}
-	if resp.StatusCode != http.StatusNotFound {
-		return true
-	}
-	return false
-}

+ 4 - 4
routes/search.go

@@ -8,8 +8,8 @@ import (
 	"net/http"
 	"strconv"
 
-	"github.com/G-Node/gin-dex/gindex"
 	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/libgin"
 	"github.com/G-Node/gogs/pkg/setting"
 	"github.com/Sirupsen/logrus"
 )
@@ -25,7 +25,7 @@ func Search(c *context.Context, keywords string, sType int64) ([]byte, error) {
 		return nil, fmt.Errorf("Extended search not implemented")
 	}
 
-	ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName),
+	ireq := libgin.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName),
 		Querry: keywords, CsrfT: c.GetCookie(setting.CSRFCookieName), SType: sType, UserID: -10}
 	if c.IsLogged {
 		ireq.UserID = c.User.ID
@@ -69,7 +69,7 @@ func ExploreData(c *context.Context) {
 		return
 	}
 
-	res := gindex.SearchResults{}
+	res := libgin.SearchResults{}
 	err = json.Unmarshal(data, &res)
 	if err != nil {
 		c.Handle(http.StatusInternalServerError, "Could not display result", err)
@@ -98,7 +98,7 @@ func ExploreCommits(c *context.Context) {
 		return
 	}
 
-	res := gindex.SearchResults{}
+	res := libgin.SearchResults{}
 	err = json.Unmarshal(data, &res)
 	if err != nil {
 		c.Handle(http.StatusInternalServerError, "Could not display result", err)