Config.purs 11.1 KB
Newer Older
1 2 3 4 5
{- | Main Configuration of Gargantext Front-End

The main function to use for internal link in the Front-End
developpement is : toUrl.

6 7 8
* Example usage (depending on your Config):
toUrl Back  Corpus 1 == "http://localhost:8008/api/v1.0/corpus/1"
toUrl Front Corpus 1 == "http://localhost:2015/#/corpus/1"
9
-}
10 11
module Gargantext.Config where

12 13
import Prelude
import Data.Argonaut (class DecodeJson, decodeJson)
14
import Data.Foldable (foldMap)
15 16
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
17 18
import Data.Map (Map)
import Data.Map as DM
19
import Data.Maybe (Maybe(..), maybe)
20 21
import Data.Tuple (Tuple(..))

22 23
import Gargantext.Types

24 25 26 27
endConfig :: EndConfig
endConfig = endConfig' V10

endConfig' :: ApiVersion -> EndConfig
28
endConfig' v = { front : frontRelative
29
               , back  : backLocal v  }
30
--               , back  : backDemo v  }
31 32

------------------------------------------------------------------------
33 34 35 36 37
frontRelative :: Config
frontRelative = { baseUrl: ""
                , prePath: "/#/"
                }

38
frontCaddy :: Config
39 40
frontCaddy = { baseUrl: "http://localhost:2015"
             , prePath: "/#/"
41 42 43
             }

frontHaskell :: Config
44 45
frontHaskell = { baseUrl: "http://localhost:8008"
               , prePath: "/#/"
46 47
               }

48 49 50 51 52
frontDev :: Config
frontDev = { baseUrl: "https://dev.gargantext.org"
           , prePath: "/#/"
           }

53
frontProd :: Config
54 55
frontProd = { baseUrl: "https://gargantext.org"
            , prePath: "/#/"
56 57 58 59
            }

------------------------------------------------------------------------

60 61 62 63 64
backLocal :: ApiVersion -> Config
backLocal v = { baseUrl: "http://localhost:8008"
              , prePath: "/api/" <> show v <> "/"
              }

65 66 67 68 69
backDemo :: ApiVersion -> Config
backDemo v = { baseUrl: "https://demo.gargantext.org"
            , prePath: "/api/" <> show v <> "/"
            }

70
backDev :: ApiVersion -> Config
71 72
backDev v = { baseUrl: "https://dev.gargantext.org"
            , prePath: "/api/" <> show v <> "/"
73 74
            }

75 76 77 78 79
backDemo :: ApiVersion -> Config
backDemo v = { baseUrl: "https://demo.gargantext.org"
             , prePath: "/api/" <> show v <> "/"
             }

80
backProd :: ApiVersion -> Config
81 82 83
backProd v = { baseUrl: "https://gargantext.org"
             , prePath: "/api/" <> show v <> "/"
             }
84 85 86 87 88 89
------------------------------------------------------------------------

type EndConfig = { front :: Config
                 , back  :: Config
                 }

90 91
type Config = { baseUrl :: String
              , prePath :: String
92
              }
93

94 95 96 97 98
------------------------------------------------------------
type UrlBase  = String
type UrlPath  = String
type UrlParam = String
type Url      = String
99

100 101
doUrl :: UrlBase -> UrlPath -> UrlParam -> Url
doUrl b p ps = b <> p <> ps
Nicolas Pouillard's avatar
Nicolas Pouillard committed
102 103 104 105 106

endOf :: forall cfg. End -> { front :: cfg, back :: cfg } -> cfg
endOf Back  = _.back
endOf Front = _.front

107
endBaseUrl :: End -> EndConfig -> UrlBase
Nicolas Pouillard's avatar
Nicolas Pouillard committed
108 109
endBaseUrl end c = (endOf end c).baseUrl

110 111
endPathUrl :: End -> EndConfig -> Path -> Maybe Id -> UrlPath
endPathUrl end = pathUrl <<< endOf end
112

113 114 115 116
tabTypeDocs :: TabType -> UrlPath
tabTypeDocs (TabCorpus  t) = "table?view="   <> show t
tabTypeDocs (TabPairing t) = "pairing?view=" <> show t

117 118 119 120 121 122
limitUrl :: Limit -> UrlPath
limitUrl l = "&limit=" <> show l

offsetUrl :: Offset -> UrlPath
offsetUrl o = "&offset=" <> show o

123 124 125
orderUrl :: forall a. Show a => Maybe a -> UrlPath
orderUrl = maybe "" (\x -> "&order=" <> show x)

126 127 128 129
showTabType' :: TabType -> String
showTabType' (TabCorpus  t) = show t
showTabType' (TabPairing t) = show t

130 131 132 133 134 135 136
tabTypeNgramsGet :: TabType -> UrlPath
tabTypeNgramsGet (TabCorpus  t) = "listGet?ngramsType=" <> show t
tabTypeNgramsGet (TabPairing t) = "listGet?ngramsType=" <> show t -- TODO

