v / cmd / tools / vcreate / project_model_web.v
120 lines · 105 sloc · 2.01 KB · 008097824561c886eae0451d886ea7a969ff2653
Raw
1module main
2
3import os
4
5fn (mut c Create) set_web_project_files() {
6 base := if c.new_dir { c.name } else { '' }
7 c.files << ProjectFiles{
8 path: os.join_path(base, 'assets', 'main.css')
9 content: "html, body {
10 font-family: Arial, Helvetica, sans-serif;
11 background: #f4efe6;
12 color: #1f2933;
13 height: 100%;
14 margin: 0;
15}
16
17body {
18 display: grid;
19 place-items: center;
20 padding: 24px;
21}
22
23.card {
24 max-width: 560px;
25 padding: 32px;
26 border: 1px solid #d8ccb8;
27 border-radius: 16px;
28 background: #fffaf1;
29 box-shadow: 0 18px 50px rgba(69, 52, 35, 0.08);
30}
31
32.eyebrow {
33 margin: 0 0 12px;
34 font-size: 12px;
35 font-weight: 700;
36 letter-spacing: 0.12em;
37 text-transform: uppercase;
38 color: #8b5e34;
39}
40
41h1 {
42 margin: 0 0 12px;
43 font-size: 40px;
44 line-height: 1.1;
45}
46
47p {
48 margin: 0;
49 font-size: 18px;
50 line-height: 1.6;
51}
52
53.hint {
54 margin-top: 16px;
55 font-size: 14px;
56 color: #52606d;
57}
58
59code {
60 font-family: 'Courier New', monospace;
61}
62"
63 }
64 c.files << ProjectFiles{
65 path: os.join_path(base, 'templates', 'index.html')
66 content: "<!doctype html>
67<html lang='en'>
68<head>
69 <meta charset='UTF-8'>
70 <meta name='viewport' content='width=device-width, initial-scale=1.0'>
71 <title>@title</title>
72 @css '/assets/main.css'
73</head>
74<body>
75 <main class='card'>
76 <p class='eyebrow'>veb starter</p>
77 <h1>@title</h1>
78 <p>@message</p>
79 <p class='hint'>Try <code>GET /health</code> for a plain-text response.</p>
80 </main>
81</body>
82</html>
83"
84 }
85 c.files << ProjectFiles{
86 path: os.join_path(base, 'main.v')
87 content: "module main
88
89import os
90import veb
91
92pub struct Context {
93 veb.Context
94}
95
96pub struct App {
97 veb.StaticHandler
98}
99
100pub fn (app &App) index() veb.Result {
101 title := '${c.name}'
102 message := 'Your new V web app is powered by veb.'
103 return \$veb.html()
104}
105
106@['/health'; get]
107pub fn (app &App) health(mut ctx Context) veb.Result {
108 return ctx.text('ok')
109}
110
111fn main() {
112 // Keep asset and template paths stable for `v run .`.
113 os.chdir(os.dir(@FILE))!
114 mut app := &App{}
115 app.handle_static('assets', false)!
116 veb.run[App, Context](mut app, 8080)
117}
118"
119 }
120}
121