Frederic G. MARAND 6 gadi atpakaļ
vecāks
revīzija
b1bc866421

+ 18 - 0
.idea/codeStyles/Project.xml

@@ -0,0 +1,18 @@
+<component name="ProjectCodeStyleConfiguration">
+  <code_scheme name="Project" version="173">
+    <option name="SOFT_MARGINS" value="80" />
+    <HTMLCodeStyleSettings>
+      <option name="HTML_KEEP_BLANK_LINES" value="1" />
+      <option name="HTML_DO_NOT_INDENT_CHILDREN_OF" value="html" />
+      <option name="HTML_ENFORCE_QUOTES" value="true" />
+    </HTMLCodeStyleSettings>
+    <codeStyleSettings language="HTML">
+      <option name="WRAP_ON_TYPING" value="1" />
+      <indentOptions>
+        <option name="INDENT_SIZE" value="2" />
+        <option name="CONTINUATION_INDENT_SIZE" value="2" />
+        <option name="TAB_SIZE" value="2" />
+      </indentOptions>
+    </codeStyleSettings>
+  </code_scheme>
+</component>

+ 5 - 0
.idea/codeStyles/codeStyleConfig.xml

@@ -0,0 +1,5 @@
+<component name="ProjectCodeStyleConfiguration">
+  <state>
+    <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
+  </state>
+</component>

+ 2 - 14
.idea/dataSources.local.xml

@@ -2,23 +2,11 @@
 <project version="4">
   <component name="dataSourceStorageLocal">
     <data-source name="osinet_pvgo@localhost" uuid="c9a644f9-bd86-4e07-a193-30f9a7c68ed3">
-      <database-info product="MySQL" version="5.5.5-10.2.15-MariaDB" jdbc-version="4.0" driver-name="MySQL Connector Java" driver-version="mysql-connector-java-5.1.46 ( Revision: 9cc87a48e75c2d2e87c1a293b2862ce651cb256e )" dbms="MARIADB" exact-version="5.5.5">
-        <extra-name-characters>#@</extra-name-characters>
-        <identifier-quote-string>`</identifier-quote-string>
-      </database-info>
-      <case-sensitivity plain-identifiers="mixed" quoted-identifiers="upper" />
-      <secret-storage>master_key</secret-storage>
-      <user-name>root</user-name>
+      <database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="MYSQL" exact-version="0" />
       <introspection-schemas>*:@</introspection-schemas>
     </data-source>
     <data-source name="osinet_kurz@localhost" uuid="93d63da0-ec03-4f00-886c-a90f7dfa3df6">
-      <database-info product="MySQL" version="5.5.5-10.2.15-MariaDB" jdbc-version="4.0" driver-name="MySQL Connector Java" driver-version="mysql-connector-java-5.1.46 ( Revision: 9cc87a48e75c2d2e87c1a293b2862ce651cb256e )" dbms="MYSQL" exact-version="10.2.15">
-        <extra-name-characters>#@</extra-name-characters>
-        <identifier-quote-string>`</identifier-quote-string>
-      </database-info>
-      <case-sensitivity plain-identifiers="mixed" quoted-identifiers="mixed" />
-      <secret-storage>master_key</secret-storage>
-      <user-name>goroot</user-name>
+      <database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="MYSQL" exact-version="0" />
       <introspection-schemas>*:@</introspection-schemas>
     </data-source>
   </component>

+ 2 - 0
.idea/dataSources/aadde7b3-7c2f-4a6d-9eab-817a418d0632/storage_v2/_src_/schema/main.uQUzAA.meta

@@ -0,0 +1,2 @@
+#n:main
+!<md> [0, 0, null, null, -2147483648, -2147483648]

+ 7 - 0
.idea/dictionaries/fgm.xml

@@ -0,0 +1,7 @@
+<component name="ProjectDictionaryState">
+  <dictionary name="fgm">
+    <words>
+      <w>kurzd</w>
+    </words>
+  </dictionary>
+</component>

