Showing posts with label GitHub. Show all posts
Showing posts with label GitHub. Show all posts

Tuesday, October 12, 2021

GitHub template project for .Net console Menu driven applications having DI and Logging capabilities

This is simple introduction to one of the new open source projects that I started in GitHub. It is not solving any planet scale problems but a simple problem of setting up a menu driven console application. Currently  every time when I start a PoC (Proof of Concept) application I start as blank console application then add the required infrastructure libraries. Those libraries include the logging, menu etc...Also a dependency injection framework. 

It is a GitHub project template and below is the link that has the DI, Logging and Menu (using EasyConsoleStd). Let me know if there are better starter templates to use.
 
Note that this is just a GitHub project template, not released as dotnet command extension. In other words if this is made that way, anyone can easily use with below command 

dotnet new <my template name>
Converting this as dotnet template is in consideration. But mean time if I get a better template why should I spend time?

Why Dependency Injection?

All the time, we start PoC as temporary arrangement to demo technology capabilities or feasibility of a solution. If its for a small project and deployed in to predefined environments, its fine. But think about the application needs to be installed in various departments or business units of a big enterprise with different environment configurations. Then our application may fail and we need to prove that the environment doesn't have the capabilities by using this PoC application. In such scenarios The PoC lives for months and on most of the environment failure, we may discover one more additional scenarios to be tested that cause changes to the PoC. As we all know the DI frameworks helps to do changes easily.

One more scenario depends on the dev team. We (from architecture or research or sales team) create a PoC and hand over to the dev team on the expectation that they know that the code we handed over is PoC grade. Never copy paste or extend that PoC to production. But sometimes it doesn't happen that way. So better do the PoC with DI so that even if dev team extend it, there will be minimum quality.

Why develop PoC as Menu driven application ? Don't you know about test projects

The scenario mentioned above about the variety of environments is the answer. If we want to run only on dev machine, its fine to do as test project having each scenario as test method. But its difficult to run a test project in production environment to debug something in isolation. 
eg: We are able to enqueue messages to ServiceBus in dev environment but in production it failed because some device in production network doesn't like AMQP traffic.

Another psychological reason is that demoing a console app with menu options gives an impression that something complex is going on. If we run tests from Visual Studio the same impact will not be there.

Why menu in the console app? Can't you use the command line switches?

Same psychological reason. The reason for PoC is that something that we are proving that we don't know. It may require days to get a PoC working. After this much time, if we just run a command line application with different arguments in a demo, there is a high chance that the stakeholders / managers think the developer is lazy. Mean time if we show a menu in the PoC with 10-15 options, it give a different impression. Coding efforts is more or less same.

I feels like I am becoming more of a sales demo guy. So stopping here. Let me know your comments.

Tuesday, February 9, 2021

Dev environment in GitHub (Not codespaces)

After scripting my dev machine setup, naturally, my next aim became scripting specific application environments. Below are some reasons

Why we need to script out per application environment?

  • Different applications require different SDK versions, prerequisites. Making sure all apps are using the same version is time-consuming. 
  • Setting up the environment for one app to run may make another app fail.

The obvious answer I came to is containerization. The next question is where to run the container. It is GitHub codespaces. But it requires an invitation or we would be in the queue. So the next place is my own machine.

Solution

Finally ended up following multiple blog posts mentioned in references and reached to get it working. First I made my Hugo based personal website containerized. Below is the link to 2 files that are required to containerize.

https://github.com/joymon/site-hugo/tree/master/.devcontainer

One easy thing now in VS Code is an extension called "Remote Development" that packs multiple extensions required to run dev containers.

Hot reload / live-reload issue when using WSL2 backed containers in Windows

The problem I faced with this is the hot reload/live-reload is not working by just following the tutorials. This seems a known issue. One of the answers I have seen is to keep the file also in the Linux system. Another workaround people are suggesting is to use WSL1. 2 more are there. One to increase "fs.inotify.max_user_watches" property and the last one to use polling instead of subscribing to events to do hot reload.

More details can be found in the official documentation below.

https://docs.docker.com/docker-for-windows/wsl/#best-practices

https://docs.microsoft.com/en-us/windows/wsl/compare-versions#performance-across-os-file-systems

I am yet to test all those workarounds.

References

https://www.freecodecamp.org/news/put-your-dev-env-in-github/
https://markmcgookin.com/2020/11/12/vscode-devcontainer-using-dotnet5-0-net-5/

Tuesday, January 28, 2020

Enabled HTTP(s) on joymononline.in without any certificate cost

