Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
wip heroweb test macos error on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
timurgordon committed Sep 20, 2024
1 parent 680d780 commit 3e6a58b
Show file tree
Hide file tree
Showing 25 changed files with 535 additions and 213 deletions.
31 changes: 0 additions & 31 deletions crystallib/webserver/heroweb/analytics_controller.v

This file was deleted.

39 changes: 0 additions & 39 deletions crystallib/webserver/heroweb/assets_controller.v

This file was deleted.

61 changes: 61 additions & 0 deletions crystallib/webserver/heroweb/controller_assets.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module heroweb

import veb
import freeflowuniverse.crystallib.core.pathlib
import freeflowuniverse.crystallib.webserver.log
import time

@['/assets']
pub fn (mut app App) assets(mut ctx Context) veb.Result {
ptrs := app.db.authorized_ptrs(ctx.user_id, .read) or {
return ctx.server_error(err.str())
}
docs := ptrs.map(Document{
title: it.name.title()
description: it.description
url: '/view/asset/${it.name}'
})

return ctx.json(docs)
}

// this endpoint serves assets dynamically
@['/asset/:paths...']
pub fn (app &App) asset(mut ctx Context, paths string) veb.Result {
name := paths.all_before('/')
infoptr := app.db.infopointers[name] or {
return ctx.not_found()
}

mut path := pathlib.get('${infoptr.path_content}${paths.all_after(name)}')
println('debugzopath ${path}')

logger := log.new('logger.sqlite') or {
return ctx.server_error(err.str())
}
// if infoptr.cat == .website && ctx.req.url.ends_with('.html') {
// logger.new_log(log.Log{
// object: infoptr.name
// timestamp: time.now()
// subject: ctx.user_id.str()
// message: 'access path ${ctx.req.url}'
// event: 'view'
// }) or {
// return ctx.server_error(err.str())
// }
// }
// println('debugzopath ${infoptr}')

file := if path.is_file() {
path
} else {
// mount the folder 'static' at path '/public', the path has to start with '/'
pathlib.get_file(path: '${path.path}/index.html') or {
return ctx.server_error(err.str())
}
}
println('debugzopath2 ${file}')

// ctx.set_content_type('application/pdf')
return ctx.file(file.path)
}
39 changes: 39 additions & 0 deletions crystallib/webserver/heroweb/controller_log.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module heroweb

import json
import veb
import time
import freeflowuniverse.crystallib.core.pathlib
import freeflowuniverse.crystallib.webserver.log

@[POST; '/log/:name']
pub fn (mut app App) log_post(mut ctx Context, name string) veb.Result {
infoptr := app.db.infopointers[name] or {
return ctx.not_found()
}

// new_log := json.decode(log.Log, ctx.req.data) or {
// return ctx.server_error(err.str())
// }
// app.db.new_log(log.Log{...new_log,
// subject: ctx.user_id.str()
// timestamp: time.now()
// }) or {
// return ctx.server_error(err.str())
// }

return ctx.json('ok')
}

@[GET; '/logs']
pub fn (mut app App) logs(mut ctx Context) veb.Result {
// logs := app.db.filter_logs(
// event: ctx.query['event'] or { '' }
// subject: ctx.query['subject'] or { '' }
// object: ctx.query['object'] or { '' }
// ) or {
// return ctx.server_error(err.str())
// }

return ctx.json('logs')
}
73 changes: 16 additions & 57 deletions crystallib/webserver/heroweb/factory.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,14 @@ import freeflowuniverse.crystallib.data.markdownparser
import freeflowuniverse.crystallib.ui.console
import freeflowuniverse.crystallib.clients.mailclient
import freeflowuniverse.crystallib.webserver.auth.jwt
import freeflowuniverse.crystallib.webserver.log
import freeflowuniverse.crystallib.webserver.log {Logger}
import db.sqlite

pub fn (app &App) index(mut ctx Context) veb.Result {
doc := Doc{
navbar: Navbar{
brand: NavItem{
href: "/",
text: "OurWorld",
class_name: "brand"
},
items: [
NavItem{
href: "/about",
text: "About Us",
class_name: "nav-item"
},
NavItem{
href: "/projects",
text: "Projects",
class_name: "nav-item"
},
NavItem{
href: "/contact",
text: "Contact",
class_name: "nav-item"
}
]
},
content: markdownparser.new(path: '${os.dir(@FILE)}/content/index.md') or {
return ctx.server_error(err.str())
}
}
return ctx.html($tmpl('./templates/index.html'))
}

pub fn (app &App) dashboard(mut ctx Context) veb.Result {
user := app.db.users[ctx.user_id]
return ctx.html($tmpl('./templates/dashboard.html'))
}

