core/Shared Tsujikiri core
Shared Tsujikiri core is placed at the root of your project so any apps can make use of it
One copy of the framework core is shared by every application during development and compilation, an idea carried over from phpCloud. Its Go code is compiled into each executable, so it does not need to be deployed to production servers; a change to shared Go code means rebuilding the applications that use it. Shared runtime resources, such as configuration and language packs, can remain external.
lib/Shared platform library
Your own shared business logic, certificates, language packs, plugins, etc... folders structure:
/Certificates
/Config
/EmailTemplates
/Entities
/Helpers
/Locales
/Plugins
/Types
/Validations
Platform-wide settings and resources that every application on the server shares like the platform configuration and the language packs.
lib/Locales/en.arbEnglish language pack
{
"@@locale": "en",
"site_title": "Tsujikiri",
"welcome_back": "Welcome back, {name}",
"items": "{count, plural, =0{No items} one{# item} other{# items}}"
}
One file per language, keyed the same way in every file. ±i welcome_back|name:Tony i± picks the text for the request's language and fills in the name.
lib/Locales/es.arbSpanish language pack
{
"@@locale": "es",
"site_title": "Tsujikiri",
"welcome_back": "Bienvenido de nuevo, {name}",
"items": "{count, plural, =0{Sin artículos} one{# artículo} other{# artículos}}"
}
One file per language, keyed the same way in every file. ±i welcome_back|name:Tony i± picks the text for the request's language and fills in the name.
lib/Locales/ja.arbJapanese language pack
{
"@@locale": "ja",
"site_title": "Tsujikiri",
"welcome_back": "おかえりなさい、{name}さん",
"items": "{count, plural, =0{アイテムなし} other{アイテム#件}}"
}
One file per language, keyed the same way in every file. ±i welcome_back|name:Tony i± picks the text for the request's language and fills in the name.
lib/Locales/ar.arbArabic language pack
{
"@@locale": "ar",
"site_title": "Tsujikiri",
"welcome_back": "مرحبًا، {name}!",
"items": "{count, plural, =0{لا عناصر} one{عنصر واحد} two{عنصران} few{# عناصر} many{# عنصرًا} other{# عنصر}}"
}
One file per language, keyed the same way in every file. ±i welcome_back|name:Tony i± picks the text for the request's language and fills in the name.
my_app/Config/app.okiteApp configuration
################################################################################################################################
# APP BASIC CONFIGURATIONS
################################################################################################################################
# Set up Tsujikiri paths
#
core_path|string|required = "/var/www/core"
lib_path|string|required = "/var/www/lib"
app_path|string|required = "/var/www/my_app"
# Set up basic required configurations for the app
#
app_name|string|required = "MY_APP"
app_domain|string|optional:"" = "my_app.tsujikiri.local"
app_url|string|optional:"" = "http://my_app.tsujikiri.local"
# Set up ports, SSL certificate ( if needed ) and server settings
#
[ports]
admin|int|optional:0 = 0
http|int|optional:80 = 80
https|int|optional:443 = 0
[/ports]
...
Everything the application needs, declared with its type, whether it is required, and its default. Okite validates it before the application starts; services are named here and connected at initialization.
my_app/main.goApp initialization
//go:generate go run ../core/Generator -require=auth -allow=auth:public,user
func main() {
// Load local App Configuration into "lib" so it can be used everywhere from now on
if err := core.Tsujikiri.LoadConfiguration("."); err != nil { panic(err) }
// Initialize Sentry ( if enabled ) and flush on shutdown
defer core.Tsujikiri.InitializeSentry()()
// Set up Services that are required by this app
core.Tsujikiri.InitializeServices(Config.App.Strings("required_services"))
// Continue application initialization.
...
} // END: main()
The whole startup in a few calls, always in the same order: configuration, error reporting, then services. Endpoints and decorators are already compiled in. The package-level //go:generate directive tells go generate how to build the endpoint registry: this example requires every endpoint to declare an auth policy and accepts public and user. Those are this application’s requirements, not framework-wide authentication modes.
my_app/Controllers/DefaultController.goControllers & automatic routing
// Examples of entry points: language / controller / method
//
// /en/default/welcome → DefaultController · Welcome() · English
// /es/default/welcome → DefaultController · Welcome() · Spanish
// /default/welcome → DefaultController · Welcome() · Localized URLs not enabled on app.okite
//
// Is using the public decorator "@auth:public" as no user sign-in required for this endpoint
// @auth:public
func (c *DefaultController) Welcome(w http.ResponseWriter, r *http.Request) CoreTypes.RenderedView {
RenderingSettings := core.View.DefaultRenderingSettings(c.Controller)
RenderingSettings.Params["page_title"] = "Hello world example"
RenderingSettings.Params["name"] = "Tony"
return core.View.HTML(RenderingSettings)
}
Controllers and their endpoint methods are discovered during code generation. URLs resolve to compiled endpoint registrations through predictable naming conventions, without manually maintained route tables or runtime reflection. The URL names the language, the controller and the method; the language segment is optional.
my_app/Controllers/Generated.goGenerated endpoint registrygenerated
// Code generated by Tsujikiri. DO NOT EDIT.
package controller
import (
"net/http"
CoreTypes "tsujikiri.org/core/Types"
)
// Endpoints is generated for the active build target; treat it as read-only.
var Endpoints = map[string]CoreTypes.Endpoint{
"DefaultController.Welcome": {
Factory: func() CoreTypes.ControllerInterface {
return &DefaultController{}
},
Execute: func(
instance CoreTypes.ControllerInterface,
w http.ResponseWriter,
r *http.Request,
) CoreTypes.RenderedView {
var method func(
*DefaultController,
http.ResponseWriter,
*http.Request,
) CoreTypes.RenderedView = (*DefaultController).Welcome
return method(instance.(*DefaultController), w, r)
},
Decorators: map[string]string{
"auth": "public",
},
},
}
Generated automatically before compilation. Controller factories, endpoint execution and decorator metadata are represented as native Go code; the explicitly typed method expression lets the Go compiler enforce the expected endpoint signature. Developers do not maintain this file manually, and the original source files are not required in production.
my_app/Decorators.goDecorators
// Handles authentication decorators like @auth:user
//
func HandleAuthDecorator(ctx *CoreTypes.DecoratorContext, value string) bool {
switch value {
case "public":
return true // Can continue
case "user":
if core.Session.Authenticated(ctx.SessionKey) {
// Inject session + user into the controller
session := core.Session.Get(ctx.SessionKey)
ctx.Session = session
ctx.User = session["user"].(map[string]interface{})
return true // Can continue
} else {
if Config.App.Bool("localized_urls") {
encoded := base64.RawURLEncoding.EncodeToString([]byte("/" + ctx.Language + ctx.Slug))
http.Redirect(ctx.W, ctx.R, "/"+ctx.Language+Config.App.String("login_page")+"?redirect="+encoded, http.StatusSeeOther)
} else {
encoded := base64.RawURLEncoding.EncodeToString([]byte(ctx.Slug))
http.Redirect(ctx.W, ctx.R, Config.App.String("login_page")+"?redirect="+encoded, http.StatusSeeOther)
}
return false // Needs login
}
default:
// Unrecognized authentication value: deny
http.Error(ctx.W, "Forbidden", http.StatusForbidden)
return false
}
} // END: HandleAuthDecorator()
Behaviour such as authentication is declared on the controller method itself. Decorator declarations are processed and validated before compilation. At runtime, their compiled metadata is used to execute the application's decorator handlers; this handler denies any value it does not recognize.
my_app/Layouts/default.htmlLayout
<!DOCTYPE html>
<html ±v locale_settings v± data-theme="dark">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Tony Roman" />
<meta name="generator" content="Tsujikiri Framework" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
±v scripts v±
<title>±v page_title v±</title>
</head>
<body data-theme="dark">
±c header c±
<!-- View START -->
<div id="view_contents">
±v view_contents v±
</div>
<!-- View END -->
</body>
</html>
The frame shared by many pages. The current view is placed into view_contents; components and translations are resolved in place.
my_app/Views/Default/Welcome.htmlView
<h1>±i welcome_back|name:±v name v± i±</h1>
The content of one page. Variables, loops, conditions, formatting filters and translations, all in the same syntax, also used for email templates.
my_app/Components/header.htmlReusable component
<header class="site-header">
<div>Hello world app example</div>
</header>
A fragment written once and included anywhere with ±c header c±: in layouts, in views or in other components.
my_app/Static/Static resources
// Add here public static resources for the web app; folders structure
/Static/favicon.ico
/Static/css
/Static/fonts
/Static/images
/Static/js
/Static/** whatever you may need **
Stylesheets, scripts and images are indexed once at startup, so serving them requires no filesystem lookup on each request.