HappyX
Web framework written in Nim with ❤
Why HappyX? 🤔
HappyX is macro-oriented full-stack web framework that provides same syntax in both parts of framework (SPA and SSR).
- SPA part compiles only on JS and provides these features:
- components;
- event handlers;
- buildJs macro;
- state management
- SSR part compiles only on C/Cpp/Obj-C and provides these features:
- CORS registration;
- Request models;
SPA and SSR also provides both syntax for routing. You don't need to learn new syntax for new projects.
HappyX provides CLI tool for creating and serving your projects 🛠
Create new project ✨
hpx create
Serve existing SPA project 🛠
hpx dev
Help ❔
hpx help [subcommand]
Examples
SPA 🎴
SPA works only on JS backend
- index.html
<html> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <div id="app"></div> <script src="main.js"></script> </body> </html>
- components/hello_world.nim
import happyx component HelloWorld: `template`: # Use HTML here tDiv: "Hello, world!" `script`: # Use real Nim code here discard `style`: """ /* Use pure CSS here */ div { color: green; } """
- main.nim
import happyx, components/[hello_world] appRoutes "app": "/": component HelloWorld
SSR 💻
SSR works only on C/Cpp/ObjC backends
- main.nim
import happyx serve "127.0.0.1", 5000: # In this scope you can declare gc-safe vars/lets var myVar = 0 get "/": # available only on GET method # In this scope you can access to # req: Request - current request # query: StringTableRef - current queries # path: string - current path myVar += 1 return "Hello, world! myVar is {myVar}" "/framework": return "This method available from any method (POST, GET, PUT, etc.)" middleware: echo "This will printed first" notfound: return "Oops, not found!"
CLI Usage 🎈
SSR
At first we need to create project and choose SSR project type.
hpx create
This creates directory with .gitignore, README.md and main.nim files
After creating you can work with project as you want
SPA
At first we need to create project and choose SPA project type.
hpx create
This creates directory with .gitignore, README.md, main.nim and components/hello_world.nim files
After creating you can work with project as you want
cd PROJECT_NAME hpx dev
hpx dev command will see all changes in your project and recompile main.nim
hpx build command will builds your project as standalone web application (HTML + JS files)
Path Params 🛠
Routing provides powerful path params.
Example
"/user{id:int}": # In this scope you can use `id` as assigned immutable variable ... "/user{username}": # In this scope you can use `username` as assigned immutable variable ...
Validation ⚙
In path params you can describe every param if you need. Here is syntax overview.
- Required param: {arg:type}, $arg:type, {arg}, $arg
- Optional param: {arg?:type}, $arg?:type, {arg?}, $arg?
- Optional param with default value: {arg:type=val}, $arg:type=val, {arg=val}, $arg=val
Aliases 🎈
Path params can be used by default in curly brackets: {arg} But you can use syntax sugar (alias) also: $arg
Typing 👮♀️
Every path param keeps type (default is string)
List of types:
- bool: can be on, 1, yes, true, y for true and off, 0, n, no and false for false
- string: string that excludes / chars
- enum(EnumTypeName): matches enum value
- word: like string but excludes any symbols
- int: any integer
- float: any float
- path: like string but includes / chars.
Immutable | Immutable Via Alias |
---|---|
{arg} | $arg |
{arg:type} | $arg:type |
{arg:type=default} | $arg:type=default |
{arg=default} | $arg=default |
{arg?:type} | $arg?:type |
Mounting 🔌
HappyX routing provides mounting also.
Here is example of mount declaration ✨
mount Settings: "/": ... mount Profile: mount "/settings" -> Settings mount "/config" -> Settings
Here is example of mount usage 🎈
serve(...): # or appRoutes 🍍 # /profile does not works # /profile/settings does not works # /profile/settings/ works! mount "/profile" -> Profile
API Reference 📄
Automatic Import 🎈
Core 🔋
- constants - describes all HappyX flags and consts.
- queries - provides some utils to work with query parameters.
- exceptions - describes all HappyX exceptions.
- secure - provides some secure features.
Single Page Application ✨
- renderer provides SPA renderer.
- components provides working with components.
- translatable provides working with translatable strings.
- state provides reactivity.
- spa_utils provides JS sugar for SPA.
- tag provides VDOM.
Server Side Rendering 🍍
- cors provides CORS registration.
- server provides routing and working with server.
- form data provides routing and working with server.
- request models provides routing and working with server.
- session provides working with sessions.
- open api provides OpenAPI for HappyX.
- utils provides some utils to work with HTTPHeaders, JSON and etc.
Template Engine 🎴
- engine provides templates for SSR.
Routing 🔌
- routing provides powerful routing and pathParams macro.
- mounting provides powerful mounting.
- decorators provides powerful decorators.
Syntax Sugar ✨
- use provides use macro.
- style provides buildStyle macro.
- sgr provides -> macro.
- js provides buildJs macro.
Language Binds ✌
Python 🐍
- python main module for Python HappyX.
- python_types Python types.
Imports
-
happyx/ssr/server, happyx/ssr/request_models, happyx/ssr/form_data, happyx/ssr/cors, happyx/ssr/session, happyx/core/secure, happyx/core/exceptions, happyx/core/constants, happyx/sugar/use, happyx/sugar/sgr, happyx/sugar/js, happyx/sugar/style, happyx/spa/renderer, happyx/spa/state, happyx/spa/translatable, happyx/spa/tag, happyx/routing/mounting, happyx/routing/decorators, happyx/ssr/utils, happyx/tmpl_engine/engine, happyx/routing/routing, happyx/core/queries, happyx/spa/components, happyx/ssr/docs/open_api
Exports
-
unsafeSend, runAsync, headers, parseHeaders, Settings, answerHtml, Request, parseHttpMethod, path, runAsync, start, answerFile, initServer, parsePath, liveview, send, newServer, newApiDocObject, send, openApiDocs, serve, run, pathWithQueries, routes, handleLiveViews, closed, liveViewsCache, ModelBase, ., ip, httpxClientBufDefaultSize, useWss, liveViewScript, send, body, sseSend, parsePathWithQueries, run, ApiDocObject, fetchModelFields, parseContentLength, fetchPathParams, genApiDoc, newWebSocket, send, HttpxDefect, procApiDocs, answer, httpxClientBufSize, happyxDocs, liveviewWsHost, Server, httpMethod, send, OnRequest, answerJson, send, answer, initSettings, forget, modelFields, modelFieldsGenerics, modelImplementationUntyped, builtinTypes, modelImplementation, model, parseFormData, parseXmlBody, parseXWwwFormUrlencoded, FormDataItem, addCORSHeaders, CORSObj, regCORS, corsRegistered, startSession, Session, closeSession, startSession, closeSession, genSessionId, getSession, generate_password, check_password, compDefArg, importFuncComponent, reRenderTmpl, scopeCss, importComponent, component, HpxModelSyntaxDefect, throwDefect, HpxServeRouteDefect, HpxBuildStyleDefect, HpxPathParamDefect, HpxUseDefect, HpxMountDefect, HpxSyntaxSugarDefect, HpxBuildJsDefect, HpxCorsDefect, HpxBuildHtmlDefect, HpxTranslatableDefect, HpxAppRouteDefect, HpxComponentDefect, appName, enableAppRouting, enableUseCompDebugMacro, htmlNonBoolAttrs, httpMethods, componentDebugTarget, HpxMajor, enableDebug, sessionIdLength, enableColors, enableHttpx, apiDocsPath, HpxPatch, enableStd, cryptoMethod, enableDebugComponentMacro, exportPython, enableDefaultDecorators, enableRoutingDebugMacro, enableSafeRequests, numThreads, enableTemplateEngine, HpxVersion, enableMicro, enableDebugTreeComponentMacro, reqModelDebugTarget, enableLiveViews, enableBuiltin, enableHttpBeast, exportJvm, HpxMinor, enableAutoTranslate, enableRequestModelDebugMacro, enableApiDoc, enableDebugSpaMacro, enableCachedRoutes, enableHistoryApi, enableDefaultComponents, nim_1_6_14, htmlTagsList, enableDebugSsrMacro, nim_2_0_0, use, componentEventHandlers, registerApp, lazyTag, components, createdComponentsList, addArg, ugly, buildHtmlSlot, application, route, tag, renderVdom, tag, routes, BaseComponent, injectJs, lvl, rerenderWithComponent, findTagsAtTop, [], []=, eventHandlers, App, initTag, initTag, registerComponent, ComponentEventHandler, getAttribute, componentsResult, addArgIter, requestResult, UnclosedTags, textTag, initTag, js, buildComponentHtml, frwrd, [], js, get, liveviewRoutes, NimKeywords, SvgElements, currentComponent, currentRoute, buildHtmls, lazyHtml, $, AppEventHandler, scopedCycleCounter, tagFromString, buildHtml, BaseComponentObj, appRoutes, findByTag, reRender, buildHtml, rerender, route, currentComponentsList, render, TagRef, toSeqIter, lazyHtmls, xmlTree2Tag, bck, rf, start, initTag, add, rendererHandlers, xml2Tag, /, []=, %, /, <, toFloat, @=, toInt64, StateChangeHandler, >=, ==, ~=, /=, ^, low, [], /, $, @, <, +=, State, -, >, *=, @=, !, toInt8, ^=, -=, *=, %=, !=, /=, &, remember, :=, $=, <=, toString, %=, ==, !, []=, :=, *, watchImpl, &, get, @, *, -=, %=, /=, |=, toInt, val=, not, toInt32, &=, enableRouting, toCString, ^, set, +, $=, [], %, <=, !, languageSettings, toBool, [], +=, !=, watch, @, >=, set, -=, &=, %, toChar, +, +=, &=, pairs, toFloat32, toSeq, []=, |=, ~=, -, []=, <, items, ^, >, toFloat64, >, *=, &, :=, ->, len, ^=, |=, +, @=, high, ~=, [], -, $=, toInt16, *, ^=, [], [], cachedRoutes, CachedRoute, decorators, decorator, rateLimits, RateLimitInfo, regDecorator, CachedResult, DecoratorImpl, LanguageSettings, translate, i18n, translatable, nnkNumbers, buildStyle, initTag, tag, textTag, SvgElements, addArgIter, initTag, TagRef, tag, toSeqIter, xml2Tag, NimKeywords, xmlTree2Tag, ugly, []=, $, findTagsAtTop, add, initTag, get, getAttribute, lvl, UnclosedTags, findByTag, initTag, lazyTag, tagFromString, [], addArg, [], mount, findAndReplaceMount, registeredMounts, sugarRoutes, ->, has, has, :=, buildJs, toHttpHeaders, fgColored, CustomHeaders, newCustomHeaders, []=, toJsonNode, toHttpHeaders, declaredPathParams, enumerate, parseBoolOrJString, scanf, getRouteParams, kind2scanable, findParams, parseIntOrJString, str, default, PathParamObj, newPathParamObj, word, findModels, onException, boolean, pathParams, RouteObject, kind, RequestModelObj, handleRoute, convertJson, newRequestModelObj, path, kind2tp, parseFloatOrJString, exportRouteArgs, RouteDataObj, integer, realnum, parseQueryArrays, ?, parseQuery, render, renderTemplate, templateFolder