tabTypeNgramsPut :: TabType -> UrlPath
tabTypeNgramsPut (TabCorpus  t) = "list?ngramsType=" <> show t
tabTypeNgramsPut (TabPairing t) = "list?ngramsType=" <> show t -- TODO
137

138 139 140
pathUrl :: Config -> Path -> Maybe Id -> UrlPath
pathUrl c (Tab t o l s) i =
    pathUrl c (NodeAPI Node) i <>
141
      "/" <> tabTypeDocs t <> offsetUrl o <> limitUrl l <> orderUrl s
142 143
pathUrl c (Children n o l s) i =
    pathUrl c (NodeAPI Node) i <>
144
      "/" <> "children?type=" <> show n <> offsetUrl o <> limitUrl l <> orderUrl s
145 146 147 148 149 150
pathUrl c (GetNgrams
            { tabType: t
            , offset: o
            , limit: l
            , listIds
            , termListFilter: tlf
151
            , termSizeFilter: tsf
152 153
            , searchQuery: q
            }) i =
154
    pathUrl c (NodeAPI Node) i <> "/" <> tabTypeNgramsGet t
155 156 157
      <> offsetUrl o <> limitUrl l
      <> foldMap (\x -> "&list=" <> show x) listIds
      <> foldMap (\x -> "&listType=" <> show x) tlf
158 159 160 161
      <> foldMap (\x -> case x of
                          MonoTerm  -> "&minTermSize=0&maxTermSize=1"
                          MultiTerm -> "&minTermSize=2"
                 ) tsf
162
      <> if q == "" then "" else ("&search=" <> q)
163 164
pathUrl c (PutNgrams t listid) i =
    pathUrl c (NodeAPI Node) i <> "/" <> tabTypeNgramsPut t <> listid'
165
  where
166
    listid' = maybe "" (\x -> "&list=" <> show x) listid
167 168 169
pathUrl c Auth Nothing = c.prePath <> "auth"
pathUrl c Auth (Just _) = "impossible" -- TODO better types
pathUrl c (NodeAPI nt) i = c.prePath <> nodeTypeUrl nt <> (maybe "" (\i' -> "/" <> show i') i)
170 171 172
pathUrl c (Search {limit,offset,orderBy}) _TODO =
  c.prePath <> "search/?dummy=dummy"
    <> offsetUrl offset <> limitUrl limit <> orderUrl orderBy
173 174
pathUrl c (CorpusMetrics {tabType, listId, limit}) i =
    pathUrl c (NodeAPI Corpus) i <> "/metrics"
175
      <> "?list=" <> show listId
176 177
      <> "&ngramsType=" <> showTabType' tabType
      <> maybe "" (\x -> "&limit=" <> show x) limit
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192
------------------------------------------------------------

class ToUrl a where
  toUrl :: End -> a -> Maybe Id -> Url

instance toUrlNodeType :: ToUrl NodeType where
  toUrl e nt i = toUrl e (NodeAPI nt) i

instance toUrlPath :: ToUrl Path where
  toUrl e p i = doUrl base path params
    where
      base   = endBaseUrl e endConfig
      path   = endPathUrl e endConfig p i
      params = ""
193
------------------------------------------------------------
194

Alexandre Delanoë's avatar
Alexandre Delanoë committed
195 196
data NodeType = NodeUser
              | Annuaire
197
                | NodeContact
Alexandre Delanoë's avatar
Alexandre Delanoë committed
198
              | Corpus
199
--                | NodeDocument
200
              | CorpusV3
Alexandre Delanoë's avatar
Alexandre Delanoë committed
201
              | Dashboard
202
              | Url_Document
203
              | Error
Alexandre Delanoë's avatar
Alexandre Delanoë committed
204 205 206
              | Folder
              | Graph
              | Individu
207
              | Node
208
              | Nodes
Alexandre Delanoë's avatar
Alexandre Delanoë committed
209
              | Tree
210
              | NodeList
211

212 213 214 215

instance showNodeType :: Show NodeType where
  show NodeUser      = "NodeUser"
  show Annuaire      = "Annuaire"
216
  show NodeContact   = "NodeContact"
217 218 219
  show Corpus        = "NodeCorpus"
  show CorpusV3      = "NodeCorpusV3"
  show Dashboard     = "NodeDashboard"
220
  show Url_Document  = "NodeDocument"
221 222 223 224 225 226 227 228
  --show NodeDocument  = "NodeDocument"
  show Error         = "NodeError"
  show Folder        = "NodeFolder"
  show Graph         = "NodeGraph"
  show Individu      = "NodeIndividu"
  show Node          = "Node"
  show Nodes         = "Nodes"
  show Tree          = "NodeTree"
229 230 231
  show NodeList      = "NodeList"

type ListId = Int
232

233 234
data Path
  = Auth
235 236
  | Tab      TabType  Offset Limit (Maybe OrderBy)
  | Children NodeType Offset Limit (Maybe OrderBy)
237 238 239 240 241 242 243
  | GetNgrams
      { tabType        :: TabType
      , offset         :: Offset
      , limit          :: Limit
      , orderBy        :: Maybe OrderBy
      , listIds        :: Array ListId
      , termListFilter :: Maybe TermList
244
      , termSizeFilter :: Maybe TermSize
245 246
      , searchQuery    :: String
      }
