Devember 2022 Project: Golang web overengineered template

So, I really would rather make something thats actually a project, but I am dry of ideas. And I really like to architect and (over) engineer projects well.

Quick precursor

I was never making this for devember just because its not an actual project, just a template which kept groing in complexity. I then decided to actually post it here becuase I have been working on it pretty much full time because I got made redundant from my dev job (dw im fine). So I thought I would just throw it here bc it has gotten out of hand and im pretty much even building a couple of frameworks.

The actual project

There is and will be more info on the github README (I will be making a better write up soon).

This template got started out of the realisation of that html templating does 80% of stuff you need todo when making a website and there is no need for overly complex javascript frameworks (especially as a golang engineer). So I got started by just making a well architected and simple template for setting up a site and some endpoints, database and authentication. So that when I did have an actual useful idea I would be able to use that tempalte to get right into it and not spend too much time engineering and setting up the project.

Aaaaand the scope of this project grew very quickly by trying to setup support for different features, a cli tool to set up the project for you and even a minimal (by design) javascript framework which would work together with the template rendering which would inject each page’s data directly into javascript (I think similar to something like next.js or svelte) so that when the js renders the page does not need to perform another get request for more data.

Whats working right now

  • Database and ORM with gorm
  • Session/Cookie authentication
    • I would like to make a drop in replacement for other standards like JWT and Oauth2 support
  • Package for simple API development
  • Package for Site development
    • Support for a frame
    • Easily create pages with everything a page needs defined in the page file (not including the template itself)
    • Dedicated function to include js in a template
    • Easy preinjected template variables for current auth, data, meta and existing routes
  • Overall easy to read and very extensible design

Features on the horison

  • [ ] Typescirpt support for the teamplate script
  • [ ] Static file serving
  • [ ] MAAAybe a golang wasm server?
  • [ ] Support for easily adding a js framework (React, Vue, Angular, etc…)
    • Maybe see if we can inject the template data into these as well, would need some thinking through

The git repo

Let me know what you think guys. I’m more than happy to take any suggestions.

UPDATE:

So one of the reasons why I started my pseudo (proto) js framework to work in tandem with go’s templating was because I wanted to be able to use js in order todo some DOM manipulation if I wanted to, as there are use cases for js on the web and I wanted to have some simple functions which would work it HTMLElements directly.

However I was not happy because of the lack of type safety.
This might sound like the rambling of someone that had too much meth, but I’ve managed to integrate typescript into all of this and its actually starting to turn into an actual framework?

Pretty much the way it works now is that you would be throwing any .ts files into the templates/ts folder then that would spit out js into the jsdist folder.
On the template side of things you’d use the includeJs function and the path to the transpiled js in the jsdist folder, then the system would inject the compiles js.
In order to make it work with all the js I already have for this proto framework, I’ve made typings in globals.d.ts github link .

My next steps here is to:
One, clean this up a bit. Maybe even add an includeTs template func which would allow you to include the .ts file then have it translate to the jsdist folder automatically.

Two, there is one more problem, the type system is not maintained across the board. When it comes to the templating language it’self, I’m not sure I can do much without using a different language all together… but what I can maybe think of doing is seeing if I can generate typescript types based on golang structs, so I can posibly match the _data passed in to js to an actual type. I could use json tags in the golang structs then use one of the best features that go has to offer reflect.StructTag

Not gunna lie, this makes me feel like a crakhead.


I’ll have to come back to this in a few day’s time and see if I can make a comprehensive doc about all the stuff I have done and maybe fild a vid or something about this.

Update on the Go structs to Ts interfaces saga:

I have spent some time trying to figure out what or even if there are any tools I could use to transpile my go structs to ts structs and found a few git repos for that, some worked but not well enough some didnt, etc.

Finally I found this gem of a tool called tygo.
It basically did everything I needed it todo however, there were a couple of issues.

  1. I had to basically just point to package and it would transpile all of the structs in it automatically
    • only options were to use a file based while/black list
  2. If I am importing any outside the package structs I would not work and just default the TS type to any

For point 2 I have a possible work around…but it might not even be a problem. Will need to dig around a bit more.

For point 1…Well I decided to raise an issue about it, then decided to dig around the codebase myself and open up a PR (my very first PR on a public repo :slight_smile: ) and add a feature where we could use golang comments in order to specify which struct to transpile.
Issue


The more I keep working on this the more complex this is getting lol.