about summary refs log tree commit diff
path: root/nixos/modules/services/misc/ripple-data-api.nix
blob: 6e5ac7ab00bd0a14ff363829c6ccebf84d9ce0f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.rippleDataApi;

  deployment_env_config = builtins.toJSON {
    production = {
      port = toString cfg.port;
      maxSockets = 150;
      batchSize = 100;
      startIndex = 32570;
      rippleds = cfg.rippleds;
      redis = {
        enable = cfg.redis.enable;
        host = cfg.redis.host;
        port = cfg.redis.port;
        options.auth_pass = null;
      };
    };
  };

  db_config = builtins.toJSON {
    production = {
      username = optional (cfg.couchdb.pass != "") cfg.couchdb.user;
      password = optional (cfg.couchdb.pass != "") cfg.couchdb.pass;
      host = cfg.couchdb.host;
      port = cfg.couchdb.port;
      database = cfg.couchdb.db;
      protocol = "http";
    };
  };

in {
  options = {
    services.rippleDataApi = {
      enable = mkEnableOption "Whether to enable ripple data api.";

      port = mkOption {
        description = "Ripple data api port";
        default = 5993;
        type = types.int;
      };

      redis = {
        enable = mkOption {
          description = "Whether to enable caching of ripple data to redis.";
          default = true;
          type = types.bool;
        };

        host = mkOption {
          description = "Ripple data api redis host.";
          default = "localhost";
          type = types.str;
        };

        port = mkOption {
          description = "Ripple data api redis port.";
          default = 5984;
          type = types.int;
        };
      };

      couchdb = {
        host = mkOption {
          description = "Ripple data api couchdb host.";
          default = "localhost";
          type = types.str;
        };

        port = mkOption {
          description = "Ripple data api couchdb port.";
          default = 5984;
          type = types.int;
        };

        db = mkOption {
          description = "Ripple data api couchdb database.";
          default = "rippled";
          type = types.str;
        };

        user = mkOption {
          description = "Ripple data api couchdb username.";
          default = "rippled";
          type = types.str;
        };

        pass = mkOption {
          description = "Ripple data api couchdb password.";
          default = "";
          type = types.str;
        };

        create = mkOption {
          description = "Whether to create couchdb database needed by ripple data api.";
          type = types.bool;
          default = true;
        };
      };

      rippleds = mkOption {
        description = "List of rippleds to be used by ripple data api.";
        default = [
          "http://s_east.ripple.com:51234"
          "http://s_west.ripple.com:51234"
        ];
        type = types.listOf types.str;
      };
    };
  };

  config = mkIf (cfg.enable) {
    services.couchdb.enable = mkDefault true;
    services.couchdb.bindAddress = mkDefault "0.0.0.0";
    services.redis.enable = mkDefault true;

    systemd.services.ripple-data-api = {
      after = [ "couchdb.service" "redis.service" "ripple-data-api-importer.service" ];
      wantedBy = [ "multi-user.target" ];

      environment = {
        NODE_ENV = "production";
        DEPLOYMENT_ENVS_CONFIG = pkgs.writeText "deployment.environment.json" deployment_env_config;
        DB_CONFIG = pkgs.writeText "db.config.json" db_config;
      };

      serviceConfig = {
        ExecStart = "${pkgs.ripple-data-api}/bin/api";
        User = "ripple-data-api";
      };
    };

    systemd.services.ripple-data-importer = {
      after = [ "couchdb.service" ];
      wantedBy = [ "multi-user.target" ];
      path = [ pkgs.curl ];

      environment = {
        NODE_ENV = "production";
        DEPLOYMENT_ENVS_CONFIG = pkgs.writeText "deployment.environment.json" deployment_env_config;
        DB_CONFIG = pkgs.writeText "db.config.json" db_config;
        LOG_FILE = "/dev/null";
      };

      serviceConfig = {
        ExecStart = "${pkgs.ripple-data-api}/bin/importer live debug2";
        User = "ripple-data-api";
      };

      preStart = mkMerge [
        (mkIf (cfg.couchdb.create) ''
          HOST="http://${optionalString (cfg.couchdb.pass != "") "${cfg.couchdb.user}:${cfg.couchdb.pass}@"}${cfg.couchdb.host}:${toString cfg.couchdb.port}"
          curl -X PUT $HOST/${cfg.couchdb.db} || true
        '')
        "${pkgs.ripple-data-api}/bin/update-views"
      ];
    };

    users.extraUsers = singleton
      { name = "ripple-data-api";
        description = "Ripple data api user";
        uid = config.ids.uids.ripple-data-api;
      };
  };
}