Coder Social home page Coder Social logo

deployment-guide's Introduction

Deployment Guide with Railway

Deploy a .NET 6 Web app with a Postgres database to Railway

❗Disclaimer❗

I've really just finished getting everything working, so this is the solution without any revisions. Probably lots of areas for improvements and less than elegant solutions, but hey it works.

Prerequisites

  1. Railway account
  2. Project is on Github

Lets Get Started

1. After creating and logging into a Railway Account, from your dashboard select new project

NewProject

2. Select Deploy from Github Repo

image

3. Select the Repo containing your application

SelectRepo

4. Inside the new project right click the background and select Database under add new service

image

5. Select Postgres

image

6. Select the Postgres block and select the variables tab

image

7. Copy the following variables, PGHOST, DATABASE_URL, PGPORT, POSTGRES_USER, PGPASSWORD

8. Select the block containing your application and add these variables with the same names and values as in the postgres block. Add an additional variable here with the key PORT and value 3000

image

Application

  1. Create a new class named ConnectionHelper. This will check the environment variables that you set inside railway. If those values are null it can be assumed the application is launching locally and will instead use the local connection string.
    2.❗IMPORTANT❗ Make sure to change the database value in the local connection string to match the name of your database, optionally change the MYCONNECTIONSTRING to your desired name.
public static class ConnectionHelper
{
    public static string getConnectionString()
    {
        string MYCONNECTIONSTRING = "";
        if (Environment.GetEnvironmentVariable("PGHOST") != null)
        {
            MYCONNECTIONSTRING = 
                $"Server={Environment.GetEnvironmentVariable("PGHOST")};" +
                $"Database={Environment.GetEnvironmentVariable("DATABASE_URL")};" +
                $"Port={Environment.GetEnvironmentVariable("PGPORT")};" +
                $"Username={Environment.GetEnvironmentVariable("POSTGRES_USER")};" +
                $"Password={Environment.GetEnvironmentVariable("PGPASSWORD")}";
        }
        else
        {
            MYCONNECTIONSTRING = "Server=localhost;Database=MYDATABASE;Port=5432;Username=postgres;Password=password123";
        }
        return MYCONNECTIONSTRING;
    }
}
  1. Alter the following line in program.cs to match the following, changing MYDBCONTEXT and MYDBNOTFOUND to match your existing names
builder.Services.AddDbContext<MYDBCONTEXT>(
    options =>
        options
            .UseNpgsql(ConnectionHelper.getConnectionString()
                    ?? throw new InvalidOperationException(
                            "Connection String 'MYDBNOTFOUND' not found"
                            )
                    )
                    .UseSnakeCaseNamingConvention()
                    );
  1. Below the line var app = builder.Build(); in program.cs Add the following block of code, this is the programmatic version of update-database and so ensures our remote DB has our migrations. Again changing MYDBCONTEXT to match your existing one.
using (var scope = app.Services.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<MYDBCONTEXT>();
    // This will throw an exception if the connection fails
    dbContext.Database.Migrate();
}

Connecting to the Remote DB in pgAdmin

  1. Right click servers, go to register, and finally server... image
  2. Inside this window give the server a name (doesn't matter what it is) image
  3. Head to the connection tab Here we will be using values from the variables tab from Railway
  • Host name/address = PGHOST
  • PORT = PGPORT
  • Username = PGUSER
  • Password = PGPASSWORD After this, hit save. image
  1. This is the connection you are looking for
    image

  2. You are finished and can now write your queries in here!

Common Issues

  1. CSS not showing
    Make this adjustment in your program.cs file, on your builder.
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
    Args = args,
    ContentRootPath = "/app/out",
    WebRootPath = "wwwroot",
});

deployment-guide's People

Contributors

jeremy-kimball avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.