Nicolas Pouillard's avatar
Nicolas Pouillard committed
247
  | PutNgrams TabType (Maybe ListId)
248
  | NodeAPI NodeType
249 250 251 252 253 254
  | Search  { {-id :: Int
            , query    :: Array String
            ,-} limit  :: Limit
            , offset   :: Offset
            , orderBy  :: Maybe OrderBy
            }
255 256 257 258
  | CorpusMetrics { tabType :: TabType
                  , listId  :: ListId
                  , limit   :: Maybe Limit
                  }
259

260
data End = Back | Front
261
type Id  = Int
262 263 264

type Limit  = Int
type Offset = Int
265 266
data OrderBy = DateAsc  | DateDesc
             | TitleAsc | TitleDesc
267
             | ScoreDesc  | ScoreAsc
268 269 270 271 272

derive instance genericOrderBy :: Generic OrderBy _

instance showOrderBy :: Show OrderBy where
  show = genericShow
273

274
------------------------------------------------------------
275 276 277 278 279
data ApiVersion = V10 | V11
instance showApiVersion :: Show ApiVersion where
  show V10 = "v1.0"
  show V11 = "v1.1"
------------------------------------------------------------
280

281 282
data CTabNgramType = CTabTerms | CTabSources | CTabAuthors | CTabInstitutes

283 284
derive instance eqCTabNgramType :: Eq CTabNgramType

285 286 287 288 289 290 291 292
instance showCTabNgramType :: Show CTabNgramType where
  show CTabTerms      = "Terms"
  show CTabSources    = "Sources"
  show CTabAuthors    = "Authors"
  show CTabInstitutes = "Institutes"

data PTabNgramType = PTabPatents | PTabBooks | PTabCommunication

293 294
derive instance eqPTabNgramType :: Eq PTabNgramType

295 296 297 298 299 300 301
instance showPTabNgramType :: Show PTabNgramType where
  show PTabPatents       = "Patents"
  show PTabBooks         = "Books"
  show PTabCommunication = "Communication"

data TabSubType a = TabDocs | TabNgramType a | TabTrash

302 303
derive instance eqTabSubType :: Eq a => Eq (TabSubType a)

304 305 306 307
instance showTabSubType :: Show a => Show (TabSubType a) where
  show TabDocs          = "Docs"
  show (TabNgramType a) = show a
  show TabTrash         = "Trash"
308

309 310 311
data TabType
  = TabCorpus  (TabSubType CTabNgramType)
  | TabPairing (TabSubType PTabNgramType)
312

313 314
derive instance eqTabType :: Eq TabType

315 316
derive instance genericTabType :: Generic TabType _

317
instance showTabType :: Show TabType where
318
  show = genericShow
319

320
------------------------------------------------------------
321 322 323 324 325 326 327 328 329 330 331 332 333
nodeTypeUrl :: NodeType -> Url
nodeTypeUrl Annuaire  = "annuaire"
nodeTypeUrl Corpus    = "corpus"
nodeTypeUrl CorpusV3  = "corpus"
nodeTypeUrl Dashboard = "dashboard"
nodeTypeUrl Url_Document  = "document"
nodeTypeUrl Error     = "ErrorNodeType"
nodeTypeUrl Folder    = "folder"
nodeTypeUrl Graph     = "graph"
nodeTypeUrl Individu  = "individu"
nodeTypeUrl Node      = "node"
nodeTypeUrl Nodes      = "nodes"
nodeTypeUrl NodeUser  = "user"
334
nodeTypeUrl NodeContact = "contact"
335
nodeTypeUrl Tree      = "tree"
336
nodeTypeUrl NodeList  = "list"
337

338
readNodeType :: String -> NodeType
339 340 341 342 343 344 345 346 347 348 349 350 351
readNodeType "NodeAnnuaire"  = Annuaire
readNodeType "NodeDashboard" = Dashboard
readNodeType "Document"      = Url_Document
readNodeType "NodeFolder"    = Folder
readNodeType "NodeGraph"     = Graph
readNodeType "Individu"      = Individu
readNodeType "Node"          = Node
readNodeType "Nodes"         = Nodes
readNodeType "NodeCorpus"    = Corpus
readNodeType "NodeCorpusV3"  = CorpusV3
readNodeType "NodeUser"      = NodeUser
readNodeType "NodeContact"   = NodeContact
readNodeType "Tree"          = Tree
352
readNodeType "NodeList"      = NodeList
353
readNodeType _               = Error
354
{-
355
------------------------------------------------------------
356 357 358 359 360
instance ordNodeType :: Ord NodeType where
  compare n1 n2 = compare (show n1) (show n2)

instance eqNodeType :: Eq NodeType where
  eq n1 n2  = eq (show n1) (show n2)
361
-}
362
------------------------------------------------------------
363 364 365 366
instance decodeJsonNodeType :: DecodeJson NodeType where
  decodeJson json = do
    obj <- decodeJson json
    pure $ readNodeType obj