mirror of
https://github.com/neocities/neocities.git
synced 2025-04-28 11:12:30 +02:00
Basic creation of initial site
This commit is contained in:
parent
0aaf894fa3
commit
5deb20e264
5 changed files with 54 additions and 3 deletions
42
app.rb
42
app.rb
|
@ -5,6 +5,24 @@ use Rack::Session::Cookie, key: 'neocities',
|
||||||
expire_after: 31556926, # one year in seconds
|
expire_after: 31556926, # one year in seconds
|
||||||
secret: $config['session_secret']
|
secret: $config['session_secret']
|
||||||
|
|
||||||
|
get %r{.+} do
|
||||||
|
subname = request.host.match /[\w-]+/
|
||||||
|
pass if subname.nil?
|
||||||
|
subname = subname.to_s
|
||||||
|
pass if subname == 'www' || subname == 'neocities' || subname == 'testneocities'
|
||||||
|
|
||||||
|
base_path = site_base_path subname
|
||||||
|
path = File.join(base_path, (request.path =~ /\/$/ ? (request.path + 'index.html') : request.path))
|
||||||
|
|
||||||
|
if File.exist?(path)
|
||||||
|
send_file path
|
||||||
|
else
|
||||||
|
send_file File.join(base_path, 'not_found.html')
|
||||||
|
end
|
||||||
|
|
||||||
|
send_file path
|
||||||
|
end
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
dashboard_if_signed_in
|
dashboard_if_signed_in
|
||||||
slim :index
|
slim :index
|
||||||
|
@ -28,8 +46,22 @@ end
|
||||||
post '/create' do
|
post '/create' do
|
||||||
dashboard_if_signed_in
|
dashboard_if_signed_in
|
||||||
@site = Site.new username: params[:username], password: params[:password], email: params[:email], new_tags: params[:tags]
|
@site = Site.new username: params[:username], password: params[:password], email: params[:email], new_tags: params[:tags]
|
||||||
|
|
||||||
if @site.valid?
|
if @site.valid?
|
||||||
DB.transaction { @site.save }
|
|
||||||
|
base_path = site_base_path @site.username
|
||||||
|
|
||||||
|
DB.transaction {
|
||||||
|
@site.save
|
||||||
|
|
||||||
|
begin
|
||||||
|
FileUtils.mkdir base_path
|
||||||
|
rescue Errno::EEXIST
|
||||||
|
end
|
||||||
|
|
||||||
|
File.write File.join(base_path, 'index.html'), slim(:'templates/index', pretty: true, layout: false)
|
||||||
|
File.write File.join(base_path, 'not_found.html'), slim(:'templates/not_found', pretty: true, layout: false)
|
||||||
|
}
|
||||||
|
|
||||||
session[:id] = @site.id
|
session[:id] = @site.id
|
||||||
redirect '/dashboard'
|
redirect '/dashboard'
|
||||||
|
@ -71,3 +103,11 @@ end
|
||||||
def current_site
|
def current_site
|
||||||
@site ||= Site[id: session[:id]]
|
@site ||= Site[id: session[:id]]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def site_base_path(subname)
|
||||||
|
File.join settings.public_folder, 'sites', subname
|
||||||
|
end
|
||||||
|
|
||||||
|
def template_site_title(username)
|
||||||
|
"#{username.capitalize}#{username[username.length-1] == 's' ? "'" : "'s"} Site"
|
||||||
|
end
|
|
@ -1,5 +1,6 @@
|
||||||
class Site < Sequel::Model
|
class Site < Sequel::Model
|
||||||
MINIMUM_PASSWORD_LENGTH = 5
|
MINIMUM_PASSWORD_LENGTH = 5
|
||||||
|
USERNAME_REGEX = /[^\w-]/i
|
||||||
many_to_one :server
|
many_to_one :server
|
||||||
many_to_many :tags
|
many_to_many :tags
|
||||||
|
|
||||||
|
@ -59,7 +60,7 @@ class Site < Sequel::Model
|
||||||
errors.add :over_capacity, 'We are currently at capacity, and cannot create your home page. We will fix this shortly. Please come back later and try again, our apologies.'
|
errors.add :over_capacity, 'We are currently at capacity, and cannot create your home page. We will fix this shortly. Please come back later and try again, our apologies.'
|
||||||
end
|
end
|
||||||
|
|
||||||
if values[:username].nil? || values[:username].empty? || values[:username].match(/[^\w.-]/i)
|
if values[:username].nil? || values[:username].empty? || values[:username].match(USERNAME_REGEX)
|
||||||
errors.add :username, 'A valid username is required.'
|
errors.add :username, 'A valid username is required.'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
.row
|
.row
|
||||||
.span6
|
.span6
|
||||||
p First, enter a username. This will also be used as your site path.<br><b>Do not forget this, it will be used to sign in to and manage your home page.</b><br>It cannot contain spaces, and can only use the following characters: a-z A-Z 0-9 . _ -
|
p First, enter a username. This will also be used as your site path.<br><b>Do not forget this, it will be used to sign in to and manage your home page.</b><br>It cannot contain spaces, and can only use the following characters: a-z A-Z 0-9 _ -
|
||||||
|
|
||||||
.row
|
.row
|
||||||
.span1
|
.span1
|
||||||
|
|
5
views/templates/index.slim
Normal file
5
views/templates/index.slim
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title #{template_site_title @site.username} - Front Page
|
||||||
|
body
|
||||||
|
p This is a new page! Coming soon.
|
5
views/templates/not_found.slim
Normal file
5
views/templates/not_found.slim
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title #{template_site_title @site.username} - Not Found
|
||||||
|
body
|
||||||
|
p The requested page was not found.
|
Loading…
Add table
Reference in a new issue