Some people still argue that encrypting web traffic is costly and reduce performance. But most of us agree that the site should be secured when comparing the performance cost against security. If there are still questions why a website should be secured, please visit the links

https://developers.google.com/web/fundamentals/security/encrypt-in-transit/why-https
https://www.forbes.com/sites/forbestechcouncil/2018/05/18/why-an-ssl-certificate-is-important-for-your-company-website/#4fffd6c21dc3
https://kinsta.com/knowledgebase/how-ssl-works/

Earlier it was an additional cost to protect web site using an SSL certificate. But nowadays there are a lot of ways to get encryption without extra cost.

Difference ways for free SSL on GitHub pages

Before GitHub started officially supporting SSL on custom domains it was little complicated. One way was using other services such as Cloudflare. But the problem is that it adds one more component and a point of failure. So it is better to go with the GitHub approach itself.

My personal website joymononline.in is hosted in GitHub pages. So no cost for hosting. Only the domain registration needs money and it's done through GoDaddy. So both GitHub and the GoDaddy accounts have the changes to serve web site with HTTP. There is a separate post on how to serve a web site hosted in GitHub pages via a domain registered in GoDaddy.

Let's see how the HTTP(s) can be integrated into the mix.

Changes in GoDaddy

Updates to A records

Below are the existing records


The new IP addresses to point to new IPs

Changes in GitHub

No specific change required. Once the new DNS records are propagated we can see the site using https://joymononline.in address.
Once the site starts working, we may enforce HTTP(s) from GitHub so that even the HTTP traffic will be forced to use HTTP(s).

Troubleshooting

How bought my certificate?

If we have experience in setting up SSL earlier, this is an obvious question. Somebody had to buy or obtain the SSL certificate from an SSL authority. Here the certificate authority is Let's Encrypt.
GitHub did it for us.

Checking the IP address

In Windows, we can use the below command to find the latest IP address


More on free SSL

https://letsencrypt.org/getting-started/
https://www.sslforfree.com
https://www.udemy.com/free-ssl-certificate/

References

https://github.blog/2018-05-01-github-pages-custom-domains-https/
https://help.github.com/en/github/working-with-github-pages/securing-your-github-pages-site-with-https

Tuesday, October 30, 2018

Travis-CI - Continuous deployment to staging but production deployment only on tag commit

Background

Travis-CI is a hosted Continuous Integration & Continuous Delivery tool. Based on configurations, users can have their application deployed to production. They provide free service to open source projects. The CI&CD environment currently using Linux.

Aim

  • Have the CI & CD pipeline trigger for each and every commit going into GitHub. Deploy only to staging or testing area.
  • Testing / staging area is GitHub pages hosted in the same repo. Deploy to staging should not trigger another build which ends up as infinite loop. Not into gh-pages branch but to \docs folder.
  • When there is tagged commit deploy to production.

How its done

Avoid build on commit from Travis-CI bot

The word bot is used to indicate the CI&CD account used by Travis-CI. We can see that account on the commits from Travis-CI. When the bot commits, it uses message as follows.

""Deploy <github userid>/<project> to github.com/<github userid>/<project>.git:master""

One such example given below.

"Deploy joymon/prayerbook to github.com/joymon/prayerbook.git:master"

We need to write build conditions at the top of .travis.yml file to bye pass the build if the commit message is matching.

Build and deploy on commit tag

This is another condition we need to put into condition. If we don't put this Travis-CI will not build on tag commit. Below is one example.

if: tag IS present OR commit_message != "Deploy joymon/prayerbook to github.com/joymon/prayerbook.git:master" OR tag = true

If we use tag = true alone it may not work. Needs more analysis on the Travis-CI yml schema.

A sample travis.yml file can be found below which works based on the above conditions.

https://github.com/joymon/prayerbook/blob/master/.travis.yml

References

https://stackoverflow.com/questions/18216991/create-a-tag-in-github-repository
https://docs.travis-ci.com/user/conditions-v1
https://github.com/travis-ci/travis-ci/issues/8518

Disclaimer

The author is not working for Travis-CI or any of their subsidiaries or partners. There is no guarantee that the technique mentioned in this post will work forever. Travis-CI may change their system the way they want it to be. 
This is not at a sponsored post.

Tuesday, August 28, 2018

Another .Net helper library via nuget package system

Background

