mirror of https://github.com/knative/docs.git
Make samples less scary when no env var is provided (#443)
* Node.js * Python * csharp * go * other samples * Fix Node README
This commit is contained in:
parent
26d3de54de
commit
9ea6e2c65d
|
@ -40,8 +40,8 @@ recreate the source files from this folder.
|
|||
```csharp
|
||||
app.Run(async (context) =>
|
||||
{
|
||||
var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED";
|
||||
await context.Response.WriteAsync($"Hello World: {target}\n");
|
||||
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
|
||||
await context.Response.WriteAsync($"Hello {target}\n");
|
||||
});
|
||||
```
|
||||
|
||||
|
|
|
@ -27,8 +27,8 @@ namespace helloworld_csharp
|
|||
|
||||
app.Run(async (context) =>
|
||||
{
|
||||
var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED";
|
||||
await context.Response.WriteAsync($"Hello World: {target}\n");
|
||||
var target = Environment.GetEnvironmentVariable("TARGET") ?? "World";
|
||||
await context.Response.WriteAsync($"Hello {target}\n");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,9 +36,9 @@ following instructions recreate the source files from this folder.
|
|||
log.Print("Hello world received a request.")
|
||||
target := os.Getenv("TARGET")
|
||||
if target == "" {
|
||||
target = "NOT SPECIFIED"
|
||||
target = "World"
|
||||
}
|
||||
fmt.Fprintf(w, "Hello World: %s!\n", target)
|
||||
fmt.Fprintf(w, "Hello %s!\n", target)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -27,9 +27,9 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||
log.Print("Hello world received a request.")
|
||||
target := os.Getenv("TARGET")
|
||||
if target == "" {
|
||||
target = "NOT SPECIFIED"
|
||||
target = "World"
|
||||
}
|
||||
fmt.Fprintf(w, "Hello World: %s!\n", target)
|
||||
fmt.Fprintf(w, "Hello %s!\n", target)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -64,14 +64,14 @@ following instructions recreate the source files from this folder.
|
|||
|
||||
main :: IO ()
|
||||
main = do
|
||||
t <- fromMaybe "NOT SPECIFIED" <$> lookupEnv "TARGET"
|
||||
t <- fromMaybe "World" <$> lookupEnv "TARGET"
|
||||
scotty 8080 (route t)
|
||||
|
||||
route :: String -> ScottyM()
|
||||
route t = get "/" $ hello t
|
||||
|
||||
hello :: String -> ActionM()
|
||||
hello t = text $ pack ("Hello world: " ++ t)
|
||||
hello t = text $ pack ("Hello " ++ t)
|
||||
```
|
||||
|
||||
1. In your project directory, create a file named `Dockerfile` and copy the code
|
||||
|
|
|
@ -10,11 +10,11 @@ import Web.Scotty.Trans
|
|||
|
||||
main :: IO ()
|
||||
main = do
|
||||
t <- fromMaybe "NOT SPECIFIED" <$> lookupEnv "TARGET"
|
||||
t <- fromMaybe "World" <$> lookupEnv "TARGET"
|
||||
scotty 8080 (route t)
|
||||
|
||||
route :: String -> ScottyM()
|
||||
route t = get "/" $ hello t
|
||||
|
||||
hello :: String -> ActionM()
|
||||
hello t = text $ pack ("Hello world: " ++ t)
|
||||
hello t = text $ pack ("Hello " ++ t)
|
||||
|
|
|
@ -52,14 +52,14 @@ recreate the source files from this folder.
|
|||
@SpringBootApplication
|
||||
public class HelloworldApplication {
|
||||
|
||||
@Value("${TARGET:NOT SPECIFIED}")
|
||||
@Value("${TARGET:World}")
|
||||
String target;
|
||||
|
||||
@RestController
|
||||
class HelloworldController {
|
||||
@GetMapping("/")
|
||||
String hello() {
|
||||
return "Hello World: " + target;
|
||||
return "Hello " + target;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,14 +9,14 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@SpringBootApplication
|
||||
public class HelloworldApplication {
|
||||
|
||||
@Value("${TARGET:NOT SPECIFIED}")
|
||||
@Value("${TARGET:World}")
|
||||
String message;
|
||||
|
||||
@RestController
|
||||
class HelloworldController {
|
||||
@GetMapping("/")
|
||||
String hello() {
|
||||
return "Hello World: " + message;
|
||||
return "Hello " + message;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,6 @@ WORKDIR /usr/src/app
|
|||
COPY package*.json ./
|
||||
|
||||
RUN npm install --only=production
|
||||
# If you are building your code for production
|
||||
# RUN npm install --only=production
|
||||
|
||||
# Bundle app source
|
||||
COPY . .
|
||||
|
|
|
@ -52,8 +52,8 @@ recreate the source files from this folder.
|
|||
app.get('/', function (req, res) {
|
||||
console.log('Hello world received a request.');
|
||||
|
||||
var target = process.env.TARGET || 'NOT SPECIFIED';
|
||||
res.send('Hello world: ' + target);
|
||||
var target = process.env.TARGET || 'World';
|
||||
res.send('Hello ' + target);
|
||||
});
|
||||
|
||||
var port = 8080;
|
||||
|
@ -71,8 +71,7 @@ recreate the source files from this folder.
|
|||
"description": "",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"start": "node app.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "Apache-2.0"
|
||||
|
@ -94,9 +93,7 @@ recreate the source files from this folder.
|
|||
# where available (npm@5+)
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
# If you are building your code for production
|
||||
# RUN npm install --only=production
|
||||
RUN npm install --only=production
|
||||
|
||||
# Bundle app source
|
||||
COPY . .
|
||||
|
|
|
@ -20,8 +20,8 @@ const app = express();
|
|||
app.get('/', function (req, res) {
|
||||
console.log('Hello world received a request.');
|
||||
|
||||
var target = process.env.TARGET || 'NOT SPECIFIED';
|
||||
res.send('Hello world: ' + target);
|
||||
var target = process.env.TARGET || 'World';
|
||||
res.send('Hello ' + target);
|
||||
});
|
||||
|
||||
var port = 8080;
|
||||
|
|
|
@ -4,12 +4,11 @@
|
|||
"description": "Simple hello world sample in Node",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"start": "node app.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/knative/serving.git"
|
||||
"url": "git+https://github.com/knative/docs.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "Apache-2.0",
|
||||
|
|
|
@ -29,8 +29,8 @@ following instructions recreate the source files from this folder.
|
|||
|
||||
```php
|
||||
<?php
|
||||
$target = getenv('TARGET', true) ?: "NOT SPECIFIED";
|
||||
echo sprintf("Hello World: %s!\n", $target);
|
||||
$target = getenv('TARGET', true) ?: "World";
|
||||
echo sprintf("Hello %s!\n", $target);
|
||||
?>
|
||||
```
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?php
|
||||
$target = getenv('TARGET', true) ?: "NOT SPECIFIED";
|
||||
echo sprintf("Hello World: %s!\n", $target);
|
||||
$target = getenv('TARGET', true) ?: "World";
|
||||
echo sprintf("Hello %s!\n", $target);
|
||||
?>
|
||||
|
|
|
@ -35,8 +35,8 @@ The following instructions recreate the source files from this folder.
|
|||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
target = os.environ.get('TARGET', 'NOT SPECIFIED')
|
||||
return 'Hello World: {}!\n'.format(target)
|
||||
target = os.environ.get('TARGET', 'World')
|
||||
return 'Hello {}!\n'.format(target)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
|
||||
|
|
|
@ -6,8 +6,8 @@ app = Flask(__name__)
|
|||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
target = os.environ.get('TARGET', 'NOT SPECIFIED')
|
||||
return 'Hello World: {}!\n'.format(target)
|
||||
target = os.environ.get('TARGET', 'World')
|
||||
return 'Hello {}!\n'.format(target)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
|
||||
|
|
|
@ -33,8 +33,8 @@ The following instructions recreate the source files from this folder.
|
|||
set :bind, '0.0.0.0'
|
||||
|
||||
get '/' do
|
||||
target = ENV['TARGET'] || 'NOT SPECIFIED'
|
||||
"Hello World: #{target}!\n"
|
||||
target = ENV['TARGET'] || 'World'
|
||||
"Hello #{target}!\n"
|
||||
end
|
||||
```
|
||||
|
||||
|
|
|
@ -3,6 +3,6 @@ require 'sinatra'
|
|||
set :bind, '0.0.0.0'
|
||||
|
||||
get '/' do
|
||||
target = ENV['TARGET'] || 'NOT SPECIFIED'
|
||||
"Hello World: #{target}!\n"
|
||||
target = ENV['TARGET'] || 'World'
|
||||
"Hello #{target}!\n"
|
||||
end
|
||||
|
|
|
@ -53,10 +53,10 @@ following instructions recreate the source files from this folder.
|
|||
let new_service = || {
|
||||
service_fn_ok(|_| {
|
||||
|
||||
let mut hello = "Hello world: ".to_string();
|
||||
let mut hello = "Hello ".to_string();
|
||||
match env::var("TARGET") {
|
||||
Ok(target) => {hello.push_str(&target);},
|
||||
Err(_e) => {hello.push_str("NOT SPECIFIED")},
|
||||
Err(_e) => {hello.push_str("World")},
|
||||
};
|
||||
|
||||
Response::new(Body::from(hello))
|
||||
|
|
|
@ -15,10 +15,10 @@ fn main() {
|
|||
let new_service = || {
|
||||
service_fn_ok(|_| {
|
||||
|
||||
let mut hello = "Hello world: ".to_string();
|
||||
let mut hello = "Hello ".to_string();
|
||||
match env::var("TARGET") {
|
||||
Ok(target) => {hello.push_str(&target);},
|
||||
Err(_e) => {hello.push_str("NOT SPECIFIED")},
|
||||
Err(_e) => {hello.push_str("World")},
|
||||
};
|
||||
|
||||
Response::new(Body::from(hello))
|
||||
|
|
Loading…
Reference in New Issue