+ 8 - 0
.idea/runConfigurations/Build.xml

@@ -0,0 +1,8 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Build" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile" singleton="false">
+    <makefile filename="$PROJECT_DIR$/Makefile" target="build" workingDirectory="$PROJECT_DIR$" arguments="-j 2">
+      <envs />
+    </makefile>
+    <method v="2" />
+  </configuration>
+</component>

+ 1 - 1
.idea/runConfigurations/Make_clean.xml → .idea/runConfigurations/Clean.xml

@@ -1,5 +1,5 @@
 <component name="ProjectRunConfigurationManager">
-  <configuration default="false" name="Make clean" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile" singleton="false">
+  <configuration default="false" name="Clean" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile" singleton="false">
     <makefile filename="$PROJECT_DIR$/Makefile" target="clean" workingDirectory="$PROJECT_DIR$" arguments="">
       <envs />
     </makefile>

+ 14 - 0
.idea/runConfigurations/Kurzd.xml

@@ -0,0 +1,14 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Kurzd" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="true" show_console_on_std_err="true" show_console_on_std_out="true">
+    <module name="kurz" />
+    <working_directory value="$PROJECT_DIR$/cmd/kurzd" />
+    <go_parameters value="-o kurzd" />
+    <parameters value="serve api" />
+    <kind value="PACKAGE" />
+    <filePath value="$PROJECT_DIR$/" />
+    <package value="code.osinet.fr/fgm/kurz/cmd/kurzd" />
+    <directory value="$PROJECT_DIR$/cmd/kurzd" />
+    <output_directory value="$PROJECT_DIR$/cmd/kurzd" />
+    <method v="2" />
+  </configuration>
+</component>

+ 0 - 8
.idea/runConfigurations/Make_build.xml

@@ -1,8 +0,0 @@
-<component name="ProjectRunConfigurationManager">
-  <configuration default="false" name="Make build" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile" singleton="false">
-    <makefile filename="$PROJECT_DIR$/Makefile" target="build" workingDirectory="$PROJECT_DIR$" arguments="">
-      <envs />
-    </makefile>
-    <method v="2" />
-  </configuration>
-</component>

+ 13 - 0
.idea/runConfigurations/Test_Infra.xml

@@ -0,0 +1,13 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Test Infra" type="GoTestRunConfiguration" factoryName="Go Test" singleton="true">
+    <module name="kurz" />
+    <working_directory value="$PROJECT_DIR$/" />
+    <framework value="gotest" />
+    <kind value="PACKAGE" />
+    <package value="code.osinet.fr/fgm/kurz/infrastructure" />
+    <directory value="$PROJECT_DIR$/" />
+    <filePath value="$PROJECT_DIR$/" />
+    <pattern value="./..." />
+    <method v="2" />
+  </configuration>
+</component>

+ 13 - 0
.idea/runConfigurations/Test_domain.xml

@@ -0,0 +1,13 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Test domain" type="GoTestRunConfiguration" factoryName="Go Test" singleton="false">
+    <module name="kurz" />
+    <working_directory value="$PROJECT_DIR$/" />
+    <framework value="gotest" />
+    <kind value="PACKAGE" />
+    <package value="code.osinet.fr/fgm/kurz/domain" />
+    <directory value="$PROJECT_DIR$/" />
+    <filePath value="$PROJECT_DIR$/" />
+    <pattern value="./..." />
+    <method v="2" />
+  </configuration>
+</component>

+ 7 - 0
api/api.go

@@ -20,6 +20,10 @@ The Kurz Web API exposes these routes:
 Code 451 MAY be replaced by 403, for example when legal censorship includes a
 gag order, super-injunction (UK), National security letter (US) or similar
 mechanisms.
+
+These routes are exposed by running ListenAndServe(address), which is enough to
+configure the Kurz domain API. Be sure to also configure the domain SPI to have
+a complete application.
 */
 package api
 