//the path is pointing to the instructions
pub fn new(path string) !&App {
mut app := &App{
db: WebDB{
logger: log.new(
backend: log.new_backend(
db:
sqlite.connect('${os.dir(@FILE)}/logger.sqlite')!
)!
)!
// Logger: log.new('${os.dir(@FILE)}/logger.sqlite')!
}
}

Expand Down Expand Up @@ -86,7 +43,7 @@ fn (mut app App) mount_middlewares() {

// must be authorized to fetch infopointers and pointed assets
app.route_use('/infopointer/:path...', handler: app.is_authorized)
app.route_use('/assets/:path...', handler: app.is_authorized)
app.route_use('/asset/:path...', handler: app.is_authorized)
}

fn (mut app App) play(path string) ! {
Expand All @@ -100,23 +57,25 @@ fn (mut app App) play(path string) ! {
}

fn (mut app App) run_infopointer_heroscripts() ! {
// QUESTION: we have some .mmd files in our collections, not sure what they do
app.static_mime_types['.mmd'] = 'txt/plain'
for key, ptr in app.db.infopointers{
app.db.infopointer_run(key)!
if ptr.path_content == '' {
continue
}

mut asset := pathlib.get(ptr.path_content)
if !asset.exists() {
return error('Asset ${ptr.name} does not exist on path ${ptr.path_content}')
}
if asset.is_file() {
app.serve_static('/assets/${ptr.name}.${asset.extension()}', asset.path)!
} else if asset.is_dir() {
app.mount_static_folder_at(asset.path, '/assets/${ptr.name}')!
} else {
return error('unsupported path ${asset}')
}
// QUESTION: this was serving files statically. Might there be a case where we nneed to reintroduce this?
// mut asset := pathlib.get(ptr.path_content)
// if !asset.exists() {
// return error('Asset ${ptr.name} does not exist on path ${ptr.path_content}')
// }
// if asset.is_file() {
// app.serve_static('/assets/${ptr.name}.${asset.extension()}', asset.path)!
// } else if asset.is_dir() {
// app.mount_static_folder_at(asset.path, '/assets/${ptr.name}')!
// } else {
// return error('unsupported path ${asset}')
// }
}
}
17 changes: 6 additions & 11 deletions crystallib/webserver/heroweb/middleware.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module heroweb

import freeflowuniverse.crystallib.webserver.auth.jwt
import freeflowuniverse.crystallib.webserver.log
import veb
import time

// middleware to set user id to context from jwt
pub fn (app App) set_user(mut ctx Context) bool {
Expand Down Expand Up @@ -40,16 +42,17 @@ pub fn (app App) is_logged_in(mut ctx Context) bool {

// middleware to check if user is authorized to access an infopointer or asset
pub fn (app App) is_authorized(mut ctx Context) bool {
println('running is authorized')
if !app.is_logged_in(mut ctx) {
return false
}

if ctx.user_id == 0 {
panic('this should never happen')
panic('this should never happen as user must be logged in to reach this stage')
}

infoptr_name := if ctx.req.url.starts_with('/assets/') {
ctx.req.url.all_after('/assets/').all_before('/')
infoptr_name := if ctx.req.url.starts_with('/asset/') {
ctx.req.url.all_after('/asset/').all_before('/')
} else if ctx.req.url.starts_with('/infopointer/') {
ctx.req.url.all_after('/infopointer/').all_before('/')
} else {
Expand All @@ -71,14 +74,6 @@ pub fn (app App) is_authorized(mut ctx Context) bool {
return false
}
{
// app.db.logger.new_log(Log{
// object: infoptr_name
// subject: ctx.user_id.str() or {
// return ctx.server_error(err.str())
// }
// message: 'access path ${app.req.url}'
// event: 'view'
// })
return true
} else {
// ctx.res.set_status(.unauthorized)
Expand Down
3 changes: 2 additions & 1 deletion crystallib/webserver/heroweb/model.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import veb
pub struct App {
veb.StaticHandler
veb.Middleware[Context]
veb.Controller
jwt_secret string = jwt.create_secret()
mut:
db WebDB
Expand All @@ -19,9 +20,9 @@ pub:
}

pub struct WebDB {
// Logger
pub mut:
authenticator StatelessAuthenticator
logger Logger
users map[u16]&User
groups map[string]&Group
acls map[string]&ACL
Expand Down
1 change: 1 addition & 0 deletions crystallib/webserver/heroweb/play_infopointers.v
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn (mut db WebDB) play_infopointers(mut plbook playbook.PlayBook) ! {
'slides' { InfoType.slides }
'pdf' { InfoType.pdf }
'wiki' { InfoType.wiki }
'website' { InfoType.website }
else {
println('Invalid category')
// Handle the error or set a default value
Expand Down
1 change: 1 addition & 0 deletions crystallib/webserver/heroweb/slides.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Slides {
pub:
url string
name string
log_endpoint string
}

pub fn (slides Slides) html() string {
Expand Down
Loading

0 comments on commit 3e6a58b

Please sign in to comment.