Skip to content

Instantly share code, notes, and snippets.

View changhuixu's full-sized avatar
💭
Everyone has a happy ending. If you're not happy, it's not the end.

Changhui Xu changhuixu

💭
Everyone has a happy ending. If you're not happy, it's not the end.
View GitHub Profile
@changhuixu
changhuixu / main.yml
Last active September 2, 2023 07:30
github actions starter workflow template
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first '.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
public void ConfigureServices(IServiceCollection services)
{
var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>();
services.AddSingleton(jwtTokenConfig);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
@changhuixu
changhuixu / BasicAuthFilter.cs
Created January 23, 2020 16:06
ASP.NET Core basic authentication attribute filter
public class BasicAuthFilter : IAuthorizationFilter
{
private readonly string _realm;
public BasicAuthFilter(string realm)
{
_realm = realm;
if (string.IsNullOrWhiteSpace(_realm))
{
throw new ArgumentNullException(nameof(realm), @"Please provide a non-empty realm value.");
@changhuixu
changhuixu / CronJobService.cs
Last active December 14, 2022 14:01
schedule cron job. IHostedService, asp.net core
public abstract class CronJobService : IHostedService, IDisposable
{
private System.Timers.Timer _timer;
private readonly CronExpression _expression;
private readonly TimeZoneInfo _timeZoneInfo;
protected CronJobService(string cronExpression, TimeZoneInfo timeZoneInfo)
{
_expression = CronExpression.Parse(cronExpression);
_timeZoneInfo = timeZoneInfo;
public string GetAccountNumber()
{
static bool IsRecruit(string purposeCode) => new List<string> { "AR", "SR", "FR" }.Contains(purposeCode);
return (GroupTravel, TravelerType, Destination) switch
{
(true, _, "I") => "6034",
(true, _, "O") => "6035",
(true, _, "F") => "6036",
(false, TravelerType.FacultyStaff, "I") => "6025",
(false, TravelerType.FacultyStaff, "O") => "6026",
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3-management
hostname: my-rabbit
volumes:
- ./rabbitmq/etc/definitions.json:/etc/rabbitmq/definitions.json
- ./rabbitmq/etc/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- ./rabbitmq/data:/var/lib/rabbitmq/mnesia/rabbit@my-rabbit
public async Task<string> UploadFile(string filePath)
{
_logger.LogInformation($"Uploading a text file [{filePath}].");
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentNullException(nameof(filePath));
}
if (!File.Exists(filePath))
{
export function appInitializer(authService: AuthService) {
return () =>
new Promise((resolve) => {
console.log('refresh token on app start up')
authService.refreshToken().subscribe().add(resolve);
});
}
@changhuixu
changhuixu / BasicAuthAttribute.cs
Created January 23, 2020 16:04
ASP.NET Core basic authentication attribute filter
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class BasicAuthAttribute : TypeFilterAttribute
{
public BasicAuthAttribute(string realm = @"My Realm") : base(typeof(BasicAuthFilter))
{
Arguments = new object[] { realm };
}
}