@@ -39,6 +43,9 @@ type Target struct {
 	Target string `json:"target"`
 }
 
+/*
+ListenAndServe() is the
+ */
 func ListenAndServe(addr string) error {
 	router := mux.NewRouter()
 	router.HandleFunc("/{short}", handleGetShort).

+ 2 - 0
cmd/kurzd/dist.config.yml

@@ -5,3 +5,5 @@ database:
   test_dsn: <user>:<pass>@[<server>]/<database>
 api:
   address: ":8000"
+web:
+  address: ":3000"

+ 46 - 0
cmd/kurzd/serve.go

@@ -1,16 +1,62 @@
 package main
 
 import (
+	"code.osinet.fr/fgm/kurz/api"
+	"code.osinet.fr/fgm/kurz/domain"
+	"code.osinet.fr/fgm/kurz/infrastructure"
+	"database/sql"
 	"github.com/spf13/cobra"
+	"github.com/spf13/viper"
+	"log"
 )
 
 var cmdServe = &cobra.Command{
 	Args:  cobra.NoArgs,
 	Long:  "Start HTTP Server",
+	Run:   serveMarkupHandler,
 	Short: "Top-level command for HTTP Serving.",
 	Use:   "serve",
 }
 
+// db is the database connection shared by "serve *" commands.
+var db *sql.DB
+
 func init() {
 	cmd.AddCommand(cmdServe)
 }
+
+func ensureInfrastructure(db *sql.DB) *sql.DB {
+	if db != nil {
+		err := db.Ping()
+		if err != nil {
+			db = nil
+		}
+	}
+
+	if db != nil {
+		return db
+	}
+
+	dbDriver, dbDsn := infrastructure.ParseDbCred()
+	db, err := infrastructure.DbDial(dbDriver, dbDsn)
+	if err != nil {
+		panic(err)
+	}
+
+	domain.RegisterRepositories(
+		infrastructure.MySQLShortURLRepository{DB: db},
+		infrastructure.MySQLTargetURLRepository{DB: db},
+	)
+
+	return db
+}
+
+// serveMarkupHandler handles paths below /web/public.
+func serveMarkupHandler(_ *cobra.Command, args []string) {
+	db = ensureInfrastructure(db)
+	defer db.Close()
+
+	address := viper.Get("web.address").(string)
+	err := api.ListenAndServe(address)
+	log.Fatal(err)
+}

+ 3 - 21
cmd/kurzd/serve_api.go

@@ -1,12 +1,9 @@
 package main
 
 import (
-	"database/sql"
-	"fmt"
+	"log"
 
 	"code.osinet.fr/fgm/kurz/api"
-	"code.osinet.fr/fgm/kurz/domain"
-	"code.osinet.fr/fgm/kurz/infrastructure"
 	_ "code.osinet.fr/fgm/kurz/migrations"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
@@ -24,27 +21,12 @@ func init() {
 	cmdServe.AddCommand(cmdServeAPI)
 }
 
-func registerInfrastructure() *sql.DB {
-	dbDriver, dbDsn := infrastructure.ParseDbCred()
-	db, err := infrastructure.DbDial(dbDriver, dbDsn)
-	if err != nil {
-		panic(err)
-	}
-
-	domain.RegisterRepositories(
-		infrastructure.MySQLShortURLRepository{DB: db},
-		infrastructure.MySQLTargetURLRepository{DB: db},
-	)
-
-	return db
-}
-
 // Set up infrastructure and listen on specified address.
 func serveAPIHandler(_ *cobra.Command, args []string) {
-	db := registerInfrastructure()
+	db = ensureInfrastructure(db)
 	defer db.Close()
 
 	address := viper.Get("api.address").(string)
 	err := api.ListenAndServe(address)
-	fmt.Println(err)
+	log.Fatal(err)
 }