
Show HN: Cortris – A Simple Url Shortener written in Go - adalricus
https://adalricus.nand.sh/projects/cortris/
======
dougbarrett
Good job on getting your first project out there, there is a lot to be
improved upon here.

First off, it's standard practice to listen for PORT over the environment
variable. You can always set a default, I often do, but it should able to be
overriden.

Second, you are opening and closing your DB on every request, it may not cause
too much latency, but you should 'open' the database upon application start,
then use a service to allow your controllers access to the DB. If the project
was larger, I would recommend some abstraction so you can use different types
of storage solutions and the controller doesn't need to have any idea what is
used behind the scenes.

errChk will cause the entire application to crash on any 'err' thrown in it
that isn't nil. Errors should be handled a bit more gracefully, a common
pattern is:

    
    
        if err := datastore.Get(c, key, record); err != nil {
            http.Error(w, err.Error(), 500)
            return
        }
    

You are definitely on the right path though! If you have any questions, feel
free to leave a comment. I can create a PR later with the suggested updates if
that would be of any interest to you.

Oh, also Heroku or Google Cloud Run may be of interest to you, both services
have free tiers that you can get started on, but you will want to change your
datasource to use something beyond sqlite.

~~~
adalricus
> it's standard practice to listen for PORT over the environment variable

I didn't think of this, I was thinking of making something like config file
where user can specify it. This will be a lot better than that, so I could ask
users to set CORTRIS_PORT & read that?

edit: I'm now reading port from environment & setting it to 8080 if it doesn't
exist.

> you are opening and closing your DB on every request

I don't understand about controllers part, could I open the db at start & put
db.Close() at end of main (?), is there more I have to think of (like what
happens after program fails & the connection is still open?)

> http.Error(w, err.Error(), 500)

Is it safe to pass the error to the user? Could something go wrong considering
the source is already world readable?

It would be nice if anyone could set it up on heroku free tier, where do I
learn about it?

I'm planning on getting a cheap vps to deploy my little projects. Thank you
for looking at my project!

~~~
adalricus
I've implemented these since then, I also read the database file from
environment now.

------
adalricus
It's nothing fancy, I'm learning how to write programs. This is one of my
first projects. I don't have resources to deploy it on the web, the code is
short & it really doesn't do much. Previously I used cgi for all my projects,
decided to try this reverse-proxy thing this time.

Source Code:
[https://github.com/adalricus/cortris/](https://github.com/adalricus/cortris/)

License: ISC License

Images: linked on the page

