Jelajahi Sumber

Domain for current version, not future.

- Moved domain extensions to domain/future.
- Added stub memory- and MySQL-based repositories.
Frederic G. MARAND 5 tahun lalu
induk
melakukan
51722a941b

+ 9 - 9
cmd/kurz/kurz.go

@@ -4,26 +4,26 @@ package main
 /*
 kurz
 	-v							Verbose (default: false).
-	--url                       Access kurzd on this URL: (default: https://localhost:443, then http://localhost:80)
+	--url                       Access kurzd on this RedirectURL: (default: https://localhost:443, then http://localhost:80)
 	-h, --help					Display help.
-	check <url>                 Show information about a Kurz URL (default command). Exit 0 if the URL is available
+	check <url>                 Show information about a Kurz RedirectURL (default command). Exit 0 if the RedirectURL is available
 		--format json|null|text|url|yaml    Use a given format for the info (default: text). For null, only return an exit code.
 		--usage                 Also provide usage information (privileged)
 	-u <user>, --user=<user>    Provide a user account. Default: $KURZ_USER
 	-p <pass>, --pass=<pass>    Provide a user password. Default: $KURZ_PASS. If used without a pass, ask for one
-	create                      Create a new Kurz URL
+	create                      Create a new Kurz RedirectURL
 		--dry-run               Simulate creation: do not store result.
 		<url>                   Default creation
 		<url> <domain>          Create on vanity domain (restricted)
-		<url> <vanity_url>      Create as vanity URL (restricted)
-	archive                     Archive a Kurz URL (restricted)
+		<url> <vanity_url>      Create as vanity RedirectURL (restricted)
+	archive                     Archive a Kurz RedirectURL (restricted)
 		--dry-run               Simulate archival
-		<url>                   The URL to archive
+		<url>                   The RedirectURL to archive
 	purge
 
-A Kurz URL created for a Kurz URL
-	- for the same user: the same URL.
-	- for a different user: a different URL.
+A Kurz RedirectURL created for a Kurz RedirectURL
+	- for the same user: the same RedirectURL.
+	- for a different user: a different RedirectURL.
 */
 func main() {
 

+ 0 - 1
cmd/kurzd/kurzd.go

@@ -21,7 +21,6 @@ kurzd
 		-m|--monitoring <port_id>         Serve monitoring on this IP port (default: none)
 	install							Install kurzd completely.
 		config							Install the configuration define by command line to ~.
-		schema							Install the kurz database schema.
 	export							Export kurzd data.
 		config						    Export kurz configuration.
 		content							Export kurz content.

+ 16 - 0
domain/doc.go

@@ -0,0 +1,16 @@
+// Copyright 2018 Ouest Systèmes Informatiques (OSInet). All rights reserved.
+
+/*
+Package domain contains the internal data handled by Kurz, in the sense of the
+"hexagonal architecture" model.
+
+Its API is made of:
+
+  - functions GetTargetURL() and GetShortURL()
+
+Its SPI is made of:
+
+  - interfaces ShortURLRepository and TargetURLRepository
+  - configuration function RegisterRepositories()
+ */
+package domain

+ 27 - 0
domain/domain.go

@@ -0,0 +1,27 @@
+package domain
+
+var shortURLRepository ShortURLRepository
+var targetURLRepository TargetURLRepository
+
+func GetTargetURL(shortURL string) (string, error) {
+	return "", nil
+}
+
+func GetShortURL(targetURL string) (string, error) {
+	return "", nil
+}
+
+type ShortURLRepository interface {
+	GetTarget(su ShortURL) (TargetURL, error)
+	GetByTarget(tu TargetURL) (ShortURL, error)
+}
+
+type TargetURLRepository interface {
+	GetShort(tu TargetURL) (ShortURL, error)
+	GetByShort(su ShortURL) (TargetURL, error)
+}
+
+func RegisterRepositories(sur ShortURLRepository, tur TargetURLRepository) {
+	shortURLRepository = sur
+	targetURLRepository = tur
+}

+ 45 - 0
domain/future/domain.go

@@ -0,0 +1,45 @@
+package future
+
+import (
+	"code.osinet.fr/fgm/kurz/domain"
+	"net/url"
+)
+
+/*
+Domain represents a DNS domain.
+ */
+type Domain interface {
+	String() string
+}
+
+/**
+HTTPStatus is a subset of int limited to the values enumerated in net/http,
+like StatusOK (200) or StatusNotFound (404).
+ */
+type HTTPStatus interface {
+	Int() int
+}
+
+/**
+HTTPRedirectStatus is a subset of HTTPStatus limited to the values from 300 to 399.
+ */
+type HTTPRedirectStatus HTTPStatus
+
+type ShortURLAssigner interface {
+	GenerateShortURLFromTarget(TargetURL, DomainController) (domain.ShortURL, error)
+	RegisterShortURLForTarget(domain.ShortURL, TargetURL, DomainController) error
+}
+
+type ShortURLResolver interface {
+	TargetURLFromShortURL(domain.ShortURL) (TargetURL, error)
+}
+
+type TargetURL interface {
+	AllowedRedirectStatuses() []HTTPRedirectStatus
+	DefaultRedirectStatus() HTTPRedirectStatus
+	RedirectURL() url.URL
+}
+
+type DomainController interface {
+	ControlledDomains() []Domain
+}

+ 10 - 6
domain/short_url.go

@@ -8,19 +8,23 @@ import (
 
 type ShortURL struct {
 	URL
-	TargetURL
+	target TargetURL
+}
+
+func (su ShortURL) Target() TargetURL {
+	return su.target
 }
 
 /*
-MakeShortURL creates a ShortURL instance from a given URL/target pair.
+MakeShortURL creates a ShortURL instance from a given RedirectURL/target pair.
 */
-func NewUnspecifiedShortURL(targetURL TargetURL) (*ShortURL, error) {
-	if targetURL.IsEmpty() {
-		return nil, errors.New("cannot target empty URL")
+func NewUnspecifiedShortURL(target TargetURL) (*ShortURL, error) {
+	if target.IsEmpty() {
+		return nil, errors.New("cannot target empty RedirectURL")
 	}
 
 	// FIXME: will cause collisions.
 	url := URL(strconv.Itoa(rand.Intn(1 << 31)))
-	u := ShortURL{url, targetURL}
+	u := ShortURL{url, target}
 	return &u, nil
 }

+ 1 - 1
domain/short_url_test.go

@@ -13,7 +13,7 @@ func TestNewShortURLHappy(t *testing.T) {
 		t.FailNow()
 	}
 
-	if su.URL.IsEmpty() || su.TargetURL.IsEmpty() || !su.MayRedirect(){
+	if su.URL.IsEmpty() || su.target.IsEmpty() || !su.target.MayRedirect(){
 		t.FailNow()
 	}
 }

+ 3 - 3
domain/target_url.go

@@ -9,11 +9,11 @@ func (tu *TargetURL) IsEmpty() bool {
 }
 
 /*
-MayRedirect defines whether the TargetURL may be used in a redirection.
+MayRedirect defines whether the Target may be used in a redirection.
 
-In the current version, any non empty URL may be used. In future versions, some
+In the current version, any non empty RedirectURL may be used. In future versions, some
 restrictions like blocked URLs or even whole domains may limit the availability
-of a target URL.
+of a target RedirectURL.
 */
 func (tu *TargetURL) MayRedirect() bool {
 	return !tu.IsEmpty()

+ 44 - 0
infrastructure/memory.go

@@ -0,0 +1,44 @@
+package infrastructure
+
+import (
+	"errors"
+
+	"code.osinet.fr/fgm/kurz/domain"
+)
+
+type urlmap map[domain.URL]domain.URL
+
+var targets = make(urlmap)
+var shorts = make(urlmap)
+
+type MemoryShortURLRepository struct {
+}
+
+func (r MemoryShortURLRepository) GetTarget(su domain.ShortURL) (domain.TargetURL, error) {
+	var tu domain.TargetURL
+	var err error
+
+	if t, ok := targets[su.URL]; ok {
+		tu = domain.TargetURL{URL: t}
+	} else {
+		err = errors.New("not found")
+	}
+	return tu, err
+}
+
+func (r MemoryShortURLRepository) GetByTarget(tu domain.TargetURL) (domain.ShortURL, error) {
+	var su domain.ShortURL
+	var err error
+
+	if s, ok := shorts[tu.URL]; ok {
+		su = domain.ShortURL{URL: s}
+	} else {
+		err = errors.New("not found")
+	}
+	return su, err
+}
+
+func (r MemoryShortURLRepository) add(s domain.ShortURL, t domain.TargetURL) {
+	targets[s.URL] = t.URL
+	shorts[t.URL] = s.URL
+}

+ 21 - 0
infrastructure/mysql.go

@@ -0,0 +1,21 @@
+package infrastructure
+
+import (
+	"database/sql"
+
+	"code.osinet.fr/fgm/kurz/domain"
+)
+
+type MySQLShortURLRepository struct {
+	db *sql.DB
+}
+
+func (r MySQLShortURLRepository) GetTarget(su domain.ShortURL) (domain.TargetURL, error) {
+	var tu domain.TargetURL
+	return tu, nil
+}
+
+func (r MySQLShortURLRepository) GetByTarget(tu domain.TargetURL) (domain.ShortURL, error) {
+	var su domain.ShortURL
+	return su, nil
+}