Over the past 13 years, to be precisely from Nov 2005 till today, I had written lot of .Net code for day job as well as to personal projects. When I started .Net, I though yes I will master it and enjoy rest of my career. But I soon realized that is not going to work with the collapse of Silverlight. Microsoft was telling or people was arguing that Silverlight will not die as Microsoft is using it for their Azure portal. All of a sudden MSFT replaced Azure Silverlight site with HTML and that was kind of last nail on Silverlight. More details on it can be found in my last post in SilverlightedWeb blog which is a readonly blog now.  Then I thought Silverlight technology's end is inevitable as it is replaced by HTML5 but .Net will live long.

That thought got shaken when MSFT released their so called code editor now becoming full fledged IDE named VS Code. That didn't use WPF which was the star of desktop programming from MSFT at that time. Instead it used Electron from Github which depends on Chrome. Yes, the browser from Google powering web. Essentially we develop browser application and show as standalone executable. That was the time I said good bye to WPF technology. More details here. Then whats left, only ASP.Net which was and still struggling to compete with NodeJS. Don't bring Windows Phone here as that is the one of very hand countable things MSFT properly shut down. No idea how long something called UWP will live.

.Net Core

Finally something came named .Net Core. Its like Angular 1.x and 2. Only name is same, internally all most new. That is what now MSFT fans betting on, as return of .Net. .It is advertised as another true cross platform which will run on Linux! Yes its the second cross platform .Net. The original one also advertised as cross platform with the intermediate language and JIT similar to JVM echo system.

The another factor is performance. .Net Core is expected to beat NodeJS for serving http responses. There are some case studies people are claiming it is faster such as on Bing.

Another areas where .Net was weak is AI, Machine Learning, distributed computing etc... Now there is ML.Net SDK also announced.

Yes it may be faster and may become powerful than Python in AI programming.  But will this technology enough to feed my family in future?

So what is next?

Personally I don't see a bright future for .Net unless .Net Core becomes a big hit. So better to reduce focus on .Net and consider other technologies as well seriously. Electron for desktop development, Angular + NodeJS for web front end, Scala for distributed programming etc...

But what I should do with all my .Net knowledge acquired in past 13 years as I still have hope on huge return of .Net core?

Offload from brain and move on. The better place to offload code level techniques is a nuget package at this point. I could have added the helper classes to my first nuget package, but unfortunately that was towards a specific problem of Orchestration. Hence I had to start another nuget library for my helper classes and coding techniques. Link to Github repo below.

https://github.com/joymon/dotnet-helpers

This library uses multi targeting feature so that one code base can be compiled to multiple targets. This is useful especially to provide libraries for .Net Core.

Thanks to AppVeyor for giving free CI&CD support to publish to nuget repo.

Why I didn't join with other helper nuget libraries is described in the readme of the repo.

Tuesday, April 18, 2017

Serverless JoymonOnline.in - Dealing with GitHub API and its limits

Introduction

This is continuation of my Serverless journey with my personal site www.Joymononline.in. As mentioned in previous posts, the main motive behind going Serverless is the cost. The principle is, 'Spend only for domain name. Nothing else'. Now the site is completely Serverless. But like anything else Serverless is not the perfect solution which can be applied everywhere.

One of the draw backs of Serverless is the lack of options to store developer keys at client side. In Serverless, we don't maintain any server but utilizes many third party services. Some services will be having limits on anonymous usage and some doesn't work at all without authentication. It can be overcome only by adding our own server side component(service) which acts as proxy between our client and third party service. Our server side component can have the developer key or API secret and utilize high quota from the third party service.

GitHub API limits

The open source projects from GitHub are listed in the site with some details. For showing that, GitHub API is consumed from the client side. But the GitHub API has limits. At the time of writing this post, if the request is not authenticated, it limits 60 requests per IP per hour. When authenticated, limit is 5000..Ideally JoymonOnline.in will never need that much requests for single user. But below are the situations where the API limits matter
  • The site is opened from corporate machines where the external IP is same and simultaneously more than 60 people from a company opens.
  • The user has opened another site which does heavy GitHub anonymous activity and after that opens JoymonOnline.in.
The first scenario may be rare as the my site at this point doesn't have that much reach. But the second is valid. So this issue is something to be addressed.

Overcome free GitHub API limits using proxy service

As mentioned above the way to overcome this is to place our own service in middle which has the developer key and let that service call GitHub API. The best way to create such a service is to use any  Function as a Service offering. There are many such as Azure Functions, Amazon Lambda, Google Functions, Webtasks etc... The selection criteria is based on cost ie it should be free (not even 1$ setup free) and ability to allow outbound calls to GitHub. Below code shows how the service can be written for Webtask. Why Webtask is selected will be coming as another post.

