@@ -17,10 +17,15 @@ use std::sync::mpsc::channel;
17
17
18
18
#[ allow( dead_code) ]
19
19
fn main ( ) {
20
+ // Initialize logging
20
21
env_logger:: init ( ) . unwrap ( ) ;
22
+
23
+ // If there isn't a git checkout containing the crate index repo at the path specified
24
+ // by `GIT_REPO_CHECKOUT`, delete that directory and clone the repo specified by `GIT_REPO_URL`
25
+ // into that directory instead. Uses the credentials specified in `GIT_HTTP_USER` and
26
+ // `GIT_HTTP_PWD` via the `cargo_registry::git::credentials` function.
21
27
let url = env ( "GIT_REPO_URL" ) ;
22
28
let checkout = PathBuf :: from ( env ( "GIT_REPO_CHECKOUT" ) ) ;
23
-
24
29
let repo = match git2:: Repository :: open ( & checkout) {
25
30
Ok ( r) => r,
26
31
Err ( ..) => {
@@ -36,11 +41,15 @@ fn main() {
36
41
. unwrap ( )
37
42
}
38
43
} ;
44
+
45
+ // All commits to the index registry made through crates.io will be made by bors, the Rust
46
+ // community's friendly GitHub bot.
39
47
let mut cfg = repo. config ( ) . unwrap ( ) ;
40
48
cfg. set_str ( "user.name" , "bors" ) . unwrap ( ) ;
41
49
cfg
. set_str ( "user.email" , "[email protected] " ) . unwrap ( ) ;
42
50
43
51
let api_protocol = String :: from ( "https" ) ;
52
+
44
53
let mirror = if env:: var ( "MIRROR" ) . is_ok ( ) {
45
54
Replica :: ReadOnlyMirror
46
55
} else {
@@ -56,7 +65,9 @@ fn main() {
56
65
57
66
let uploader = match ( cargo_env, mirror) {
58
67
( Env :: Production , Replica :: Primary ) => {
59
- // `env` panics if these vars are not set
68
+ // `env` panics if these vars are not set, and in production for a primary instance,
69
+ // that's what we want since we don't want to be able to start the server if the server
70
+ // doesn't know where to upload crates.
60
71
Uploader :: S3 {
61
72
bucket : s3:: Bucket :: new (
62
73
env ( "S3_BUCKET" ) ,
@@ -69,8 +80,14 @@ fn main() {
69
80
}
70
81
}
71
82
( Env :: Production , Replica :: ReadOnlyMirror ) => {
72
- // Read-only mirrors don't need access key or secret key,
73
- // but they might have them. Definitely need bucket though.
83
+ // Read-only mirrors don't need access key or secret key since by definition,
84
+ // they'll only need to read from a bucket, not upload.
85
+ //
86
+ // Read-only mirrors might have access key or secret key, so use them if those
87
+ // environment variables are set.
88
+ //
89
+ // Read-only mirrors definitely need bucket though, so that they know where
90
+ // to serve crate files from.
74
91
Uploader :: S3 {
75
92
bucket : s3:: Bucket :: new (
76
93
env ( "S3_BUCKET" ) ,
@@ -82,8 +99,13 @@ fn main() {
82
99
proxy : None ,
83
100
}
84
101
}
102
+ // In Development mode, either running as a primary instance or a read-only mirror
85
103
_ => {
86
104
if env:: var ( "S3_BUCKET" ) . is_ok ( ) {
105
+ // If we've set the `S3_BUCKET` variable to any value, use all of the values
106
+ // for the related S3 environment variables and configure the app to upload to
107
+ // and read from S3 like production does. All values except for bucket are
108
+ // optional, like production read-only mirrors.
87
109
println ! ( "Using S3 uploader" ) ;
88
110
Uploader :: S3 {
89
111
bucket : s3:: Bucket :: new (
@@ -96,6 +118,9 @@ fn main() {
96
118
proxy : None ,
97
119
}
98
120
} else {
121
+ // If we don't set the `S3_BUCKET` variable, we'll use a development-only
122
+ // uploader that makes it possible to run and publish to a locally-running
123
+ // crates.io instance without needing to set up an account and a bucket in S3.
99
124
println ! ( "Using local uploader, crate files will be in the dist directory" ) ;
100
125
Uploader :: Local
101
126
}
@@ -110,13 +135,15 @@ fn main() {
110
135
gh_client_secret : env ( "GH_CLIENT_SECRET" ) ,
111
136
db_url : env ( "DATABASE_URL" ) ,
112
137
env : cargo_env,
113
- max_upload_size : 10 * 1024 * 1024 ,
138
+ max_upload_size : 10 * 1024 * 1024 , // 10 MB default file upload size limit
114
139
mirror : mirror,
115
140
api_protocol : api_protocol,
116
141
} ;
117
142
let app = cargo_registry:: App :: new ( & config) ;
118
143
let app = cargo_registry:: middleware ( Arc :: new ( app) ) ;
119
144
145
+ // On every server restart, ensure the categories available in the database match
146
+ // the information in *src/categories.toml*.
120
147
cargo_registry:: categories:: sync ( ) . unwrap ( ) ;
121
148
122
149
let port = if heroku {
@@ -131,7 +158,11 @@ fn main() {
131
158
let mut cfg = civet:: Config :: new ( ) ;
132
159
cfg. port ( port) . threads ( threads) . keep_alive ( true ) ;
133
160
let _a = Server :: start ( cfg, app) ;
161
+
134
162
println ! ( "listening on port {}" , port) ;
163
+
164
+ // Creating this file tells heroku to tell nginx that the application is ready
165
+ // to receive traffic.
135
166
if heroku {
136
167
File :: create ( "/tmp/app-initialized" ) . unwrap ( ) ;
137
168
}
0 commit comments