const http=require('https');
var express    = require('express');
var Webtask    = require('webtask-tools');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());

app.get('/', function (req, res) {
  res.json({ message: 'Welcome to my api!.Currently it supports api/joymon/<name of github repo>. Not open for all repos ' });
});
app.get('/joymon/:projectName', function (req, res) {
  console.log(req.params);
  var  callback = function(response) {
    var str = '';
    response.on('data', function (chunk) {
          str += chunk;
    });
    response.on('end', function () {
        res.json(JSON.parse(str));
        res.end(str);
    });
  };
  
  http.request(getOptions(req.params.projectName),callback)
    .on('error',function(err){
      console.log(err);
      res.json({ message: 'Soemthing went wrong when contacting GitHub API.' });
    })
    .end();
});
module.exports = Webtask.fromExpress(app);

function getOptions(projectName) {
  return {
        method:'GET',
        host:'api.github.com',
        path:'/repos/joymon/'+projectName,
        uri:'https://api.github.com/repos/joymon/'+projectName,
        json:true,
        headers: {'User-Agent': 'Custom Header Demo works'} //Add dev key via header
    };
}

If we have Webtask account, we can simply copy paste the above code and enjoy free service.

Client side changes

The GitHub API calls return headers which talks about quota and usage. The client side has to check that and call the proxy service accordingly. It is kind of retry which was never needed prior to Serverless.

http://jsfiddle.net/joygeorgek/arp6bmrj/5/

Overall Serverless brings benefits but at the same time, we have to do more such as retry and service orchestration. Also it brings so many points of failure and has to design for it. If one third party service is down we should continue giving the other features as normal.

Tuesday, March 21, 2017

GoDaddy to use Github pages

Recently I setup my personal site www.joymononline.in to point to GitHub pages. The domain registration is still with GoDaddy but the hosting space is no more required from GoDaddy. Infact that was eating good amount of cost involved in maintaining the site.

How to configure GoDaddy to use GitHub pages is already a solved problem. There are many sites which explains the same. Below are some.

http://captainwhippet.com/blog/2014/05/11/blog-setup-details.html
http://mycyberuniverse.com/web/configuring-a-godaddy-domain-name-with-github-pages.html


Why this post?

The IP addresess

One of my confusion was about the IP addresses mentioned in those posts. Are those still valid?Some posts are written in 2015. Fortunately those are still valid.

GoDaddy Records

Another place I was little confused was about setting up A records. There were some A records. Instead of deleting all, I just modified to have GitHub IPs.
Also some sites talk about 2 IP addresses to be added via 2 different A records but some talks about only 1. I had updated 2 records and the site got up with GitHub pages in couple of mins.


Tuesday, July 12, 2016

Using GitHub API to show our projects in personal sites or blog

Introduction

There are still debates going on regarding whether Github is a developer's resume or not. Whatever happens it is good to show the list of GitHub projects in a software engineer's personal site to whom he/she is contributing to.

Integrating Github API to www.JoymonOnline.in

Below is the diagram shows how I have integrated GitHub into my personal site.

www.JoymonOnline.in is a web site

Most of us know the difference between web site and web app. Here in this case we are dealing with a web site where the HTML is generated at server side and send to client. Client browser is supposed to just render the markup given by web server.

How the GitHub API looks like

Its just a URL. More specifically ReST urls which points to resources. Below is one example API url. This gives details about the repository joyful-visualstudio. Just copy paste the url into browser address bar and hit enter. The data will be displayed.

How to call GitHub API

Coming to technical programming aspects. Here in this site the API is called using WebClient class. This is a simple class in .Net framework which can be used to deal with web sites and web APIs.

Authentication / Security

If the request is not authenticated, we can perform read operations. But there is a limit in such calls. That limit is based on the IP address from where the call originates. In this case the call originates from ASP.Net code of http://joymononline.in web site. The site is hosted in shared hosting plan. It means there will be other sites in same server machine. They may call GitHub without authentication and from GitHub point all originate from same IP and it limits.

To get rid of that problem we can authenticate the requests with token. The simple way is to obtain the token from GitHub and keep in the site and transfer via header. The header name used is 'Authorization'. Now the limit is per token, its high limit and no need to worry about shared hosting.

Securing API token

Make sure the token is not public. In JoymonOnline where source is hosted in public repository in Github itself, token is not visible. Its inserted during the deployment time from AppVeyor CI solution.

.Net Client SDK

GitHub also has client sdk to make the interaction easier. If we are using client SDK we don't need to worry about low level http related WebClient class.